im trying to program my project so that when I click the button "click it" the text "never gonna give you up" in the title is replaced with whatever text is in the text box. here's most of my code:
class MyApp:
def __init__(self, root):
root.title("never gonna give you up") #title of the window
root.geometry("1000x500") #the size of the window on launch
root.maxsize(2000,1000) #the maximum size of the window
label=Label(root,text='the text thats displayed in the window',bg="red") #creates a 'label' for the window 'root'
label.pack() #displays widget
#using dictionary syntax, one at a time
#label['text']="new label text"
#label["font"]=("Arial",30)
#using configure method
label.configure(text="never gonna give you up", font=("Comic Sans MS", 20))
self.entry_text=StringVar() #used to hold text in entry, can be bound to other widgets
entry=Entry(root, textvariable=self.entry_text) #when this changes stingvar will change as well
entry.pack() #will display widget below other widget
#label["textvariable"]=entry_text #binds label to what is typed in entry
button=Button(root, text="click it",command=self.press_button)
button.pack()
#label.configure(text="new xxx", font=("Comic Sans MS",26))
def press_button(self):
text=self.entry_text.get()
self.label_text.set(text)
whenever I click the button, it returns this error:
line 34, in press_button
self.label_text.set()
AttributeError: 'MyApp' object has no attribute 'label_text'
I've been working on this for a couple days, and I still don't know whats wrong.
CodePudding user response:
self.label_test
is not defined anywhere in the class. That is why you are getting AttributeError
. I guess you want to change the window title too. Also, if you want to modify the label outside __init__()
you need to make the variable accessible globally throughout the class by using self.label = Label()
instead of just label = Label()
in __init__()
. The code below incorporates the required modifications, while also changing the window title.
class MyApp:
def __init__(self, root):
self.root = root #so that root remains accessible everywhere in MyApp and not just here
root.title("never gonna give you up") #title of the window
root.geometry("1000x500") #the size of the window on launch
root.maxsize(2000,1000) #the maximum size of the window
#use self.label instead of label so that it's accessible in press_button()
self.label=Label(root,text='the text thats displayed in the window',bg="red") #creates a 'label' for the window 'root'
self.label.pack() #displays widget
#using dictionary syntax, one at a time
#label['text']="new label text"
#label["font"]=("Arial",30)
#using configure method
self.label.configure(text="never gonna give you up", font=("Comic Sans MS", 20))
self.entry_text=StringVar() #used to hold text in entry, can be bound to other widgets
entry=Entry(root, textvariable=self.entry_text) #when this changes stingvar will change as well
entry.pack() #will display widget below other widget
#label["textvariable"]=entry_text #binds label to what is typed in entry
button=Button(root, text="click it",command=self.press_button)
button.pack()
#label.configure(text="new xxx", font=("Comic Sans MS",26))
def press_button(self):
text=self.entry_text.get()
#self.label_text.set(text) #Will give error as self.label_text isn't defined anywhere in MyApp
self.label.configure(text=text, font=("Comic Sans MS", 20))
self.root.title(text) #will change the window title too
CodePudding user response:
You can use:
class MyApp:
def __init__(self, root):
root.title("never gonna give you up") #title of the window
root.geometry("1000x500") #the size of the window on launch
root.maxsize(2000,1000) #the maximum size of the window
label=Label(root,text='the text thats displayed in the window',bg="red") #creates a 'label' for the window 'root'
label.pack() #displays widget
#using dictionary syntax, one at a time
#label['text']="new label text"
#label["font"]=("Arial",30)
#using configure method
label.configure(text="never gonna give you up", font=("Comic Sans MS", 20))
self.entry_text=StringVar() #used to hold text in entry, can be bound to other widgets
entry=Entry(root, textvariable=self.entry_text) #when this changes stingvar will change as well
entry.pack() #will display widget below other widget
#label["textvariable"]=entry_text #binds label to what is typed in entry
button=Button(root, text="click it",command=self.press_button)
button.pack()
#label.configure(text="new xxx", font=("Comic Sans MS",26))
def press_button(self):
text=self.entry_text.get()
self.label["text"] = text
Your code uses label_text
, but it isn't defined. Normal name is label
. And method "set" is not defined on tkinter.Label
.