python - 使用 Tkinter 的 Python 测验
问题描述
对于一个项目,我需要创建一个教育计划,并且我选择了一个测验。我无法让程序识别标志已增加到 3。这是我如何增加标志或其他问题的问题吗?我对python(或tkinter)不太熟悉,所以请不要太苛刻。
from tkinter import *
from tkinter import ttk
root = Tk()
notebook = ttk.Notebook(root)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
frame3 = ttk.Frame(notebook)
notebook.add(frame1, text="Q1")
notebook.add(frame2, text="Q2")
notebook.add(frame3, text="Q3")
def displayscore(total):
score = Tk()
score.geometry("760x450")
score.title("Score")
score.configure(background="#000138")
Label(score, text="Your score is:", fg="Black", font="Arial 36").pack()
Label(score, textvariable=total, fg="Black", font="Arial 36").pack()
def flagset():
flag.set(flag.get() + 1)
if flag == 3:
displayscore(total)
def totalset():
total.set(total.get() + 1)
def main():
#Question 1
def correct1():
clear1()
totalset()
flagset()
def clear1():
list = frame1.grid_slaves()
for l in list:
l.destroy()
Label(frame1, text="Where is the '7' in 1704?").grid(row=0)
flagset()
Label(frame1, text="Where is the '7' in 1704?").grid(row=0)
Button(frame1, text="In the Thousands", command=clear1, width=15).grid(row=1, sticky=W)
Button(frame1, text="In the Hundreds", command=correct1, width=15).grid(row=2, sticky=W)
Button(frame1, text="In the Tens", command=clear1, width=15).grid(row=3, sticky=W)
Button(frame1, text="In the Ones", command=clear1, width=15).grid(row=4, sticky=W)
#Question2
def correct2():
clear2()
totalset()
flagset()
def clear2():
list = frame2.grid_slaves()
for l in list:
l.destroy()
Label(frame2, text="What is 1704 in words?").grid(row=0)
flagset()
Label(frame2, text="What is 1704 in words?").grid(row=0)
Button(frame2, text="One Million, Seven thousand and Four", command=clear2, width=35).grid(row=1, sticky=W)
Button(frame2, text="One Hundred and Seventy Four", command=clear2, width=35).grid(row=2, sticky=W)
Button(frame2, text="One Thousand and Seventy Four", command=clear2, width=35).grid(row=3, sticky=W)
Button(frame2, text="One Thousand, Seven Hundred and Four", command=correct2, width=35).grid(row=4, sticky=W)
#Question 3
def correct3():
clear3()
totalset()
flagset()
def clear3():
list = frame3.grid_slaves()
for l in list:
l.destroy()
Label(frame3, text="Find ?: 3 x ? = 18").grid(row=0, column=2)
flagset()
Label(frame3, text="Find ?: 3 x ? = 18").grid(row=0, column=2)
Button(frame3, text="6", command=correct3, width=15).grid(row=1, column=1, sticky=W)
Button(frame3, text="7", command=clear3, width=15).grid(row=2, sticky=W)
Button(frame3, text="8", command=clear3, width=15).grid(row=3, sticky=W)
Button(frame3, text="9", command=clear3, width=15).grid(row=4, sticky=W)
flag = IntVar()
total = IntVar()
main()
notebook.pack()
root.mainloop()
解决方案
flag
是一个 tkinter IntVar
,所以flag == 3
永远不会是真的。
相反,使用flag.get() == 3
.
此外,当给出正确答案时,您会增加flag
两次,一次在clear
函数中,一次在correct
函数中。
此外,您在运行之前运行flagset
(from ),因此在显示分数之前不会更新最后一个答案。clear
totalset
total
推荐阅读
- c++ - 用 C++ 将 OpenCV 帧写入磁盘:单线程写入速度是否受磁盘吞吐量以外的任何限制?
- aws-cli - AWS CLI 根据标签过滤图像
- java - 如何使用 Maven 对任何存储库中不可用的非 Java 依赖项进行建模?
- c# - 将查询和标头参数绑定到同一个模型
- c# - 以编程方式将 3D 模型添加到 PowerPoint 演示文稿
- java - Maven JavaDoc Plugin 输出不正确的参数对齐
- mongodb - MongoDB聚合组和计数嵌套数组
- c# - 如何将调试器“附加”到 WPF 窗口按钮?
- ios - 尝试在内容视图中使用 SwiftUI 获取用户位置但显示 nil
- angularjs - 如何在 ng-repeat angularJs 中仅突出显示表的第一行?