Stinson, Wynn A Civ USAF AFMC 556 SMXS/MXDED wrote:

[In the future please take the time to choose a meaningful subject]

> Can someone give me some sample code to use to determine if a checkbox
> has been selected using Tkinter? thanks

Method 1: check the state of the underlying variable:

import Tkinter as tk

root = tk.Tk()

var = tk.IntVar()
cb = tk.Checkbutton(root, text="the lights are on", variable=var)
cb.pack()

def showstate():
    if var.get():
        print "the lights are on"
    else:
        print "the lights are off"

button = tk.Button(root, text="show state", command=showstate)
button.pack()

root.mainloop()

Method 2: trigger a function when the underlying variable changes

import Tkinter as tk

root = tk.Tk()

var = tk.IntVar()
cb = tk.Checkbutton(root, text="the lights are on", variable=var)
cb.pack()

def showstate(*args):
    if var.get():
        print "the lights are on"
    else:
        print "the lights are off"

var.trace_variable("w", showstate)
root.mainloop()


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to