I am writing an application using python 3 and tkinter where I have a listbox and an empty label that should update in response to the listbox selection. I need to do it using polymorphism and I cannot write this method in the main file. Here is what I am doing:
And the same for index == 1 it prints in the shell the names of the methods chosen and keep printing it, so it seems to be working, but what I really need is to some how update label2.
For example if I click on the method 1 in the listbox I would like to set my lable2 to 'Days' and if click on method 2 label 2 changes for 'Hours'
Can anyone spot where is the problem?... I cannot think in anything else
Thanks in advance! :)
Found the solution!
class App(Frame): def __init__(self, master = None): ttk.Frame.__init__(self, master, relief = GROOVE) self.master.minsize(600, 480) self.master.title("App") self.grid() # App label 1 self.label1 = ttk.Label(self, text = "Select:") self.label1.grid(row = 0, column = 0) # Methods list self.methods = Listbox(self, selectmode=SINGLE, width=10,height=3) self.methods.grid(row = 0, column = 1) # Get a the methods names from anther file that is inside of a class which return an list # Then I go through the list and append the values to the list box self.getMethods = UI().methodsNames('methods.txt') for mode in self.getMethods: self.methods.insert(1, mode) # That is the part that I have to update the label next to the listbox # App label2 # First I create the label self.labelText = StringVar() self.label2 = ttk.Label(self, text = self.labelText.get()) self.label2.grid(row = 0, column = 2) # then a bind to call the method inside of the calls in the other file update = self.methods.bind('<<ListboxSelect>>', UI().parameterNames) self.label2.set(update) if __name__ == "__main__": App().mainloop()And here is the method that I call with the ListboxSelect command which is located in another file.
Class UI: def parameterNames(self, event): widget = event.widget index = widget.curselection()[0] selection = widget.get(index) if index == 0: return selection if index == 1: return selectionI am returning the selection value just to test if it works. However, the label does not change at all and also I am not able to access the returned value. If I put a print statement in that method, like if index == 0: print selection
And the same for index == 1 it prints in the shell the names of the methods chosen and keep printing it, so it seems to be working, but what I really need is to some how update label2.
For example if I click on the method 1 in the listbox I would like to set my lable2 to 'Days' and if click on method 2 label 2 changes for 'Hours'
Can anyone spot where is the problem?... I cannot think in anything else
Thanks in advance! :)
(Mar-21-2017, 09:38 PM)Dannds Wrote: I am writing an application using python 3 and tkinter where I have a listbox and an empty label that should update in response to the listbox selection. I need to do it using polymorphism and I cannot write this method in the main file. Here is what I am doing:
class App(Frame): def __init__(self, master = None): ttk.Frame.__init__(self, master, relief = GROOVE) self.master.minsize(600, 480) self.master.title("App") self.grid() # App label 1 self.label1 = ttk.Label(self, text = "Select:") self.label1.grid(row = 0, column = 0) # Methods list self.methods = Listbox(self, selectmode=SINGLE, width=10,height=3) self.methods.grid(row = 0, column = 1) # Get a the methods names from anther file that is inside of a class which return an list # Then I go through the list and append the values to the list box self.getMethods = UI().methodsNames('methods.txt') for mode in self.getMethods: self.methods.insert(1, mode) # That is the part that I have to update the label next to the listbox # App label2 # First I create the label self.labelText = StringVar() self.label2 = ttk.Label(self, text = self.labelText.get()) self.label2.grid(row = 0, column = 2) # then a bind to call the method inside of the calls in the other file update = self.methods.bind('<<ListboxSelect>>', UI().parameterNames) self.label2.set(update) if __name__ == "__main__": App().mainloop()And here is the method that I call with the ListboxSelect command which is located in another file.
Class UI: def parameterNames(self, event): widget = event.widget index = widget.curselection()[0] selection = widget.get(index) if index == 0: return selection if index == 1: return selectionI am returning the selection value just to test if it works. However, the label does not change at all and also I am not able to access the returned value. If I put a print statement in that method, like if index == 0: print selection
And the same for index == 1 it prints in the shell the names of the methods chosen and keep printing it, so it seems to be working, but what I really need is to some how update label2.
For example if I click on the method 1 in the listbox I would like to set my lable2 to 'Days' and if click on method 2 label 2 changes for 'Hours'
Can anyone spot where is the problem?... I cannot think in anything else
Thanks in advance! :)
Found the solution!