【发布时间】:2020-05-02 18:40:40
【问题描述】:
欢迎 我想让棋盘像象棋一样。单击此按钮后,应使用“命令”方法更改您的背景。我尝试输入我的代码 StringVar()、.config、全局变量和 lambda。如果有解决此问题的链接,我会很高兴。 (这是我在这个网站上的第一个问题,对一切感到抱歉)
from tkinter import *
class App():
"""docstring for App"""
root=Tk(className="application 4")
x, y = 0, 0
tab = []
def __init__(self, x, y):
App.x = x
App.y = y
App.root.geometry("800x600")
App.root.resizable(height = FALSE, width = FALSE)
for i in range(App.x):
for j in range(App.y):
App.tab.append(Button(App.root, width=1, bg="white", command=lambda: self.changeBG(self)).grid(row=i, column=j))
App.root.mainloop()
def changeBG(self, btn):
self.config(bg = "red")
application = App(10, 10)
错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\S\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\S\Desktop\Spanish number project\clicked.py", line 16, in <lambda>
App.tab.append(Button(App.root, width=1, bg="white", command=lambda: self.changeBG(self)).grid(row=i, column=j))
File "C:\Users\S\Desktop\Spanish number project\clicked.py", line 21, in changeBG
self.config(bg = "red")
AttributeError: 'App' object has no attribute 'config'
[Finished in 6.4s]
【问题讨论】: