Home > Back-end >  python tkinter module Label widget wrapped in oop
python tkinter module Label widget wrapped in oop

Time:01-25

I am practicing on my personal project to create oop from tkinter module. I have written a code block where I try to create Label widget by imperative code line, then within a class.

from tkinter import *

app=Tk()
app.geometry('500x300')
Button(app,width=13,height=1,text='scrape').pack()
var_str=StringVar()
var_str='result 001 ...'
Label(app,width=33,height=1,text='res',textvariable=var_str).pack()

class label():
    def __init__(self,master,var_text):
        self.label=Label(master,width=33,height=1,textvariable=var_text).pack()

lbl_one=label(app,var_str)
app.mainloop()

The strangeness is if I comment out Label(app,width=33,height=1,text='res',textvariable=var_str).pack() then my object instantiation does not work out. I would like to have a clear answer why lbl_one object gives same text result as with Label(app...) line?

CodePudding user response:

because you probably forgot that u put StringVar at top then changed it to string which not work.

var_str=StringVar(master=app, value="res")

replace that line to this and comment the first label's line then the object would work fine.

  • Related