vendredi 4 septembre 2020

When a Certain Dropdown Value is Selected, How Do You Make A Button Do Something Else? Tkinter

I'm very sorry about the title-- not sure how to explain it + I'm fairly new to Python. What I'm trying to do is make a dropdown menu that makes it so when you press the button it will either convert the text entered into binary or the binary entered into text depending on what you have selected in the dropdown menu. I made a simple dropdown menu and have the first value in the list as the default value for the dropdown menu as shown below

dropDownItems = [
    'Convert Text to Binary',
    'Convert Binary to Text'
]

clicked = tk.StringVar()
clicked.set(dropDownItems[0])

dropDown = tk.OptionMenu(root, clicked, *dropDownItems)
dropDown.pack()

Near the end of the program, I made it so that when clicked.set(dropDownItems[0]), it will convert text into binary. This is the default value so in my mind I'm thinking that this should run when the program is ran.

if clicked.set(dropDownItems[0]):
    myButton = tk.Button(root, text='Convert Text to Binary', command=myClick) #myClick on the left is basically myClick() but tkinter doesn't want the two parenthasis. Change the text to a variable checking what "mode" the thing is in
    myButton.pack(side=BOTTOM, pady=25)

I'm confused because dropDownItems[0] is the default value since I did clicked.set(dropDownItems[0]) so the code above should be running when I run the program (i expected the button to be created when ran but it isn't created). Sorry if this isn't clear, here is the entire program.

https://paste.pythondiscord.com/husobixise.py

Aucun commentaire:

Enregistrer un commentaire