Подскажите,как отцентровать текст
Делаю викторину на ткинтере, там должно быть 4 слайда с вопросами (если можно так сказать), а потом должна быть страница с результатами и правильными ответами и у меня почему-то на последней странице не центрируется текст.
Я предположил, что это из-за элементов, которые под текстом находятся и закрытые лейблом, но и их удалить я не смог.
Подскажите, пожалуйста, как сделать это?
class Quiz:
def __init__(self):
self.q_no=0
self.d_no = 0
self.display_question()
self.opt_selected=IntVar()
self.opts=self.radio_buttons()
self.display_options()
self.buttons()
self.data_size=len(question)
self.correct=0
def display_result2(self):
d_no = Label(gui, bg = 'gray',text=description[self.d_no],bd = 350,anchor = 'n',
font=( 'ariel' ,12, 'bold' ))
d_no.place(x = 0, y = 0)
def check_ans(self, q_no):
if self.opt_selected.get() == right_answer[q_no]:
return True
def display_result(self):
wrong_count = self.data_size - self.correct
correct = f"Correct: {self.correct}"
wrong = f"Wrong: {wrong_count}"
score = int(self.correct / self.data_size * 100)
result = f"Score: {score}%"
mb.showinfo("Result",f"{result}\n{correct}\n{wrong}")
def next_btn(self):
if self.check_ans(self.q_no):
self.correct += 1
self.q_no += 1
if self.q_no==self.data_size:
self.display_question().place_forget()
self.display_result()
self.display_result2() #тут поменять на страницу с ответами
# gui.destroy()
else:
self.display_question()
self.display_options()
def buttons(self):
next_button = Button(gui, text="Next",command=self.next_btn, relief=RAISED,
width=10,bg="blue",fg = ['#FACE8D'],font=("ariel",16,"bold"))
next_button.place(x=350,y=370)
quit_button = Button(gui, text="Quit", command=gui.destroy, relief=RAISED,
width=5,bg="black", fg=['#856ff8'],font=("ariel",16," bold"))
quit_button.place(x=700,y=50)
def display_options(self):
val=0
self.opt_selected.set(0)
for option in answers[self.q_no]:
self.opts[val]['text']=option
val+=1
def display_question(self):
q_no = Label(gui,bg = ['#ADADAD'], text=question[self.q_no], width=40,
font=( 'ariel' ,16, 'bold' ), anchor= 'w' )
q_no.place(x=70, y=100)
def radio_buttons(self):
q_list = []
y_pos = 150
while len(q_list) < 4:
radio_btn = Radiobutton(gui,text=" ",variable=self.opt_selected,
value = len(q_list)+1,font = ("ariel",14),
bg = ['#ADADAD'],activebackground = ['#808080'], activeforeground = ['#112244'],
selectcolor = ['#ADADAD'], border = 0,relief = SUNKEN)
q_list.append(radio_btn)
radio_btn.place(x = 100, y = y_pos)
y_pos += 40
return q_list
gui = Tk()
A = Canvas(height=100, width=780)
filename2 = PhotoImage(file = "best_label2.png")
background_label2 = Label(gui, image=filename2,bg = ['#ADADAD'])
background_label2.place(x = 0, y =0)
A.pack()
C = Canvas(height=350, width=780) #450 800
filename = PhotoImage(file = "bg.png")
background_label = Label(gui, image=filename,bg = ['#ADADAD'])
background_label.place(x=0, y=100)
C.pack()
gui.title("Test")
with open('data.json.txt') as f:
data = json.load(f)
question = (data['question'])
answers = (data['answers'])
right_answer = (data[ 'right_answer'])
description = (data['descriptions'])
quiz = Quiz()
gui.resizable(width=False, height=False)
gui.mainloop()