【问题标题】:Getting all of the Entry text content from a frame [duplicate]从框架中获取所有条目文本内容[重复]
【发布时间】:2018-08-31 20:21:21
【问题描述】:

如果我有一个名为变量table 的框架,它在该框架内有多个条目小部件,是否有办法获取所有条目小部件的文本内容?

【问题讨论】:

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


【解决方案1】:

是的。在下面的示例中,当单击按钮时,检查父窗口小部件中的每个子窗口是否相等,然后打印它们的内容:

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk


def get_all_entry_widgets_text_content(parent_widget):
    children_widgets = parent_widget.winfo_children()
    for child_widget in children_widgets:
        if child_widget.winfo_class() == 'Entry':
            print(child_widget.get())


def main():
    root = tk.Tk()
    table = tk.Frame(root)
    for _ in range(3):
        tk.Entry(table).pack()
    tk.Button(table, text="Get",
        command=lambda w=table: get_all_entry_widgets_text_content(w)).pack()
    table.pack()
    tk.mainloop()


if __name__ == '__main__':
    main()

根据条目的实例化方式,可以有一个更好的 get_all_entry_widgets_text_content 方法,但是,无论如何,这个方法应该适用于所有直接子代。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2015-01-28
    • 1970-01-01
    • 2014-11-01
    • 2018-07-06
    • 2017-08-20
    相关资源
    最近更新 更多