模式匹配 Tkinter 子小部件 (winfo_children) 以确定类型 [英] Pattern matching Tkinter child widgets (winfo_children) to determine type

查看:100
本文介绍了模式匹配 Tkinter 子小部件 (winfo_children) 以确定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动清除父小部件中的所有 Entry 小部件.

导入Tkinter进口重新根=Tkinter.Tk()E1=Tkinter.Entry(root)E1.pack()E2=Tkinter.Entry(root)E2.pack()L1=Tkinter.Label(root,text='Label1')L1.pack()

我遇到了 3 个问题

  1. 虽然我可以找出子小部件类型,但我似乎无法在模式匹配中使用它.打印出下面的 wlist[0] 与 shell 输出不同?

例如:

>>wlist=root.winfo_children()>>列表[<Tkinter.Entry 实例在 0x00000000151911C8>,<Tkinter.Entry 实例在 0x00000000151BAD88>,<Tkinter.Label 实例在 0x00000000151B29C8>]>>wlist[0] # shell 输出<Tkinter.Entry 实例在 0x00000000151911C8>>>print wlist[0] # 这里的打印输出与上面的 shell 输出不同.353964488L

  1. 我认为由于打印输出和上面的 shell 输出,我的模式匹配不起作用?

例如

<代码>>>re.search(r'Entry',wlist[0])<<无输出 >>

  1. 假设一个人能够通过模式匹配确定一个子部件确实是一个 Entry 部件,你将如何让部件对象本身执行一个 delete 方法调用?

例如:

## 假设我有一个函数来清除条目## 如何将对象从 #2 中的模式匹配传递给这个函数?def clear_entry(objEntry):objEntry.delete(0,Tkinter.END)

解决方案

winfo_children() 返回的项目是一个小部件列表.Tkinter 小部件有一种方法可以告诉您底层小部件类:winfo_class.

<预><代码>>>>wlist[0].winfo_class()'入口'

您也可以简单地比较对象类型,就像与任何其他 Python 对象一样:

<预><代码>>>>isinstance(wlist[0], Tkinter.Entry)真的

由于 winfo_children 的结果是一个小部件列表,您可以遍历它们并清除所有入口小部件,如下所示:

 用于 root.winfo_children() 中的小部件:if isinstance(widget, Tkinter.Entry):widget.delete(0, "end")

I'm trying to automatically clear all Entry widgets in a parent widget.

import Tkinter
import re
root=Tkinter.Tk()

E1=Tkinter.Entry(root)
E1.pack()

E2=Tkinter.Entry(root)
E2.pack()

L1=Tkinter.Label(root,text='Label1')
L1.pack()

I'm running into 3 problems

  1. While I can find out the children widget type, I can't seem to be able to use it in a pattern match. Printing out the wlist[0] below is different from the shell output?

Eg:

>> wlist=root.winfo_children()
>> wlist
[<Tkinter.Entry instance at 0x00000000151911C8>,
 <Tkinter.Entry instance at 0x00000000151BAD88>,
 <Tkinter.Label instance at 0x00000000151B29C8>]

>> wlist[0]    # shell output
<Tkinter.Entry instance at 0x00000000151911C8>

>> print wlist[0]   # print output here is different vs shell output above
.353964488L

  1. I think due to the differences between the print output & shell output above, my pattern match can't work?

Eg

>> re.search(r'Entry',wlist[0])
    << No output >>

  1. Assuming one is able to determine via pattern match that a child widget is indeed an Entry widget, how would you get the widget object itself to perform a delete method call?

Eg:

## Assuming I have a function to to clear the entry
## How would I pass the object from the pattern match in #2 to this function?
def clear_entry(objEntry):
    objEntry.delete(0,Tkinter.END)

解决方案

The items returned from winfo_children() is a list of widgets. Tkinter widgets have a method to tell you the underlying widget class: winfo_class.

>>> wlist[0].winfo_class()
'Entry'

You can also simply compare the object type, like you can with any other python object:

>>> isinstance(wlist[0], Tkinter.Entry)
True

Since the result of winfo_children is a list of widgets, you can iterate over them and clear out all of the entry widgets like this:

for widget in root.winfo_children():
    if isinstance(widget, Tkinter.Entry):
        widget.delete(0, "end")

这篇关于模式匹配 Tkinter 子小部件 (winfo_children) 以确定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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