Python tkinter测验

时间:2016-01-18 10:53:11

标签: python tkinter

我正在使用tkinter模块在python中进行测验,我仍然坚持如何创建一个按钮来检查答案是否正确。但我会把它放在一个程序中,但问题已经在一个。

import tkinter as tk
window = tk.Tk()
window.title("6 Questions")
window.geometry("500x150")
score = 0
def inst():
    t = tk.Label(window, text="All you need to do is just answer each question with either a '1, 2, 3' or the actual word.")
    t.pack()
def start():
    root = tk.Tk()
    root.title("question 1")
    q = tk.Label(root, text="what type of input holds whole numbers")
    q.pack()
    a = tk.Label(root, text="1.) int")
    a.pack()
    b = tk.Label(root, text="2.) string")
    b.pack()
    c = tk.Label(root, text="3.) float")
    c.pack()
    ans = tk.Entry(root, width=40)
    ans.pack()
    #here is the button I want to verify the answer
    sub = tk.Button(root, text="Submit")
    sub.pack()

greet = tk.Label(window, text="Welcome to the 6 Question Quiz.")
greet.pack()
start = tk.Button(window, command=start, text="Start")
start.pack()
instr = tk.Button(window, text="Instructions", command=inst)
instr.pack()
end = tk.Button(window, text="Exit", command=exit)
end.pack()

2 个答案:

答案 0 :(得分:1)

创建一个在单击提交按钮时打开的功能,并创建RadioButtons而不是Labels

像这样:

def gettingDecision():
    if var.get() is 'True':
        messagebox.showinfo('Congrats', message='You Are Correct.Score is {}'.format(score))
    else:
        messagebox.showinfo('Lose', message='You Are Wrong.')


Question1 = ttk.Label(frame1, text='Q.1.Where does a computer add and compare data ?')
Question1.grid(row=1, column=0, sticky=W)

var = StringVar()
Q1A = ttk.Radiobutton(frame1, text='[A] Hard disk', variable=var, value='False1')
Q1A.grid(row=2, column=0, sticky=W)

Q1B = ttk.Radiobutton(frame1, text='[B] Floppy disk', variable=var, value='False2')
Q1B.grid(row=3, column=0, sticky=W)

Q1C = ttk.Radiobutton(frame1, text='[C] CPU chip', variable=var, value='True')
Q1C.grid(row=4, column=0, sticky=W)

Q1D = ttk.Radiobutton(frame1, text='[D] Memory chip', variable=var, value='False3')
Q1D.grid(row=5, column=0, sticky=W)

submit = ttk.Button(frame1, text='Submit', command=gettingDecision)
submit.grid()

答案 1 :(得分:0)

请注意,理想的方法是使用课程。

您可以在函数内定义函数。

import tkinter as tk
window = tk.Tk()
window.title("6 Questions")
window.geometry("500x150")
score = 0
def inst():
    t = tk.Label(window, text="All you need to do is just answer each question with either a '1, 2, 3' or the actual word.")
    t.pack()

def start():
    def submit():
        print (ans.get())
        #or do whatever you like with this

    root = tk.Tk()
    root.title("question 1")
    q = tk.Label(root, text="what type of input holds whole numbers")
    q.pack()
    a = tk.Label(root, text="1.) int")
    a.pack()
    b = tk.Label(root, text="2.) string")
    b.pack()
    c = tk.Label(root, text="3.) float")
    c.pack()
    ans = tk.Entry(root, width=40)
    ans.pack()
    #here is the button I want to verify the answer
    sub = tk.Button(root, text="Submit", command=submit)
    sub.pack()


greet = tk.Label(window, text="Welcome to the 6 Question Quiz.")
greet.pack()
startButton = tk.Button(window, command=start, text="Start")
startButton.pack()
instr = tk.Button(window, text="Instructions", command=inst)
instr.pack()
end = tk.Button(window, text="Exit", command=window.destroy)
end.pack()

window.mainloop()