简体   繁体   中英

Python tkinter quiz

I am making a quiz in python using the tkinter module and I am stuck on how to create a button that checks to see if the answer is correct or not. But I would put it in a procedure however the question is already in one.

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

Create a function that opens when the submit button is clicked and create RadioButtons rather than Labels .

Like this:

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

Please note that, ideal way to go would be using classes.

You can define a function, inside of a function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM