Python3.7 TypeError:“ NoneType”对象不支持项目分配

时间:2019-10-22 09:37:44

标签: python user-interface tkinter

我用tkinter模块做了一个按钮来处理事件,但是它不起作用,并且出现上述错误。我们应该如何解决?

我的环境正在使用Python版本3.7,并且我正在使用Windows10。编辑器正在使用AtomEditor。

#-*-coding: utf-8
from tkinter import *

def ok():
    b1["text"] = "accept"
def cancel():
    b2["text"] = "확인 됨"
def bu3():
    b3["text"] = "확인 됨"
def bu4():
    b4["text"] = "확인 됨"

window = Tk()
b1 = Button(window, text="버튼 1", command=ok).pack()
b2 = Button(window, text="버튼 2", command=cancel).pack()
b3 = Button(window, text="버튼 3", command=bu3).pack()
b4 = Button(window, text="버튼 4", command=bu4).pack()

window.mainloop()

2 个答案:

答案 0 :(得分:2)

您需要将每个按钮拆分为两个语句:

b1 = Button(window, text="버튼 1", command=ok)

b1.pack()

因为要将按钮分配给b1,而不是pack调用的结果(无)

答案 1 :(得分:0)

尝试:

b1 = Button(window, text="버튼 1", command=ok)
b2 = Button(window, text="버튼 2", command=cancel)
b3 = Button(window, text="버튼 3", command=bu3)
b4 = Button(window, text="버튼 4", command=bu4)
b1.pack()
b2.pack()
b3.pack()
b4.pack()

或更好:

b1 = Button(window, text="버튼 1", command=ok)
b2 = Button(window, text="버튼 2", command=cancel)
b3 = Button(window, text="버튼 3", command=bu3)
b4 = Button(window, text="버튼 4", command=bu4)
for c in sorted(window.children):
    window.children[c].pack()