tkinter_quiz_show
Table of Contents
TKInter Quiz Show
Quiz Game
For today's lesson we will make a quiz game. You will need to have a program window and a 'play game' window. You will also need to know how to use radio buttons.
Opening Secondary Windows
from tkinter import *
from tkinter.ttk import *
def main():
# creates a Tk() object
root = Tk()
# sets the geometry of root window
root.geometry("200x200")
label = Label(master, text="This is the main window")
label.pack(pady=10)
button1 = Button(root,
text="Click to open a new window",
command=openNewWindow)
button1.pack(pady=10)
# mainloop, runs infinitely
root.mainloop()
# function to open a new window
def openNewWindow(oldwin):
# Toplevel object which will
# be treated as a new window
newRoot = Toplevel(oldwin)
# sets the title of the
# Toplevel widget
newRoot.title("New Window")
# sets the geometry of toplevel
newRoot.geometry("200x200")
# A Label widget to show in toplevel
Label(newRoot, text="This is a new window").pack()
if __name__ == "__main__":
main()
Radio Buttons
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
# root window
root = tk.Tk()
root.geometry('300x220')
root.resizable(False, False)
root.title('Quiz Game!')
def show_selected_size():
if selected_size.get() == 'F':
showinfo(
title='Result',
message="Yes, Bears like to eat fish."
)
else:
showinfo(
title='Result',
message="No, please try again."
)
selected_size = tk.StringVar()
sizes = (('Cookies', 'C'),
('Pizza', 'P'),
('Fish', 'F'),
('Cake', 'K'))
# label
label = ttk.Label(text="What do Bears like to eat?")
label.pack(fill='x', padx=5, pady=5)
# radio buttons
for size in sizes:
r = ttk.Radiobutton(
root,
text=size[0],
value=size[1],
variable=selected_size
)
r.pack(fill='x', padx=5, pady=5)
# OK button
button = ttk.Button(
root,
text="Ok!",
command=show_selected_size)
button.pack(fill='x', padx=5, pady=5)
root.mainloop()
tkinter_quiz_show.txt · Last modified: 2024/04/20 01:10 by appledog
