0

I'm new with Tkinter module and i tried to use OOP with Tkinter. Mycode has no error but the Listbox widget has no output , here is my code :

from tkinter import *
class Test:
   word = ""
   def __init__(seld , w):
       self.word = w.get()

   def Try(self):
       return self.word

win = Tk()
a = StringVar()
win.geometry("750x750")
l1 = Label(win , text = "Name").grid(row = 0 , column = 0)
e1 = Entry(win , textvariable = a).grid(row = 0 , column = 1)
t = Test(a)
b = Button(win , text = "Show" , command = t.Try).grid(row = 1 , column = 0)
lb = Listbox(win , width = 50)
lb.grid(row = 2 , column = 0)
lb.insert(1 , t.Try)
win.mainloop()

can someone help me ? thanks

1 Answer 1

1

When you create your t object, you give I'm the value of a at the moment. So at the creation, the value of a.get() is an empty string, as the program just started to run. You should try this :

from tkinter import *
class Test:
   def __init__(self , w):
       self.w = w

   def Try(self):
       return self.w.get()

win = Tk()
a = StringVar()
win.geometry("750x750")
l1 = Label(win , text = "Name").grid(row = 0 , column = 0)
e1 = Entry(win , textvariable = a).grid(row = 0 , column = 1)
t = Test(a)
b = Button(win , text = "Show" , command = t.Try).grid(row = 1 , column = 0)
lb = Listbox(win , width = 50)
lb.grid(row = 2 , column = 0)
lb.insert(1 , t.Try())
win.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Did you notice that lb.insert(1, t.Try) should be lb.insert(1, t.Try()) instead. However it still inserts empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.