python tkinter 打包 [英] python tkinter packing

查看:56
本文介绍了python tkinter 打包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么当你有一个简单的小部件作为一行代码 A 工作

Could someone please explain why when you have an plain widget as one line Code A works

Entry(root, width=10).pack(side=LEFT,anchor=W)

但是当你给它命名或给它附加一个命令时,代码 A 不再起作用并给你错误消息 B

but when you name it or attach a command to it, Code A no longer works and gives you Error Message B

self.my_entry = Entry(root, width=10).pack(side=LEFT,anchor=W)

并且您必须使用单独的行打包?

and you must pack using a seperate line?

self.my_entry = Entry(root, width=10)
self.my_entry.pack(side=LEFT,anchor=W)

代码 A

self.my_entry.get()

错误信息 B

AttributeError: 'NoneType' object has no attribute 'get'

推荐答案

pack 方法返回 None.所以

self.my_label = Label(root, text="My Label").pack(side=LEFT,anchor=W)

self.my_label 设置为 None.这就是使用 self.my_label 的进一步命令不再起作用的原因.

sets self.my_label to None. That is why further commands using self.my_label no longer work.

您已找到解决方案;在单独的行上调用 pack:

You've found the solution; call pack on a separate line:

self.my_label = Label(root, text="My Label")
self.my_label.pack(side=LEFT,anchor=W)

这篇关于python tkinter 打包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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