问题描述:
tkinter 定义控件之后再次修改控件的属性, 报错:TypeError: 'NoneType' object does not support item assignment
import tkinter as tk
window = tk.Tk()
window.title("win")
window.geometry("200x200")
imglabel = tk.Button(window, text="点击修改").place(x=10, y=10)
imglabel["text"] = "修改成功"
原因分析:
tkinter中两种定义方式,效果不同。
参考:【tkinter】填坑 解决Entry、Label不能修改text、image等属性的问题
imglabel1 = tk.Button(window, text="点击修改1").place(x=10, y=10)
print(type(imglabel1))
#<class 'NoneType'>
imglabel2 = tk.Button(window, text="点击修改2")
imglabel2.place(x=10, y=80)
print(type(imglabel2))
#<class 'tkinter.Button'>
解决方案:
先定义控件,再设置布局
imglabel2 = tk.Button(window, text="点击修改2")
imglabel2.place(x=10, y=80)