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
#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


Messages In This Thread
RE: tkinter AttributeError: 'GUI' object has no attribute - by pfdjhfuys - May-18-2023, 03:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 870 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  'NoneType' object has no attribute 'get' zunebuggy 8 1,404 Oct-13-2023, 06:39 PM
Last Post: zunebuggy
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 2,348 Feb-20-2023, 09:26 PM
Last Post: deanhystad
  Tkinter object scope riversr54 6 1,999 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,574 Aug-30-2022, 08:44 AM
Last Post: tompranks
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 15,533 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,481 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  tkinter moving an class object with keybinds gr3yali3n 5 3,301 Feb-10-2021, 09:13 PM
Last Post: deanhystad
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 14,713 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 6,910 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