tkinter AttributeError: 'GUI' object has no attribute - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: tkinter AttributeError: 'GUI' object has no attribute (/thread-40007.html) |
tkinter AttributeError: 'GUI' object has no attribute - pfdjhfuys - May-18-2023 Hi, When I click the button I expect the entry to be populated. Instead I get the error "AttributeError: 'GUI' object has no attribute 'genPassText'". What am I missing? from tkinter import * class GUI: def DoSomething(self): self.genPassText.delete(0, END) self.genPassText.insert(0,'Hello World') def __init__(self): # root window self.root = Tk() self.root.title("Demo") self.root.geometry("800x280") self.root.resizable(width=FALSE, height=FALSE) # pass length information genPassButton = Button(self.root, text = "Generate Password", command=self.DoSomething).grid(row=8, column=0, padx=5, pady=5) genPassText = Entry(self.root, width=80, bd=3, font=('Bold')).grid(row=8, column=1, padx=5, pady=5) if __name__ == '__main__': mainwindow = GUI() mainloop()Thanks, RE: tkinter AttributeError: 'GUI' object has no attribute - Gribouillis - May-18-2023 Use self.genPassText = ... RE: tkinter AttributeError: 'GUI' object has no attribute - deanhystad - May-18-2023 If you are going to write a class, why not subclass Tk()? # Do not use wildcard imports. They fill the global namespace with names, # many of which you might be unaware. import tkinter as tk # Convention is to use Pascal case for class names. This would be Gui, not GUI. # Following conventions makes it easier for others to read your code. class GUI(tk.Tk): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Normally you want the contents to set the window size # instead of using geometry. self.geometry=("800x280") self.resizable(width=False, height=False) # Don't need to remember the button. No need to assign to a variable. tk.Button( self, text = "Generate Password", command=self.DoSomething ).grid(row=8, column=0, padx=5, pady=5) # Cannot daisychain creating the Entry and setting the grid location. # .grid() returns None, so x = tk.Entry().grid() sets x = None. # For an Entry you should consider using a StringVar. Provides # a cleaner interface for getting or setting the text. self.gen_pass_text = tk.StringVar(self, "") tk.Entry( self, textvariable = self.gen_pass_text, width=80, bd=3, font=('Bold') ).grid(row=8, column=1, padx=5, pady=5) # Convention is to use snake_case for attributes (including methods). # This would be do_someting or dosomething. genPassText would be # gen_pass_test. def DoSomething(self): """Should have a docstring that describes what it does.""" text = self.get_pass_text.get() text = "".join(reversed(text)) self.get_pass_text.set(text) if __name__ == '__main__': mainwindow = GUI() mainwindow.mainloop() RE: tkinter AttributeError: 'GUI' object has no attribute - pfdjhfuys - May-18-2023 (May-18-2023, 03:22 PM)deanhystad Wrote: If you are going to write a class, why not subclass Tk()? Double thank you for the additional information . Your answer is not only helpful but really helps people. |