簡體   English   中英

Python Tkinter: AttributeError: 'str' object 沒有屬性 '_root'(使用類)

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

所以這是代碼:

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()

我得到這個錯誤

Traceback (most recent call last):
  File "C:\Users\epicg\OneDrive\Desktop\Code\ExamAppDemo\gui\boot\debug.py", line 7, in <module>
    class DebugPrompt(customtkinter.CTk):
  File "C:\Users\epicg\OneDrive\Desktop\Code\ExamAppDemo\gui\boot\debug.py", line 37, in DebugPrompt
    check_var = tkinter.StringVar("on")
  File "C:\Users\epicg\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 540, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Users\epicg\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 372, in __init__
    self._root = master._root()
AttributeError: 'str' object has no attribute '_root'

我花了 1 小時試圖查看答案,但沒有一個有效! 請幫忙

看了很多答案:(

IntVar和其他變量類的第一個位置參數是擁有該變量的主人。 您傳遞的字符串“on”不能是 master。

如果要初始化該值,請將其作為關鍵字參數傳遞:

tkinter.StringVar(value="on")

這是Variable.__init__的簽名(所有 tkinter 變量類都繼承自此類):

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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM