I have one menu in my GUI. This menu is used to select which label will be added to the GUI.
When I start the program, the menu already shows the default option selected, but this option isn't used anywhere in the program. It requires user's action (click and select in menu) to get something out of that menu.
I want my program to immediately use the default menu's option, that later can be changed by the user. Can you give me tips not only for this specific label-related task, but in general? How to use the default menu value in the program, without interaction with the menu?
This is my code:
from tkinter import *
root=Tk()
root.title("test")
# root.geometry("400x400")
def selected(event):
myLabel=Label(root, text=clicked.get()).pack()
options=["a","b","c"]
clicked = StringVar()
clicked.set(options[0])
drop=OptionMenu(root,clicked,*options, command=selected)
drop.pack()
root.mainloop()
An easy way:
from tkinter import *
root=Tk()
root.title("test")
# root.geometry("400x400")
def selected(event):
myLabel['text'] = clicked.get()
options=["a","b","c"]
clicked = StringVar()
clicked.set(options[0])
drop=OptionMenu(root,clicked,*options, command=selected)
drop.pack()
myLabel = Label(root, text=clicked.get())
myLabel.pack()
root.mainloop()
Or I suggested you use textvariable
,then you needn't to use a function to change the label:
from tkinter import *
root=Tk()
root.title("test")
# root.geometry("400x400")
options=["a","b","c"]
clicked = StringVar()
clicked.set(options[0])
drop=OptionMenu(root,clicked,*options)
drop.pack()
myLabel = Label(root, textvariable=clicked) # bind a textvariable
myLabel.pack()
root.mainloop()