获取optionMenu Tkinter中所选项的值

时间:2015-07-06 11:24:23

标签: python tkinter optionmenu

我在python的Tkinter中做了一些optionMenu,我想得到用户选择的值。我在单击某个项时调用的方法中使用了var.get(),但是我没有得到正确的值。我一直使用var.set()获得“status”,这是我最初赋给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.

有谁能告诉我如何获得所选项目的价值?

感谢。

1 个答案:

答案 0 :(得分:1)

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获取的示例。)

对于您的代码,由于您使用计数器fori循环内制作菜单按钮,因此您可以执行command = lambda: self.justamethod(i)之类的操作,然后在{{justamethod内1}}打印出传递的i参数,看看我的意思。

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