【发布时间】:2021-07-24 22:21:19
【问题描述】:
我也没有太多的 Python 实践,并且在理解错误时存在一个基本问题:AttributeError: 'NoneType' object has no attribute '_root',它仅在我定义 dec 变量 BEFORE 时出现strong> 定义主窗口win:
import tkinter as tk
from tkinter import ttk
from tkinter import *
# This variable must be defined AFTER definition of the Tk() window!
dec = tk.BooleanVar()
# Main window
win = Tk()
# # This variable must be defined AFTER definition of the Tk() window!
# dec = tk.BooleanVar()
decreaseButton = Checkbutton(win, text = "Decrease (optional)", variable = dec)
decreaseButton.grid(row=1, column=1, sticky='W')
# Runs the event loop of Tkinter
win.mainloop()
为什么我必须定义 first 窗口和 than 布尔变量?我对 Tkinter 有什么不明白的地方?
感谢大家的大力帮助和最良好的祝愿 拉尔斯
【问题讨论】:
-
tkinter 中的每个小部件都有必要的参数 master。如果未定义主窗口,则默认设置根窗口
tk.Tk()。因此,如果没有主窗口且没有根窗口,则会引发错误。 Reference