Reputation: 1688
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.
Upvotes: 3
Views: 5741
Reputation: 2115
The justamethod
function won't print anything other than the initialized value of varLoc
because you don't do anything with it.
Since menu options cannot take a variable
argument, instead of trying to update the value of one variable for all menu options, how about passing some arbitrary value for each one of the menu buttons?
Example:
from Tkinter import *
root = Tk()
def callback(var):
print ("%d" %var)
menubar = Menu(root)
# create a pulldown menu, and add it to the menu bar
filemenu = Menu(menubar, tearoff=0)
filemenu.add("command", label="Open", command=lambda: callback(1))
filemenu.add("command", label="Save", command=lambda: callback(2))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
# create more pulldown menus
editmenu = Menu(menubar, tearoff=0)
editmenu.add("command", label="Cut", command=lambda: callback(3))
editmenu.add("command", label="Copy", command=lambda: callback(4))
editmenu.add("command", label="Paste", command=lambda: callback(5))
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add("command", label="About", command=lambda: callback(6))
menubar.add_cascade(label="Help", menu=helpmenu)
# display the menu
root.config(menu=menubar)
root.mainloop()
(Example taken directly from this tutorial.)
In the case of your code, since you make the menu buttons within a for
loop using the counter i
, you could simply do something like command = lambda: self.justamethod(i)
, and then within justamethod
print out the i
argument that is passed to see what I mean.
I hope this helps you to solve your problem, as I cannot really modify your provided code to offer a solution as it is unusable.
Upvotes: 1