dildeolupbiten
dildeolupbiten

Reputation: 1342

Python/Tkinter: How to display a blank bottom frame when the application is run first?

When the application is run first, the last frame's widgets are displayed on the screen. What i wanted to do is, displaying the related frames when the user clicks their buttons. So, i want to display a blank frame with the top buttons. In order to do that, what should i do? (I removed the button functions, because they are not related to the question.) Thanks in advance.

import tkinter as tk


class TopFrame(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid(row=0, column=0, sticky="nsew")

        self.BottomFrame = tk.Frame(master=master)
        self.BottomFrame.grid(row=1, column=0, sticky="nsew")

        self.f1 = tk.Frame(master=self.BottomFrame)
        self.f2 = tk.Frame(master=self.BottomFrame)
        self.f3 = tk.Frame(master=self.BottomFrame)

        for f in (self.f1, self.f2, self.f3):
            f.grid(row=0, column=0, sticky="nsew")

        self.b1 = tk.Button(master=self, text="Add Words")
        self.b2 = tk.Button(master=self, text="Add From File")
        self.b3 = tk.Button(master=self, text="Change Words")

        self.add_button = tk.Button(master=self.f1, text="Add")
        self.open_button = tk.Button(master=self.f2, text="Open File")
        self.change_button = tk.Button(master=self.f3, text="Change")

        self.l1 = tk.Label(master=self.f1, text="English")
        self.l2 = tk.Label(master=self.f1, text="Turkish")
        self.l3 = tk.Label(master=self.f3, text="Old word")
        self.l4 = tk.Label(master=self.f3, text="New word")

        self.e1 = tk.Entry(master=self.f1)
        self.e2 = tk.Entry(master=self.f1)
        self.e3 = tk.Entry(master=self.f3)
        self.e4 = tk.Entry(master=self.f3)

        self.configure_buttons()
        self.configure_labels()
        self.configure_entries()

    def configure_buttons(self):
        self.b1.grid(row=0, column=0)
        self.b1.configure(command=lambda: self.f1.tkraise())
        self.b2.grid(row=0, column=1)
        self.b2.configure(command=lambda: self.f2.tkraise())
        self.b3.grid(row=0, column=2)
        self.b3.configure(command=lambda: self.f3.tkraise())

        self.add_button.grid(row=2, columnspan=2)
        #self.add_button.configure(command=self.add_word)
        self.open_button.pack(side="top")
        #self.open_button.configure(command=self.add_from_file)
        self.change_button.grid(row=2, columnspan=2)

    def configure_labels(self):
        self.l1.grid(row=0, column=0)
        self.l2.grid(row=0, column=1)
        self.l3.grid(row=0, column=0)
        self.l4.grid(row=0, column=1)

    def configure_entries(self):
        self.e1.grid(row=1, column=0)
        self.e2.grid(row=1, column=1)
        self.e3.grid(row=1, column=0)
        self.e4.grid(row=1, column=1)


if __name__ == "__main__":
    root = tk.Tk()
    example = TopFrame(master=root)
    example.mainloop()

Upvotes: 0

Views: 564

Answers (1)

Aran-Fey
Aran-Fey

Reputation: 43256

Instead of having 3 widgets in the same location, it's better to have only the one you need.

First, get rid of this code:

for f in (self.f1, self.f2, self.f3):
    f.grid(row=0, column=0, sticky="nsew")

Now the frame will start in a blank state.

Then, instead of calling .tkraise() on the frames, we will remove the current frame (if any) and add another one in its place. So

self.b1.configure(command=lambda: self.f1.tkraise())
self.b2.configure(command=lambda: self.f2.tkraise())
self.b3.configure(command=lambda: self.f3.tkraise())

becomes:

self.b1.configure(command=lambda: self._activate(self.f1))
self.b2.configure(command=lambda: self._activate(self.f2))
self.b3.configure(command=lambda: self._activate(self.f3))

with

def _activate(self, frame):
    # remove the current frame
    for child in self.BottomFrame.winfo_children():
        child.grid_forget()

    # add the new frame in its place
    frame.grid(row=0, column=0, sticky='nsew')

Upvotes: 1

Related Questions