2016-01-18 90 views
0

我正在python中使用tkinter模塊進行測驗,我被困在如何創建一個按鈕來檢查答案是否正確。但我會把它放在一個程序中,但問題已經在一個。Python tkinter測驗

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

請記住,SO是_not_代碼寫作服務。你需要做的是創建一個函數來驗證答案並使用'sub = tk.Button(root,text =「Submit」,command = verify)'如果'verify'是函數的名字。 –

+0

如果我在另一個程序中這樣做,它將無法從條目中獲取信息 – morgan

+0

然後在當前程序中聲明程序。不過,我應該告訴你,你打算使用tkinter的類,例如'class Start(tk.Tk):'等 –

回答

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