在TKINTER中使用窗口按钮时出错

发布于 2025-01-25 12:25:31 字数 657 浏览 2 评论 0原文

这是我的调试代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫思念不要吵 2025-02-01 12:25:31

我相信这是答案:

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

I believe that this is the answer:

from tkinter import *
window = Tk()
b1 = Button(window,text="Dark",command=window.configure(bg='black'))
b1.pack()
window.mainloop()

Your mistake was that your input was b1 = window.button() instead of b1 = Button()

Moreover, you forgot to add in a b1.pack() after the b1 = Button()

汹涌人海 2025-02-01 12:25:31

只需遵循此代码,它将按照预期运行,

import tkinter as tk
window = tk.Tk()
b1 = tk.Button(window,text="Dark", command=lambda: window.configure(bg='black'))
b1.pack()
window.mainloop()

您会犯一些错误。

  1. 您使用b1 = window.button
  2. 您无法在命令中传递函数,如果您需要通过它传递参数,则不能直接调用它,您需要通过 lambda < /strong>函数如上所述。
  3. 您忘记了 pack 显示它所需的窗口中的按钮。

Just follow this code it will run as expected

import tkinter as tk
window = tk.Tk()
b1 = tk.Button(window,text="Dark", command=lambda: window.configure(bg='black'))
b1.pack()
window.mainloop()

You made several mistakes in it.

  1. You use b1 = window.button
  2. You can't pass function in command like that, you cannot call it directly in command if you need to pass arguments with it you need to pass a lambda function like above.
  3. You forgot to pack the button in the window that is needed to display it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文