Python Forum
tkinter AttributeError: 'GUI' object has no attribute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter AttributeError: 'GUI' object has no attribute
#1
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,
Reply
#2
Use
self.genPassText = ...
Reply
#3
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()
pfdjhfuys likes this post
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 788 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  'NoneType' object has no attribute 'get' zunebuggy 8 1,243 Oct-13-2023, 06:39 PM
Last Post: zunebuggy
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 2,244 Feb-20-2023, 09:26 PM
Last Post: deanhystad
  Tkinter object scope riversr54 6 1,870 Feb-17-2023, 05:40 AM
Last Post: deanhystad
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,467 Aug-30-2022, 08:44 AM
Last Post: tompranks
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 15,144 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,236 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  tkinter moving an class object with keybinds gr3yali3n 5 3,183 Feb-10-2021, 09:13 PM
Last Post: deanhystad
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 14,444 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 6,757 Aug-08-2020, 12:47 AM
Last Post: linuxhacker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020