简体   繁体   中英

Error when using window button in tkinter

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM