簡體   English   中英

在tkinter中使用window按鈕時出錯

[英]Error when using window button in 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'

我不確定如何解決這個問題。

我相信這就是答案:

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 = Button() b1.pack() )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM