简体   繁体   中英

python tkinter module Label widget wrapped in oop

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM