Search code examples
pythonpython-3.xtkinterframetkinter-entry

Tkinter entry widget in different Frames


I am starting to learn Python and the GUI, I am using the 3.7 version. My issue is with the entry widget. I am just checking how the diferents widget are working with diferents frames and is not working like I thought it will work. So I want to do an app with two differents pages or more but I starting with two, and I want to change between them, every page will have differents widgets. In this case when I am using the call to the Entry class in the second page or frame, the entry block is in the main page as well, so I just want the entry block in one page or frame. As you can see I using .pack() with entry I would like to use .grid() but python show me an error when I tried to do it.

Thank you for your help.

import tkinter as tk

LARGE_FONT = ("Verdana", 12)


class mainapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # self.geometry("800x600")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, NewData):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.configure(background='white')
        labeltop = tk.Label(self, text="Hello\n"
                                       "Please Select a option:",
                            bg="white", fg="green", height=4, width=30, font=LARGE_FONT)
        labeltop.grid(row=0, column=0, pady=10, padx=10, columnspan=2)

        self.logo = tk.PhotoImage(file="logo.gif")
        logolabel=tk.Label(self, image=self.logo)
        logolabel.grid(row=0, column=3)

        button1 = tk.Button(self, text="Option1", fg="White", bg="Green", height=6, width=30,
                            command=lambda: controller.show_frame(NewData))
        button1.grid(row=1, column=0, padx=1, pady=10)


class NewData(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.configure(background='white')
        label = tk.Label(self, text="Option1", font=LARGE_FONT)
        label.grid(pady=10, padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        E1 = tk.Entry()
        E1.pack()

        button1.grid()

app = mainapp()
app.mainloop()

Solution

  • When you create widgets, you must tell them where they belong. When you do tk.Entry() you aren't doing that so it defaults to going into the root window.

    If you want the entry to be inside a specific page, you need to give that page or one of its children as the master for the entry: tk.Entry(self).