Home > Mobile >  Tkinter Get Text entry in Notebook page
Tkinter Get Text entry in Notebook page

Time:03-24

I have created a notebook and added a frame to it:

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

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

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

So i have a function layout1, the first page of the notebook, i added to it a Text:

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)

And next i have my import_entry function:

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

I can't get the entry because of accessibilty of variables in function. So, how can i get it?

CodePudding user response:

Here is an example of how you should structure your app with a 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()

  • Related