use*_*820 6 python dictionary tkinter button
我把按钮放到一个阵列中但是当我打电话给他们时他们不在那里.如果我打印出阵列,我得到:
{0: None, 1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, ...}
Run Code Online (Sandbox Code Playgroud)
我只是不知道我做错了什么.
from tkinter import *
def main():
pass
if __name__ == '__main__':
main()
b={}
app = Tk()
app.grid()
f = Frame(app, bg = "orange", width = 500, height = 500)
f.pack(side=BOTTOM, expand = 1)
def color(x):
b[x].configure(bg="red") # Error 'NoneType' object has no attribute 'configure'
print(b) # 0: None, 1: None, 2: None, 3: None, 4: None, 5:.... ect
def genABC():
for r in range(3):
for c in range(10):
if (c+(r*10)>25):
break
print(c+(r*10))
b[c+(r*10)] = Button(f, text=chr(97+c+(r*10)), command=lambda a=c+(r*10): color(a), borderwidth=1,width=5,bg="white").grid(row=r,column=c)
genABC()
app.mainloop()
Run Code Online (Sandbox Code Playgroud)
iCo*_*dez 10
的grid,pack和place每一个部件Tkinter的方法操作就地并始终返回None.这意味着您无法在创建窗口小部件的同一行中调用它们.相反,它们应该在下面的行上调用:
widget = ...
widget.grid(...)
widget = ...
widget.pack(...)
widget = ...
widget.place(...)
Run Code Online (Sandbox Code Playgroud)
所以,在你的代码中,它将是:
b[c+(r*10)] = Button(f, text=chr(97+c+(r*10)), command=lambda a=c+(r*10): color(a), borderwidth=1,width=5,bg="white")
b[c+(r*10)].grid(row=r,column=c)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1910 次 |
| 最近记录: |