Naseem Tomkinson
Naseem Tomkinson

Reputation: 119

How would i get rid of a frame, whats placed on a frame when wanting to display another frame?

As you can see by the code, on the MainWindow it should display a blue and a green box, one ontop of the other and then the SecondMainWindow should have a red and yollow box displaying on ontop of the other. By running the code you can see that from the login window to the MainWindow all the frames remain as they should due to them only being on the MainWindowand the SecondMainWindow However the problem persists when you go from the MainWindow to the SecondMainWindow where the fames are getting mixed up with which window thier on. I've set the background to MainWindow and SecondMainWindow to different colours to further develop an understanding on what frame should be shown.

import tkinter as tk
class start(tk.Tk):
  def __init__(self):
     tk.Tk.__init__(self)
     self.frame = login()
     self.frame.grid()

  def change(self, frame):
    self.frame.grid_forget()
    self.frame = frame()
    self.frame.grid()


class login(tk.Frame):
  def __init__(self, master=None, **kwargs):
    tk.Frame.__init__(self, master, **kwargs)
    self.master.geometry('300x300')
    self.master.configure(bg='orange')

    button1 = tk.Button(self.master, text='New Frame', command = self.ChangeFrame).grid(row=0,column=0)

  def ChangeFrame(self):
    self.master.change(MainWindow)


class MainWindow (tk.Frame):
  def __init__(self, master=None, **kwargs):
    tk.Frame.__init__(self, master, **kwargs)
    self.master.geometry('300x300')
    self.master.configure(bg='white')

    self.master.grid_rowconfigure(0, weight=2)
    self.master.grid_rowconfigure(1, weight=3)

    self.master.grid_columnconfigure(0, weight=1)

    Frame1 = tk.Frame(self.master, background='blue', bd=1, relief='sunken')
    Frame1.grid(row=0,column=0,padx=10,pady=10,sticky='nsew')

    Frame2 = tk.Frame(self.master, background='green', bd=1, relief='sunken')
    Frame2.grid(row=1,column=0,padx=10,pady=10,sticky='nsew')

    button1 = tk.Button(Frame1, text='New Frame', command = self.ChangeFrame).grid(row=0,column=0)

  def ChangeFrame(self):
    self.master.change(SecondMainWindow)

class SecondMainWindow (tk.Frame):
  def __init__(self, master=None, **kwargs):
    tk.Frame.__init__(self, master, **kwargs)
    self.master.geometry('300x300')
    self.master.configure(bg='grey')

    self.master.grid_rowconfigure(0, weight=1)
    self.master.grid_rowconfigure(1, weight=1)

    self.master.grid_columnconfigure(0, weight=1)
    self.master.grid_columnconfigure(1, weight=1)

    Frame3 = tk.Frame(self.master, background='yellow', bd=1, relief='sunken')
    Frame3.grid(row=1,column=1,padx=10,pady=10,sticky='nsew')

    Frame4 = tk.Frame(self.master, background='red', bd=1, relief='sunken')
    Frame4.grid(row=0,column=1,padx=10,pady=10,sticky='nsew')

    button1 = tk.Button(Frame4, text='New Frame', command = self.ChangeFrame).grid(row=0,column=0)

  def ChangeFrame(self):
    self.master.change(MainWindow)    

app = start()
app.mainloop()

With only self being used enter image description here

With only self.master being used enter image description here

As you can see when using, just self it alters with the layout, how would i fix this?

Upvotes: 1

Views: 48

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

You are placing widgets for every window in self.master but the code that you copied from was designed so that each window is an independent object. All of the widgets in a single window must be children of that window.

In short, instead of this:

class SecondMainWindow (tk.Frame):
    def __init__(self, master=None, **kwargs):
        ...
        Frame3 = tk.Frame(self.master, ...)
        Frame4 = tk.Frame(self.master, ...)
        ...

You should be doing this:

class SecondMainWindow (tk.Frame):
    def __init__(self, master=None, **kwargs):
        ...
        Frame3 = tk.Frame(self, ...)
        Frame4 = tk.Frame(self, ...)
        ...

If you want every window to put widgets directly in the root window, there's no reason for you to create classes that inherit from tk.Frame.

Upvotes: 1

Related Questions