I am trying to create a Tkinter dropdown menu to add into the main 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).
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. The segmentation function takes two parameters, net
and path
, where it is neural network and image file pathway respectively.
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.
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.
App
is the main 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
). 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.
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?
# 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.