这是我的调试代码:
from tkinter import *
window = Tk()
b1 = window.button(window,text="Dark",command=window.configure(bg='black'))
window.mainloop()
我想添加一个按钮将背景颜色设置为黑色。很简单。但它给出了一个错误:
Traceback (most recent call last):
File "C:/Users/----/Downloads/windows.py", line 3, in <module>
b1 = window.button(window,text="Dark",command=window.configure(bg='black'))
File "C:\Users\----\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2383, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'button'
我不知道如何解决这个问题。
我相信这就是答案:
from tkinter import *
window = Tk()
b1 = Button(window,text="Dark",command=window.configure(bg='black'))
b1.pack()
window.mainloop()
您的错误是您的输入是
b1 = window.button()
而不是 b1 = Button()
此外,你忘了在
b1.pack()
之后添加
b1 = Button()
只需遵循此代码,它就会按预期运行
from tkinter import *
window = Tk()
b1 = Button(window,text="Dark", command=lambda: window.configure(bg='black'))
b1.pack()
window.mainloop()
你犯了几个错误。
b1 = window.button