I've made some optionMenu in Tkinter in python, I want to get the value that has been selected by the user. I've used var.get() in the method that gets called when an item is clicked but I'm not getting the correct value. I keep getting "status", which is the value that I've initially assigned to var, using var.set(). The items in my menu get initialized after a browse button is clicked, hence I've filled the list in a method called browser. in the command attribute of each item I've called justamethod to print the value. Here is my code:
self.varLoc= StringVar(master)
self.varLoc.set("status")
self.varColumn= StringVar(master)
self.varColumn.set("")
self.locationColumn= Label(master,text="Select a column as a location indicator", font=("Helvetica", 12))
self.columnLabel= Label(master,text="Select a column to process", font=("Helvetica", 12))
global locationOption
global columnOption
columnOption= OptionMenu (master, self.varColumn,"",*columnList)
locationOption= OptionMenu (master, self.varLoc,"",*columnList)
self.locationColumn.grid (row=5, column=1, pady=(20,0), sticky=W, padx=(5,0))
locationOption.grid (row=5, column=3, pady=(20,0))
def browser (selft):
filename = askopenfilename()
#open_file(filename)
t=threading.Thread (target=open_file, args=(filename, ))
#t=thread.start_new_thread (open_file,(filename, )) # create a new thread to handle the process of opening the file.
# we must then send the file name to the function that reads it somehow.
t.start()
t.join() #I use join because if I didn't,next lines will execute before open_file is completed, this will make columnList empty and the code will not execute.
opt=columnOption.children ['menu']
optLoc= locationOption.children ['menu']
optLoc.entryconfig (0,label= columnList [0], command=selft.justamethod)
opt.entryconfig (0, label= columnList [0], command=selft.justamethod)
for i in range(1,len (columnList)):
opt.add_command (label=columnList[i], command=selft.justamethod)
optLoc.add_command (label=columnList[i], command=selft.justamethod)
def justamethod (self):
print("method is called")
print(self.varLoc.get())
window= Tk () #main window.
starter= Interface (window)
window.mainloop() #keep the window open until the user decides to close it.
Can anyone tell me how to get the value of the selected item?
Thanks.