Ozzy Kids
Ozzy Kids

Reputation: 77

How to set an optionmenu with a button click tkinter

I am trying to make a page that has a refresh button on that will reset all the widgets back to what they were originally. The entry widgets are fine, however, I have an optionmenu that starts off with some text already selected. If someone changes this option, then hits the reset button, how do I change it back?

genderList = ["Gender", "Male", "Female", "Other"]
genderVar = tk.StringVar()
genderVar.set(genderList[0])
genderO = tk.OptionMenu(self, genderVar, *genderList)
genderO.config(width = 8)
genderO.pack()

Upvotes: 0

Views: 546

Answers (1)

Anton Gurevich
Anton Gurevich

Reputation: 50

You had your own answer in your code already (line 3) (: You can make a function that is called when the reset button is hit, which will reset genderVar to genderList[0]. This is what it will look like: (Keep in mind I changed the name of the window to Root for testing purposes)

def reset():
    global genderVar
    genderVar.set(genderList[0])

root=tk.Tk()

genderList = ["Gender", "Male", "Female", "Other"]
genderVar = tk.StringVar()
genderVar.set(genderList[0])
genderO = tk.OptionMenu(root, genderVar, *genderList)
genderO.config(width = 8)
genderO.pack()

tk.Button(root,text="Reset",command=reset).pack()

root.mainloop()

Upvotes: 1

Related Questions