我想以界面的形式创建一个测验。一切似乎都完成了,但我不知道如何添加正确答案的计数器,这将显示在下面和最后的消息框中。编码:
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('Викторина')
root.geometry('600x600')
root["bg"] = "#FFD700"
correctCount = 0
incorrectCount = 0
def que_one():
question = Label(root, text='Тут будет вопрос', font='20', fg='#000000', bg='#FFE4B5')
question_2 = Label(root, text='Введи ответ цифрой: ', font='15', fg='#000000')
answer = Entry(bg='#C0C0C0')
btn = Button(root, text='Ответить', command=lambda: game1(que_two))
btn.grid(row = 0)
btn.place(relx=.5, rely=.8, anchor="c", height=30, width=500, bordermode=OUTSIDE)
question_3 = Label(root, text='1. Тут будет ответ', font='15', bg='#DC143C', fg='#FFFFFF')
question_4 = Label(root, text='2. Тут будет ответ', font='15', bg='#DC143C', fg='#FFFFFF')
question_5 = Label(root, text='3. Тут будет ответ', font='15', bg='#DC143C', fg='#FFFFFF')
question_6 = Label(root, text='Правильных ответов: 0', font='5', bg='#FFD700')
question_6.place(relx=.2, rely=.9, anchor="c", height=25, width=200, bordermode=OUTSIDE)
question.grid(row = 0)
question.place(relx=.5, rely=.1, anchor="c", height=50, width=500, bordermode=OUTSIDE)
question_2.place(relx=.5, rely=.6, anchor="c", height=50, width=500, bordermode=OUTSIDE)
answer.grid(row = 1)
answer.place(relx=.5, rely=.7, anchor="c", height=30, width=500, bordermode=OUTSIDE)
question_3.grid(row = 1)
question_3.place(relx=.5, rely=.2, anchor="c", height=50, width=500, bordermode=OUTSIDE)
question_4.grid(row = 1)
question_4.place(relx=.5, rely=.3, anchor="c", height=50, width=500, bordermode=OUTSIDE)
question_5.grid(row = 1)
question_5.place(relx=.5, rely=.4, anchor="c", height=50, width=500, bordermode=OUTSIDE)
def game1(que_two):
if answer.get().lower() == '1':
correctCount = correctCount + 1
que_two()
else:
incorrectCount = incorrectCount +1
que_two()
def que_two():
question = Label(root, text='Тут будет вопрос', font='20', fg='#000000', bg='#FFE4B5')
question_2 = Label(root, text='Введи ответ цифрой: ', font='15', fg='#000000')
answer = Entry(bg='#C0C0C0')
btn_1 = Button(root, text='Ответить', command=lambda: game2(que_two))
btn_1.grid(row = 0)
btn_1.place(relx=.5, rely=.8, anchor="c", height=30, width=500, bordermode=OUTSIDE)
question_3 = Label(root, text='1. Тут будет ответ', font='15', bg='#DC143C', fg='#FFFFFF')
question_4 = Label(root, text='2. Тут будет ответ', font='15', bg='#DC143C', fg='#FFFFFF')
question_5 = Label(root, text='3. Тут будет ответ', font='15', bg='#DC143C', fg='#FFFFFF')
question.grid(row = 0)
question.place(relx=.5, rely=.1, anchor="c", height=50, width=500, bordermode=OUTSIDE)
question_2.place(relx=.5, rely=.6, anchor="c", height=50, width=500, bordermode=OUTSIDE)
answer.grid(row = 1)
answer.place(relx=.5, rely=.7, anchor="c", height=30, width=500, bordermode=OUTSIDE)
question_3.grid(row = 1)
question_3.place(relx=.5, rely=.2, anchor="c", height=50, width=500, bordermode=OUTSIDE)
question_4.grid(row = 1)
question_4.place(relx=.5, rely=.3, anchor="c", height=50, width=500, bordermode=OUTSIDE)
question_5.grid(row = 1)
question_5.place(relx=.5, rely=.4, anchor="c", height=50, width=500, bordermode=OUTSIDE)
def game2(que_two):
if answer.get().lower() == '2':
que_three();
correctCount = correctCount + 1
else:
que_three();
incorrectCount = incorrectCount +1
def que_three():
question = Label(root, text='Тут будет вопрос', font='20', fg='#000000', bg='#FFE4B5')
question_2 = Label(root, text='Введи ответ цифрой: ', font='15', fg='#000000')
answer = Entry(bg='#C0C0C0')
btn_1 = Button(root, text='Ответить', command=lambda: game3(que_two))
btn_1.grid(row = 0)
btn_1.place(relx=.5, rely=.8, anchor="c", height=30, width=500, bordermode=OUTSIDE)
question_3 = Label(root, text='1. Тут будет ответ', font='15', bg='#DC143C', fg='#FFFFFF')
question_4 = Label(root, text='2. Тут будет ответ', font='15', bg='#DC143C', fg='#FFFFFF')
question_5 = Label(root, text='3. Тут будет ответ', font='15', bg='#DC143C', fg='#FFFFFF')
question.grid(row = 0)
question.place(relx=.5, rely=.1, anchor="c", height=50, width=500, bordermode=OUTSIDE)
question_2.place(relx=.5, rely=.6, anchor="c", height=50, width=500, bordermode=OUTSIDE)
answer.grid(row = 1)
answer.place(relx=.5, rely=.7, anchor="c", height=30, width=500, bordermode=OUTSIDE)
question_3.grid(row = 1)
question_3.place(relx=.5, rely=.2, anchor="c", height=50, width=500, bordermode=OUTSIDE)
question_4.grid(row = 1)
question_4.place(relx=.5, rely=.3, anchor="c", height=50, width=500, bordermode=OUTSIDE)
question_5.grid(row = 1)
question_5.place(relx=.5, rely=.4, anchor="c", height=50, width=500, bordermode=OUTSIDE)
def game3(que_two):
if answer.get().lower() == '2':
correctCount = correctCount + 1
messagebox.showinfo('Правильных ответов: ' + str(correctCount))
else:
incorrectCount = incorrectCount +1
messagebox.showinfo('Правильных ответов: ' + str(correctCount))
que_one()
root.mainloop()
我提出了这个版本的代码(尽管它可以更好地优化):
或者这个选项,更接近你的: