我对 Python 还很陌生,刚刚开始玩 tkinter。运行下面的代码,我得到一个属性错误but1.pack()
(NoneType
对象没有属性pack
)。但据我所知,这个错误对窗口的功能没有影响,它仍然pack
是按钮。该窗口仍然出现,并且所有按钮都按预期运行。
搜索我可以看到其他人有这个错误,但给出的答案都没有解决我的问题。希望你能提供帮助。
编码:
import tkinter
import ctypes
lst=[]
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
def closewindow():
window.destroy()
def btn1():
lst.append("Button1")
def btn2():
lst.append("Button2")
window = tkinter.Tk()
size = str(screensize[0])+'x'+str(screensize[1])
window.geometry(size)
but1 = tkinter.Button(window, text="Button1", command=btn1).grid(column = 1, row = 1)
but2 = tkinter.Button(window, text="Button2", command=btn2).grid(column = 2, row = 1)
ext = tkinter.Button(window, text="Stop", command=closewindow).grid(column = 3, row = 1)
but1.pack()
but2.pack()
ext.pack()
window.mainloop()
回调;
Traceback (most recent call last):
File "C:\Python33\temp.py", line 59, in <module>
but1.pack()
AttributeError: 'NoneType' object has no attribute 'pack'