gpt4 book ai didi

python - 模式匹配 Tkinter 子小部件 (winfo_children) 以确定类型

转载 作者:行者123 更新时间:2023-12-03 23:15:09 27 4
gpt4 key购买 nike

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

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()

我遇到了 3 个问题
  • 虽然我可以找出子部件类型,但我似乎无法在模式匹配中使用它。打印出下面的 wlist[0] 与 shell 输出不同?

  • 例如:

    >> 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
  • 我想由于上面的打印输出和 shell 输出之间的差异,我的模式匹配不能工作?

  • 例如
    >> re.search(r'Entry',wlist[0])
    << No output >>
  • 假设一个人能够通过模式匹配确定一个子部件确实是一个 Entry 部件,你将如何让部件对象本身执行 delete 方法调用?

  • 例如:
    ## 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)

    最佳答案

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

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

    您还可以简单地比较对象类型,就像与任何其他 python 对象一样:
    >>> isinstance(wlist[0], Tkinter.Entry)
    True

    由于 winfo_children 的结果是一个小部件列表,您可以遍历它们并清除所有入口小部件,如下所示:
    for widget in root.winfo_children():
    if isinstance(widget, Tkinter.Entry):
    widget.delete(0, "end")

    关于python - 模式匹配 Tkinter 子小部件 (winfo_children) 以确定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34667710/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com