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

标签 python tkinter optionmenu

我在 python 的 Tkinter 中做了一些 optionMenu,我想获得用户选择的值。我在单击项目时调用的方法中使用了 var.get(),但我没有获得正确的值。我不断获得“状态”,这是我最初使用 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 的初始化值以外的任何内容,因为您不会用它任何事情。

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

示例:

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 参数以了解我的意思。

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

关于python - 在 optionMenu Tkinter 中获取所选项目的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31244610/

相关文章:

python - 如何在 tkinter 中插入特殊字符

python - 为特定脚本类型解析 html

python - Google App Engine Datastore 中是否可以有多值字段?

python - Tkinter gui 中的指数函数

python-3.x - 如何在Python中检查线程是否结束

python - Tkinter OptionMenu DisplayOptions 和分配值

python - 测量Python中的快速排序和堆排序的时间

python - pygame混音器正在停止运行程序?

android - 如何在 Activity 开始时显示选项菜单

android-删除右侧选项菜单上子菜单项的标题?