tkinter 'NoneType' 对象没有属性 'pack'(仍然有效?) [英] tkinter 'NoneType' object has no attribute 'pack' (Still works?)

查看:97
本文介绍了tkinter 'NoneType' 对象没有属性 'pack'(仍然有效?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Python 还很陌生,刚刚开始使用 tkinter.运行下面的代码,我得到 but1.pack() 的属性错误(NoneType 对象没有属性 pack).但据我所知,这个错误对窗口的功能没有影响,它仍然pack按钮.窗口仍然出现,所有按钮都按预期运行.

I'm fairly new to Python and have just started to play around with tkinter. Running the below code I get an attribute error for but1.pack() (NoneType object has no attribute pack). But as far as I can tell this error is having no effect on the window's functionality, it is still packing the button. The window still appears and all the buttons behave as expected.

搜索我可以看到其他人也有这个错误,但给出的答案都没有解决我的问题.希望能帮到你.

Searching I can see other people have had this error, but none of the answers given solved my problem. Hoping you can help.

代码:

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'

推荐答案

当您收到诸如 'NoneType' object has no attribute 'X' 之类的错误时,这意味着您有一个其值的变量是 None,而您正在尝试执行 None.X().如果您使用的是 tkinter 或任何其他软件包,则无关紧要.所以,你必须问自己,为什么我的变量值为 None?"

When you get an error such as 'NoneType' object has no attribute 'X', that means you have a variable whose value is None, and you are trying to do None.X(). It doesn't matter if you're using tkinter or any other package. So, you have to ask yourself, "why does my variable have the value None?"

问题是这一行:

but1 = tkinter.Button(window, text="Button1", command=btn1).grid(column = 1, row = 1)

在python中,当你执行foo=x(...).y(...)时,foo将始终拥有最后调用的函数的值.在上面的例子中,but 将返回 .grid(column = 1, row = 1) 返回的值,而 grid 总是返回 <代码>无.因此,but1None,因此你得到 `'NoneType' 对象没有属性 'pack'".

In python, when you do foo=x(...).y(...), foo will always have the value of the last function called. In the case above, but will have the value returned by .grid(column = 1, row = 1), and grid always returns None. Thus, but1 is None, and thus you get `'NoneType' object has no attribute 'pack'".

因此,直接的解决方法是将您对 grid 的调用移动到单独的一行:

So, the immediate fix is to move your call to grid to a separate line:

but1 = tkinter.Button(window, text="Button1", command=btn1)
but1.grid(column = 1, row = 1)

这样,错误就会消失.

但是,您还有另一个问题.调用 grid 然后再调用 pack 不会做你认为它会做的事情.对于任何给定的小部件,您一次只能有一个几何管理器有效,并且 gridpack 都是几何管理器.如果你执行 but1.grid(...) 和之后的 but1.pack(...),任何调用 grid 的效果都会被扔掉,就好像你从来没有调用过 grid 一样.

However, you have another problem. Calling grid and then later calling pack isn't going to do what you think it's going to do. You can only have one geometry manager in effect at a time for any given widget, and both grid and pack are geometry managers. If you do but1.grid(...) and later but1.pack(...), any effect that calling grid had will be thrown away, as if you had never called grid in the first place.

您必须决定是要使用grid,还是要使用pack,并且对于根窗口中的所有小部件仅使用一种或另一种.

You have to decide whether you want to use grid, or whether you want to use pack, and use only one or the other for all widgets in your root window.

这篇关于tkinter 'NoneType' 对象没有属性 'pack'(仍然有效?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆