为什么我的Tkinter的部件得到存储为无? [英] Why do my Tkinter widgets get stored as None?
本文介绍了为什么我的Tkinter的部件得到存储为无?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我把我的按钮到一个数组,但是当我打电话给他们,他们不存在。如果我打印出数组我得到:
{0:无,1:无,2:无,3:无,4:无,5:无,6:无,7:没有,...}
我不知道我做错了。
从Tkinter的进口*高清的main():
通过如果__name__ =='__main__':
主要()B = {}应用= TK()
app.grid()F =帧(应用程序,BG =橙色,宽度= 500,高度= 500)
f.pack(边=底部,展开= 1)
DEF颜色(X):
B〔X] .configure(BG =红)#错误NoneType'对象有没有属性'配置'
打印(二)#0:无,1:无,2:无,3:无,4:无,5:....等
高清genABC():
对于在范围r(3):
对中的c的范围(10):
如果(C +(R * 10)→25):
打破
打印(C +(R * 10))
B〔C +(R * 10)] =按钮(F,文本= CHR(97 + C +(R * 10)),命令=拉姆达一个= C +(R * 10):颜色(a)中,边框宽度= 1,宽度= 5,BG =白)。格(行= R,列= C)genABC()
app.mainloop()
解决方案
电网
, 包
和 地方
一>每一个部件的Tkinter方法就地操作的 的并始终返回无
。这意味着,当你创建一个小部件,你不能叫他们在同一行。相反,他们应该叫下面的一行:
部件= ...
widget.grid(...)小部件= ...
widget.pack(...)小部件= ...
widget.place(...)
所以,在你的code,这将是:
B [C +(R * 10)] =按钮(F,文本= CHR(97 + C +(R * 10)),命令=拉姆达A = C +(R * 10 ):颜色(a)中,边框宽度= 1,宽度= 5,BG =白色)
B〔C +(R * 10)]。电网(行= R,列= C)
I'm putting my buttons into an array but when I call them they are not there. If I print out the array I get:
{0: None, 1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, ...}
I just don't know what I am doing wrong.
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()
解决方案
The grid
, pack
, and place
methods of every Tkinter widget operate in-place and always return None
. This means that you cannot call them on the same line as you create a widget. Instead, they should be called on the line below:
widget = ...
widget.grid(...)
widget = ...
widget.pack(...)
widget = ...
widget.place(...)
So, in your code, it would be:
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)
这篇关于为什么我的Tkinter的部件得到存储为无?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文