[英]Python Tkinter: Parsing dropdown menu value into a function as a variable?
I am trying to create a Tkinter dropdown menu to add into the main GUI.我正在尝试创建一个 Tkinter 下拉菜单以添加到主 GUI 中。 The purpose of the menu is to give the user several options to choose the image segmentation neural network (currently there are only two options).
菜单的目的是给用户几个选项来选择图像分割神经网络(目前只有两个选项)。
I am having trouble getting the value of the dropdown menu and parsing it through the function as a parameter (variable, not a string).我无法获取下拉菜单的值并通过 function 作为参数(变量,而不是字符串)解析它。
I have tried creating a staticmethod
within the GUI window class to search for the value of the dropdown menu and parse the value through the image segmentation function when the segment button in the GUI is pressed.我尝试在 GUI window class 中创建一个
staticmethod
方法来搜索下拉菜单的值,并在按下 GUI 中的分段按钮时通过图像分割 function 解析该值。 The segmentation function takes two parameters, net
and path
, where it is neural network and image file pathway respectively.分割 function 有两个参数
net
和path
,分别是神经网络和图像文件路径。
I am not too sure how to change the individual parameters of a variable so I just changed the whole variable depending on the value of the dropdown menu.我不太确定如何更改变量的各个参数,所以我只是根据下拉菜单的值更改了整个变量。 The only thing being changed is the
net
parameter of the segment function bind to the Tkinter button.唯一改变的是段 function 绑定到 Tkinter 按钮的
net
参数。
The code:编码:
@staticmethod
def find_option():
if menu1.get() == "fcn":
App.btn2 = Button(App.btn_frame, text="Segment", width = 10, height = 1, cursor = "hand2", command=lambda: App.segment(net=App.fcn, path=App.path))
else:
App.btn2 = btn2 = Button(App.btn_frame, text="Segment", width = 10, height = 1, cursor = "hand2", command=lambda: App.segment(net=App.dlab, path=App.path))
The dropdown menu code:下拉菜单代码:
fcn = models.segmentation.fcn_resnet101(pretrained=True).eval()
dlab = models.segmentation.deeplabv3_resnet101(pretrained=1).eval()
options = ["fcn", "dlab"]
variable = StringVar(btn_frame)
variable.set(options[0]) # default value
menu1 = OptionMenu(btn_frame, variable, *options)
menu1.pack(side=LEFT)
btn_frame
is the frame within the main window containing the buttons. btn_frame
是包含按钮的主 window 内的框架。
App
is the main GUI class. App
是主GUI class。 menu1
needed to be referenced as App.menu1
but it would, for some reason, cause several errors (other class variables would become undefined
and it would say App object has no attribute menu
). menu1
需要被引用为App.menu1
但由于某种原因,它会导致几个错误(其他 class 变量将变得undefined
,并且会说App object has no attribute menu
)。 I also tried referencing menu1
as App().menu1
but would initiate a new window each time I pressed the segment button, also it never actually showed the segmented image.我还尝试将
menu1
引用为App().menu1
但每次按下分段按钮时都会启动一个新的 window ,而且它实际上从未显示分段图像。
Edit: I think you want something like this.编辑:我想你想要这样的东西。 It's hard to know when I can't see the entire code, but could it be that you are forgetting ot pass
self
ot the find_option
method?很难知道我什么时候看不到整个代码,但可能是你忘记了通过
find_option
方法传递self
吗?
# This is the option menu part
self.options = ["fcn", "dlab"]
self.variable = tk.StringVar(btn_frame)
self.variable.set(options[0]) # default value
self.menu1 = tk.OptionMenu(
btn_frame, self.variable, *self.options)
self.menu1.pack(side=LEFT)
# And then later you have this method (remember to pass self to find_option)
def find_option(self):
if self.variable.get() == "fcn":
# your code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.