在 optionMenu Tkinter 中获取所选项目的值

Getting the value of selected item in optionMenu Tkinter

我在python的Tkinter中做了一些optionMenu,我想获取用户选择的值。我在单击项目时调用的方法中使用了 var.get() 但我没有获得正确的值。我不断得到 "status",这是我最初使用 var.set() 分配给 var 的值。单击浏览按钮后,我的菜单中的项目将被初始化,因此我在一个名为 browser 的方法中填充了列表。在每个项目的命令属性中,我调用了 justamethod 来打印值。这是我的代码:

        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.

谁能告诉我如何获取选中项的值?

谢谢。

justamethod 函数不会打印除 varLoc 初始化值以外的任何内容,因为您不会用它 任何事情。

由于菜单选项不能采用 variable 参数,与其尝试为所有菜单选项更新一个变量的值,不如为每个菜单按钮传递一些任意值如何?

示例:

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()

(示例直接取自 this tutorial。)

对于您的代码,由于您使用计数器 ifor 循环中制作菜单按钮,您可以简单地执行类似 command = lambda: self.justamethod(i) 的操作,然后在justamethod打印出传递过来的i参数,看看我的意思。

我希望这能帮助您解决问题,因为我无法真正修改您提供的代码来提供解决方案,因为它不可用。