Issue
I know this code needs to be improved but first I can't figure out why sample_choice a tk.StringVar() seems to not be passed to my if...elif block. What I'm trying to do with the code is display certain labels based upon a selection in a Combobox which saves its current state in sample_choice. It seems like after the first execution of this function the program never loops back into the function even though it is called and in the mainloop and the if...elif block is never reached to take an action on a newly selected value in the Combobox. I think I'm missing something pretty basic here. If more information is needed please let me know and I will upload the class this function is apart of. Thanks in advance!
def pin_interface(self):
path = "Images\\Sample.png"
sample_choice = tk.StringVar()
samples = ("Sample A", "Sample B")
pin_index = ("A1:", "A2:", "A3:", "A4:","B1:", "B2:", "B3:", "B4:")
sample_img = ImageTk.PhotoImage(file = path)
img_label = ttk.Label(self.settings_subframe_2_1, image = sample_img)
img_label.image = sample_img
img_label.grid(row = 0 , column = 0)
label_1 = ttk.Label(self.settings_subframe_2_1, text = "Select a Sample:")
label_1.grid(row = 1, column = 0)
option_1 = ttk.Combobox(self.settings_subframe_2_1, textvariable = sample_choice, values = samples, width = len(samples[0])+1)
option_1.grid(row = 2, column = 0)
pin_label_a1 = ttk.Label(self.settings_subframe_2_1, text = pin_index[0])
pin_label_a2 = ttk.Label(self.settings_subframe_2_1, text = pin_index[1])
pin_label_a3 = ttk.Label(self.settings_subframe_2_1, text = pin_index[2])
pin_label_a4 = ttk.Label(self.settings_subframe_2_1, text = pin_index[3])
pin_label_b1 = ttk.Label(self.settings_subframe_2_1, text = pin_index[4])
pin_label_b2 = ttk.Label(self.settings_subframe_2_1, text = pin_index[5])
pin_label_b3 = ttk.Label(self.settings_subframe_2_1, text = pin_index[6])
pin_label_b4 = ttk.Label(self.settings_subframe_2_1, text = pin_index[7])
if sample_choice.get() == samples[0]:
pin_label_b1.grid_forget()
pin_label_b2.grid_forget()
pin_label_b3.grid_forget()
pin_label_b4.grid_forget()
pin_label_a1.grid(row = 3 , column = 0, sticky = "W")
pin_label_a2.grid(row = 4 , column = 0, sticky = "W")
pin_label_a3.grid(row = 3 , column = 1, sticky = "W")
pin_label_a4.grid(row = 4 , column = 1, sticky = "W")
elif sample_choice.get() == samples[1]:
pin_label_a1.grid_forget()
pin_label_a2.grid_forget()
pin_label_a3.grid_forget()
pin_label_a4.grid_forget()
pin_label_b1.grid(row = 3 , column = 0)
pin_label_b2.grid(row = 4 , column = 0)
pin_label_b3.grid(row = 3 , column = 1)
pin_label_b4.grid(row = 4 , column = 1)
Solution
The code does not work because you check for the selection before the window is even displayed. You should check for the selection after the user changes the value of the Combobox.
In order to do so, you best bind the event of changes in the Combobox to a function that checks the values and takes action. I would suggest making the StringVar an attribute of your tkinter widget, so it can be accessed from all methods:
[...]
self. sample_choice = tk.StringVar() ## Make it an attribute of self
[...]
option_1 = ttk.Combobox(self.settings_subframe_2_1, textvariable = self.sample_choice, values = samples, width = len(samples[0])+1)
option_1.grid(row = 2, column = 0)
option_1 .bind("<<ComboboxSelected>>", self.check_value)
Then, you just need to move your complete if-block to a new method of self, called .check_value()
:
def check_value(self):
if self.sample_choice.get() == samples[0]:
pin_label_b1.grid_forget()
pin_label_b2.grid_forget()
[...]
This method is now executed whenever you make changes to the selection of the combobox.
Answered By - Martin Wettstein
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.