Getting the value of the selected item in a Tkinter Menu option

I have made several variants of Menu in Tkinter in python, I want to get the value that was selected by the user. I have used var.get () in the method that is called when the item is clicked, but I am not getting the correct value. I keep getting "status" which is the value I originally assigned to var using var.set (). The items in my menu are initialized after the browse button is clicked, so I populated the list in a method called the browser. in the command attribute of each element which I 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?

Thank.

+3


source to share


1 answer


The function justamethod

does not print anything other than the initialized value varLoc

, because you are not doing anything with it.

Since menu options cannot take an argument variable

, instead of trying to update the value of one variable for all menu options, how do you skip some arbitrary value for each 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 are doing the menu buttons in a loop for

using a spinner i

, you can just do something like command = lambda: self.justamethod(i)

and then in a justamethod

print i

that is passed to see what I mean.

I hope this helps you to solve your problem as I cannot change your provided code to suggest a solution as it is unusable.

+1


source







All Articles