João Sousa
João Sousa

Reputation: 41

Tkinter frame tkraise Entry bug

I need to create a multiple window GUI and I found a video (https://youtu.be/jBUpjijYtCk) that shows the functionality of tk raise with multiple frames. However I'm facing a bug when I press the button "Visit Page 1" all labels and even "ADICIONAR" button dissapears, but not the Entries, what should I do for it to work properly?

The code I'm writing about happens on the StartPage class.

The Client class isn't important, its just a class I created to store data.

from tkinter import *
from client import Client
LARGE_FONT = ("Verdana", 16)
MEDIUM_FONT = ("VERDANA", 12)

clients = []

class Sea(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        self.geometry("800x500")
        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, PageOne):
            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(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        label = Label(self, text="Adicionar cliente", font=LARGE_FONT)
        label.place(x=50, y=100)

        label = Label(self, text="Nome", font=MEDIUM_FONT)
        label.place(x=10, y=160)
        self.name_entry = Entry(width=30)
        self.name_entry.place(x=100, y=162)

        label = Label(self, text="GSM", font=MEDIUM_FONT)
        label.place(x=10, y=200)
        self.gsm_entry = Entry(width=30)
        self.gsm_entry.place(x=100, y=202)

        label = Label(self, text="Email", font=MEDIUM_FONT)
        label.place(x=10, y=240)
        self.email_entry = Entry(width=30)
        self.email_entry.place(x=100, y=242)

        label = Label(self, text="Local", font=MEDIUM_FONT)
        label.place(x=10, y=280)
        self.local_entry = Entry(width=30)
        self.local_entry.place(x=100, y=282)

        label = Label(self, text="Produto", font=MEDIUM_FONT)
        label.place(x=10, y=320)
        self.product_entry = Entry(width=30)
        self.product_entry.place(x=100, y=322)

        add_button = Button(self, text="ADICIONAR", command=self.adicionar)
        add_button.place(x=100, y=360)

        self.button = Button(self, text="Visit Page 1", 
                        command=lambda:controller.show_frame(PageOne))
        self.button.place(x=500, y=20)

    def adicionar(self):
        if(self.name_entry.get()=="" or self.gsm_entry.get()=="" or self.local_entry.get()=="" or self.product_entry.get()==""):
            return

        clients.append(Client(self.name_entry.get(), self.gsm_entry.get(), self.local_entry.get(), self.gsm_entry.get()))
        print(clients)

class PageOne(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="Page One", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button = Button(self, text="Start Page", 
                        command=lambda:controller.show_frame(StartPage))
        button.pack()

app = Sea()
app.mainloop()

Upvotes: 0

Views: 196

Answers (2)

sciroccorics
sciroccorics

Reputation: 2427

Your Entry widget are not created as child of your StartPage Frame. So simply add (self,...) at the beginning of each Entry command

Upvotes: 2

toti08
toti08

Reputation: 2464

You didn't pass the Frame instance to your Entry classes, so you're not putting them into your frame. Pass the self parameter to each of your Entry instance and it will work:

self.name_entry = Entry(self, width=30)

Do that for all entries.

Upvotes: 2

Related Questions