【问题标题】:Is there a way to get frame childs reference?有没有办法获得框架孩子的参考?
【发布时间】:2020-05-01 08:03:00
【问题描述】:

我编写了一个运行良好的 GUI,它应该从文件中获取一些信息并向我显示条目,并且根据存在的信息行数,它会生成相同数量的条目。

此代码的问题是:我想让我更改一些值并将它们保存回文件中,但我不知道如何从框架上的所有条目中获取该 entry.get()。如果我尝试获取有关它的参考,我将只返回打印的最后一个条目更改。

def DataShow(self):
    # Import dictionary after the settings file was read
    from res.lib.Settings import app_settings

    # Make local frame
    frame = ScrolledFrameVertical(self, width=353)
    frame.pack(expand=True, fill="y", side="top")

    Button(frame, text="Save", command=lambda:save()).pack(fill="x", side="top")

    # Create entrys base on dictionary data and info
    for data, info in app_settings.items():
        d = Settings.Entry(frame, data, 20, info, 31)

    def save():
        print(d.get())

看起来像这样:

【问题讨论】:

  • 您可以尝试在创建条目小部件时添加textvariable 以保存条目值,并在您想查看here 以获取更多信息时将其取回
  • 您的问题是获取一个小部件的所有孩子? (在这种情况下是框架)
  • 来自Settings.Entry(frame, data, 20, info, 31) 我只返回条目。它们是生成的,我不知道如何获取它们的值...我仍然尝试使用 textvariable
  • 您可以使用datainfo 作为键将Entry 的引用保存在字典中吗?

标签: python python-3.x tkinter frame tkinter-entry


【解决方案1】:

您可以使用.winfo_children() 获取小部件的所有子级。

def save():
    for child in frame.winfo_children():
        print(child.get())

【讨论】:

  • 这不是很容易理解,但我玩过它,.winfo_children() 是我的解决方案。非常感谢。
猜你喜欢
  • 2016-09-10
  • 2012-02-07
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多