Tkinter 在笔记本页面中获取文本条目

Tkinter Get Text entry in Notebook page

我创建了一个笔记本并在其中添加了一个框架:

nb = ttk.Notebook(root, style="TNotebook")

page1 = ttk.Frame(nb, style='Frame1.TFrame')
layout1(page1)

nb.add(page1, text='Welcome')

所以我有一个功能 layout1,笔记本的第一页, 我给它添加了一个文本:

def layout1(page):
    
    entry = Text(page, width=20)
    entry.place(relx=0.03, rely=0.1, height=400)
    Button(page, text='EXECUTE', command=import_entry).place(relx=0.5, rely=0.6)

接下来我有我的 import_entry 功能:

def import_entry():
    result = entry.get()
    print(result)

由于函数中变量的可访问性,我无法获取该条目。那么,我怎样才能得到它呢?

下面是一个示例,说明您应该如何使用 class 构建您的应用程序:

import tkinter
import tkinter.ttk as ttk


class App(tkinter.Tk):
    def __init__(self):
        super().__init__()

        # assign on_closing method to window close event
        self.protocol("WM_DELETE_WINDOW", self.on_closing)

        self.title("Example App")
        self.geometry("600x500")

        self.button_1 = tkinter.Button(master=self, text="Test", command=self.button_event)
        self.button_1.pack(pady=10)

        # create more widgets ...

    def button_event(self, event):
        print("button pressed")

    def on_closing(self):
        # code that needs to happen when gets closed

        self.destroy()  # controlled closing of window with .destroy()


if __name__ == "__main__":
    app = App()
    app.mainloop()