External Exam Download Resources Web Applications Games Recycle Bin

Radio Buttons

radio buttons.py

#1. import and init:
import random
from tkinter import *
from tkinter import messagebox
window = Tk()

#2. widgets and variables:
magic = 0
play = Button(text="launch game")
character = StringVar()
wizard = Radiobutton(text="wizard", variable=character, value="wizard")
elf = Radiobutton(text="elf", variable=character, value="elf")

#3. events:
def launch(event):
    global magic
    if character.get() == "wizard":
        magic = random.randint(14,21)
    else:
        magic = random.randint(7,12)
    messagebox.showinfo(character.get(), "Magic: " + str(magic))
    
#4. bindings:
play.bind("<ButtonPress>", launch)

#5. pack:
wizard.pack(side="top", anchor="nw")
elf.pack(side="top", anchor="nw")
play.pack(side="top", anchor="nw")

#6. run:
window.mainloop()