访问 OptionMenu 项目 - Tkinter
Access OptionMenu items - Tkinter
我有四个 OptionMenu,每个 OptionMenu 都填充了一个条目列表(每个菜单都相同)。
from tkinter import *
def selected (event):
print (clicked.get())
window = Tk()
entries = ["a", "b", "c", "d"]
for i in range (4):
window.rowconfigure (i, minsize = 5)
a_frame = Frame (master = window)
a_frame.grid (row = i, column = 0, padx = 1, pady = 1)
clicked = StringVar()
clicked.set (entries[0])
a_menu = OptionMenu (a_frame, clicked, *entries, command = selected)
a_menu.pack()
window.mainloop()
我试图从每个 OptionMenu 访问值 selected,但我只打印了底部 OptionMenu 的值。当我 select 来自其他下拉菜单的一些值时,操作被“检测到”但仅打印底部 OptionMenu 的值。
如果您尝试 运行 上面的代码,您会很容易理解我的意思。如何从每个 OptionMenu 访问每个值 I select?非常感谢!
OptionMenu 构造函数:
OptionMenu(parent, variable, choice1, choice2, ...)
from tkinter import Tk, StringVar, OptionMenu
def callback(arg):
print(arg)
root = Tk()
option = StringVar()
op = OptionMenu(root, option, 'a', 'b', 'c', 'd', command=callback)
op.grid()
option.set('a')
root.mainloop()
这是您可以开始的基础。调用回调函数时,它将您的选择作为参数传递,并仅将参数打印到控制台。
希望你现在已经有了基本的了解。
因此,在您的情况下,您需要打印参数而不是“print (clicked.get())”
试试这个:
from tkinter import *
def selected (event):
print (event)
window = Tk()
entries = ["a", "b", "c", "d"]
for i in range (4):
window.rowconfigure (i, minsize = 5)
a_frame = Frame (master = window)
a_frame.grid (row = i, column = 0, padx = 1, pady = 1)
clicked = StringVar()
clicked.set (entries[0])
a_menu = OptionMenu (a_frame, clicked, *entries, command = selected)
a_menu.pack()
window.mainloop()
我有四个 OptionMenu,每个 OptionMenu 都填充了一个条目列表(每个菜单都相同)。
from tkinter import *
def selected (event):
print (clicked.get())
window = Tk()
entries = ["a", "b", "c", "d"]
for i in range (4):
window.rowconfigure (i, minsize = 5)
a_frame = Frame (master = window)
a_frame.grid (row = i, column = 0, padx = 1, pady = 1)
clicked = StringVar()
clicked.set (entries[0])
a_menu = OptionMenu (a_frame, clicked, *entries, command = selected)
a_menu.pack()
window.mainloop()
我试图从每个 OptionMenu 访问值 selected,但我只打印了底部 OptionMenu 的值。当我 select 来自其他下拉菜单的一些值时,操作被“检测到”但仅打印底部 OptionMenu 的值。 如果您尝试 运行 上面的代码,您会很容易理解我的意思。如何从每个 OptionMenu 访问每个值 I select?非常感谢!
OptionMenu 构造函数:
OptionMenu(parent, variable, choice1, choice2, ...)
from tkinter import Tk, StringVar, OptionMenu
def callback(arg):
print(arg)
root = Tk()
option = StringVar()
op = OptionMenu(root, option, 'a', 'b', 'c', 'd', command=callback)
op.grid()
option.set('a')
root.mainloop()
这是您可以开始的基础。调用回调函数时,它将您的选择作为参数传递,并仅将参数打印到控制台。
希望你现在已经有了基本的了解。
因此,在您的情况下,您需要打印参数而不是“print (clicked.get())”
试试这个:
from tkinter import *
def selected (event):
print (event)
window = Tk()
entries = ["a", "b", "c", "d"]
for i in range (4):
window.rowconfigure (i, minsize = 5)
a_frame = Frame (master = window)
a_frame.grid (row = i, column = 0, padx = 1, pady = 1)
clicked = StringVar()
clicked.set (entries[0])
a_menu = OptionMenu (a_frame, clicked, *entries, command = selected)
a_menu.pack()
window.mainloop()