Python Forum

Full Version: tkinter AttributeError: 'GUI' object has no attribute
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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,
Use
self.genPassText = ...
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()
(May-18-2023, 03:22 PM)deanhystad Wrote: [ -> ]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()

Double thank you for the additional information Heart. Your answer is not only helpful but really helps people.