【问题标题】:Python multiple choice quiz scoring using tkinter使用 tkinter 的 Python 多项选择测验评分
【发布时间】:2018-02-01 02:28:31
【问题描述】:

我正在为我的编码课程做一个项目,它是一个简单的高中初学者课​​程。该作业是一项多项选择测验,还计算正确答案的数量。该代码使用 Tkinter,特别是 Notebook。我很难计算分数。如果我将它设置为变量,它只会返回 0,当正确答案正确时它应该显示数字增加。有什么帮助吗??

global total

y=0  
from tkinter import *
from tkinter import ttk
n= ttk.Notebook()
f1= ttk.Frame(n)
f2= ttk.Frame(n)
f3= ttk.Frame(n)
f4= ttk.Frame(n)
f5= ttk.Frame(n)
f6= ttk.Frame(n)

window= ttk.Frame(n)


def main(x):
    global total
    n.add(f1, text="One")
    n.add(f2, text="Two")
    n.add(f3, text="Three")
    n.add(f4, text="Four")
    n.add(f5, text="Five")
    n.add(f6, text="Six")

    total= ttk.Label(window, text="0")


    Label(f1, text="What is Tkinter?").grid(row=2,column=2)
    Button(f1, text="Guided User Interface").bind(correct)
    Button(f1, text="Guided User Interface").grid(row=3,column=1)
    Button(f1, text="Variable", command=incorrect).grid(row=3,column=2)
    Button(f1, text="Function", command=incorrect).grid(row=3,column=3)


    Label(f2, text="What is Turtle?").grid(row=2,column=2)
    Button(f2, 
    text="GuidedUserInterface",command=incorrect2).grid(row=3,column=1)
    Button(f2, text="Module", command=correct2).grid(row=3,column=2)
    Button(f2, text="Boolean Value", command=incorrect2).grid(row=3,column=3)

    Label(f3, text="What does the 'Print' command do?").grid(row=2,column=2)
    Button(f3, text="Creater a window",command=incorrect3).grid(row=3,column=1)
    Button(f3, text="Show a message in the Python Shell", command=correct3).grid(row=3,column=2)
    Button(f3, text="Print to the printer", command=incorrect3).grid(row=3,column=3)

    Label(f4, text="What is the moniter?").grid(row=2,column=2)
    Button(f4, text="A display that shows what the computer is doing",command=correct4).grid(row=3,column=1)
    Button(f4, text="A circut board", command=incorrect4).grid(row=3,column=2)
    Button(f4, text="A Program", command=incorrect4).grid(row=3,column=3)


    Label(f5, text="What does the 'from ____ import' command do?").grid(row=2,column=2)
    Button(f5, text="Import an image",command=incorrect5).grid(row=3,column=1)
    Button(f5, text="Import text", command=incorrect5).grid(row=3,column=2)
    Button(f5, text="Import a module", command=correct5).grid(row=3,column=3)

    Label(f6, text="Which of these is a Boolean Value?").grid(row=2,column=2)
    Button(f6, text="Enter",command=incorrect6).grid(row=3,column=1)
    Button(f6, text="Esc", command=incorrect6).grid(row=3,column=2)
    Button(f6, text="True", command=correct6).grid(row=3,column=3)
    return total



def correct():
    global total
    Label(f1, text="Correct").grid(row=1,column=2)
    counter()
def incorrect():
    Label(f1, text="Incorrect").grid(row=1,column=2)

def correct2():
    global total
    Label(f2, text="Correct").grid(row=1,column=2)
    counter()

def incorrect2():
    Label(f2, text="Incorrect").grid(row=1,column=2)

def correct3():
    global total
    Label(f3, text="Correct").grid(row=1,column=2)
    counter()

def incorrect3():
    Label(f3, text="Incorrect").grid(row=1,column=2)

def correct4():
    global total
    Label(f4, text="Correct").grid(row=1,column=2)
    counter()

def incorrect4():
    Label(f4, text="Incorrect").grid(row=1,column=2)

def correct5():
    global total
    Label(f5, text="Correct").grid(row=1,column=2)
    counter()

def incorrect5():
    Label(f5, text="Incorrect").grid(row=1,column=2)

def correct6():
    global total
    Label(f6, text="Correct").grid(row=1,column=2)
    counter()

def incorrect6():
    Label(f6, text="Incorrect").grid(row=1,column=2)


def counter():
    global total
    total['text'] = str(int(total['text']) + 1)


main(y)


n.pack()

n.mainloop()

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    我相信在这种情况下 tkinter 的变量类会有所帮助,特别是 IntVar 类。我在下面修改了您的代码,使 total 成为 IntVar,但将其简化为三个问题以简化示例:

    from tkinter import *
    from tkinter import ttk
    
    def main():
    
        notebook.add(frame1, text="One")
        notebook.add(frame2, text="Two")
        notebook.add(frame3, text="Three")
    
        Label(frame1, text="What is Tkinter?").grid(row=2, column=2)
        Button(frame1, text="Guided User Interface", command=correct1).grid(row=3, column=1)
        Button(frame1, text="Variable", command=incorrect1).grid(row=3, column=2)
        Button(frame1, text="Function", command=incorrect1).grid(row=3, column=3)
    
        Label(frame2, text="What is Turtle?").grid(row=2, column=2)
        Button(frame2, text="Guided User Interface", command=incorrect2).grid(row=3, column=1)
        Button(frame2, text="Module", command=correct2).grid(row=3, column=2)
        Button(frame2, text="Boolean Value", command=incorrect2).grid(row=3, column=3)
    
        Label(frame3, text="What does the 'Print' command do?").grid(row=2, column=2)
        Button(frame3, text="Create a window", command=incorrect3).grid(row=3, column=1)
        Button(frame3, text="Show a message in the Python Shell", command=correct3).grid(row=3, column=2)
        Button(frame3, text="Print to the printer", command=incorrect3).grid(row=3, column=3)
    
        notebook.pack()
    
        Label(root, text="Total:").pack()
        Label(root, textvariable=total).pack()
    
    def correct1():
        Label(frame1, text="Correct").grid(row=1, column=2)
        counter()
    
    def incorrect1():
        Label(frame1, text="Incorrect").grid(row=1, column=2)
    
    def correct2():
        Label(frame2, text="Correct").grid(row=1, column=2)
        counter()
    
    def incorrect2():
        Label(frame2, text="Incorrect").grid(row=1, column=2)
    
    def correct3():
        Label(frame3, text="Correct").grid(row=1, column=2)
        counter()
    
    def incorrect3():
        Label(frame3, text="Incorrect").grid(row=1, column=2)
    
    def counter():
        total.set(total.get() + 1)
    
    root = Tk()
    
    total = IntVar()  # defaults to 0
    
    notebook = ttk.Notebook(root)
    
    frame1 = ttk.Frame(notebook)
    frame2 = ttk.Frame(notebook)
    frame3 = ttk.Frame(notebook)
    
    main()
    
    root.mainloop()
    

    您可以显式设置标签文本,而不是使用IntVar,但这似乎是变量类存在的经典示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      • 2015-12-22
      • 2016-07-09
      • 2015-09-10
      相关资源
      最近更新 更多