I am new to programming I started a new project (Quiz Project) on Python And I use Tkinter for graphic interface So, I want that when I ask a question to the user, he choose the right one, and I will output "Great", else output "False, try again", all that with Tkinter
If I am not understood, I will show a little example. Example : Who slapped Chris Rock at the Oscars ? • Willow Smith • Will Smith • Jaden Smith Choose the correct answer !
So right now, if he chooses Will Smith, it shall output "Great", else "False, try again"
Sorry, If I did some mistakes in my sentence.
CodePudding user response:
You could do that easily with a combobox. Check out the following example.
import tkinter as tk
from tkinter import ttk
def analyze():
selection = combo_box.get()
combo_box.delete(0, tk.END)
correct_answer = "Will Smith"
if selection == correct_answer:
label["text"] = "Great!!"
elif selection != correct_answer:
label["text"] = "False, try again"
name_list = ["Willow Smith", "Will Smith", "Jaden Smith"]
root = tk.Tk()
root.geometry("800x800")
root.resizable(False, False)
question_label = tk.Label(root, text="Who slapped Chris Rock at the Oscars?")
question_label.pack()
combo_box = ttk.Combobox(root, font=20, state="normal")
combo_box['values'] = tuple(name_list)
combo_box.pack()
button = tk.Button(root, bg="green", width=10, text="Enter", command=lambda: analyze())
button.pack()
label = tk.Label(root, font="times 16")
label.pack()
root.mainloop()
CodePudding user response:
Well, you could just put in a label and then configure that label again and again using the configure()
function. For instance:
r = Tk()
def check_answer(option):
result = Label(r, font="Arial 22", text="")
if option == True:
result.configure(text="Great!!!")
result.grid(row=2, column=0)
else:
result.configure(text="False, try again!!!")
result.grid(row=2, column=0)
qst = Label(r, text="Who slapped Chris Rock at the Oscars?", font=("Arial 20", 20))
qst.grid(row=0, column=0)
op1 = Button(r, text="Willow Smith", font="Arial 16", command=lambda: check_answer(False))
op2 = Button(r, text="Will Smith", font="Arial 16", command=lambda: check_answer(True))
op3 = Button(r, text="Jaden Smith", font="Arial 16", command=lambda: check_answer(False))
op1.grid(row=1, column=0)
op2.grid(row=1, column=1)
op3.grid(row=1, column=2)
r.title('Quiz')
r.mainloop()
Hope this helps.