Taylor's Blog

Atypical ramblings

Learning to .trace()

I been putting in some good work on my project. The latest problem has been getting the .trace() method in Tkinter to work properly. I finally managed to produce something usable today. It’s a simple example of how .trace() can be used to change the value of a label based on what is select from an OptionMenu widget. My next task will be having one OptionMenu change another OptionMenu’s choices.
[python]
root = Tk()

def myFunction(*args):
myLabel.config(text=someVariable.get())

myLabel = Label(root, text=’Blank’)
myLabel.pack()

someVariable = StringVar(root)
someVariable.set(“select”) # default value
someVariable.trace(“w”, myFunction)

myMenu = OptionMenu(root, someVariable, “one”, “two”)
myMenu.pack()

root.mainloop()
[/python]

Updated: April 23, 2015 — 5:26 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

Taylor's Blog © 2015