【问题标题】:Python Tkinter: AttributeError: 'str' object has no attribute '_root' (Using Classes)Python Tkinter:AttributeError:\'str\' 对象没有属性 \'_root\'(使用类)
【发布时间】:2022-12-18 04:27:49
【问题描述】:

所以这是代码:

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 小时试图查看答案,但没有一个有效!请帮忙

看了很多答案:(

【问题讨论】:

    标签: python class user-interface tkinter


    【解决方案1】:

    IntVar 和其他变量类的第一个位置参数是拥有该变量的主人。您传递的字符串“on”不能是 master。

    如果要初始化该值,请将其作为关键字参数传递:

    tkinter.StringVar(value="on")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2013-10-28
      • 2015-07-14
      • 2022-01-21
      相关资源
      最近更新 更多