在TKINTER中使用窗口按钮时出错
这是我的调试代码:
from tkinter import *
window = Tk()
b1 = window.button(window,text="Dark",command=window.configure(bg='black'))
window.mainloop()
我想添加一个按钮将BG颜色设置为黑色。很简单。但这给出了一个错误:
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'
我不确定如何解决此问题。
Here is my code for debugging:
from tkinter import *
window = Tk()
b1 = window.button(window,text="Dark",command=window.configure(bg='black'))
window.mainloop()
I want to add a button to set the bg color to black. Pretty simple. But it gives an error:
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'
I'm not sure how to fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这是答案:
您的错误是您的输入为
b1 = window.button()
而不是b1 = button()
此外,您忘了添加
b1.pack()
b1 = button()
I believe that this is the answer:
Your mistake was that your input was
b1 = window.button()
instead ofb1 = Button()
Moreover, you forgot to add in a
b1.pack()
after theb1 = Button()
只需遵循此代码,它将按照预期运行,
您会犯一些错误。
b1 = window.button
Just follow this code it will run as expected
You made several mistakes in it.
b1 = window.button