Python Forum
Setup a label by binding listbox importing a subclass method using python 3 and tkint
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setup a label by binding listbox importing a subclass method using python 3 and tkint
#1
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 selection
I 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 selection
I 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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open a setup python file? Adlo777 2 1,899 Jan-28-2021, 08:10 PM
Last Post: nilamo
  Python 3.6.4 setup fails embeeusername 2 4,832 Mar-16-2018, 02:29 PM
Last Post: sparkz_alot
  Python UI - Getting Label to show to left of QLineEdit ijosefson 1 2,876 Nov-11-2017, 01:54 AM
Last Post: ijosefson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020