簡體   English   中英

Python Tkinter測驗

[英]Python tkinter quiz

我正在使用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()

創建一個單擊提交按鈕時打開的函數,並創建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()

請注意,理想的方法是使用類。

您可以在函數內部定義函數。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM