External Exam Download Resources Web Applications Games Recycle Bin

Checkboxes

checkboxes.py

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

#2. vars and form controls:
go = Button()
homework = BooleanVar(value = True) #checked on start up
chkhomework = Checkbutton(text="Homework assigned?",
                          onvalue=True,
                          offvalue=False,
                          variable=homework)

#3. events:
def lesson(event):
    if homework.get():
        messagebox.showinfo("Homework!", "no playing")
    else:
        messagebox.showinfo("No homework!", "go play")
    
#4. bindings:
go.bind("<ButtonPress>", lesson)

#5. pack:
go.pack(side="top", anchor="nw")
chkhomework.pack(side="top", anchor="nw")

#6. run:
window.mainloop()