Python Tkinter: AttributeError: 'str' object has no attribute '_root' (Using Classes)

Question:

So this is the code:

import customtkinter, tkinter
import win32gui, win32con

customtkinter.set_appearance_mode('system')
customtkinter.set_default_color_theme('blue')

class DebugPrompt(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        self.title("3DT Menu")
        self.attributes('-transparentcolor', self['bg'])
        self.overrideredirect(1)
        self.focus_force()

        self.w = 500
        self.h = 350
        self.sw = self.winfo_screenwidth()
        self.sh = self.winfo_screenheight()
        self.x = (self.sw / 2) - (self.w / 2)
        self.y = (self.sh / 2) - (self.h / 2)
        self.geometry('%dx%d+%d+%d' % (self.w, self.h, self.x, self.y))

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure((0, 1), weight=1)

        self.frame = customtkinter.CTkFrame(master=self)
        self.frame.grid(row=0, column=0, columnspan=1, padx=20, pady=(20, 20))

        self.loadingLabel = customtkinter.CTkLabel(master=self.frame, text='Debug Menu', font=('Roboto', 24))
        self.loadingLabel.grid(row=0, column=0, columnspan=1, padx=20, pady=(20, 20))

        self.hideConsole = False
        self.hideConCheckBX = customtkinter.CTkCheckBox(master=self.frame, text='Disable terminal on program start', variable= check_var,onvalue='on', offvalue='off')
        self.hideConCheckBX.grid(row=1, column=0, columnspan=1, padx=20, pady=(10, 0))

    check_var = tkinter.StringVar("on")


app = DebugPrompt()
app.mainloop()

and I get this error

Traceback (most recent call last):
  File "C:UsersepicgOneDriveDesktopCodeExamAppDemoguibootdebug.py", line 7, in <module>
    class DebugPrompt(customtkinter.CTk):
  File "C:UsersepicgOneDriveDesktopCodeExamAppDemoguibootdebug.py", line 37, in DebugPrompt
    check_var = tkinter.StringVar("on")
  File "C:UsersepicgAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 540, in __init__
    Variable.__init__(self, master, value, name)
  File "C:UsersepicgAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 372, in __init__
    self._root = master._root()
AttributeError: 'str' object has no attribute '_root'

I have spent 1 hour trying to look at answers but none of them worked! Please help

Looked at lots of answers 🙁

Asked By: Dynamic Games

||

Answers:

The first positional parameter to IntVar and the other variable classes is the master that owns the variable. You are passing in the string "on" which cannot be a master.

If you want to initialize the value, pass it as a keyword argument:

tkinter.StringVar(value="on")

This is the signature for Variable.__init__ (all tkinter variable classes inherit from this class):

class Variable:
    def __init__(self, master=None, value=None, name=None):
        """Construct a variable

        MASTER can be given as master widget.
        VALUE is an optional value (defaults to "")
        NAME is an optional Tcl name (defaults to PY_VARnum).

        If NAME matches an existing variable and VALUE is omitted
        then the existing value is retained.
Answered By: Bryan Oakley