-1

I have looked at all the related questions to this but I can't seem to find an answer to my exact issue. I am using this code: Switch between two frames in tkinter.

My main Frame is 800x600, it has to be this size and only ran at this size. I know it's improper, but it will never run on anything other that what it is designed for.

My issue is my start frame is 800x600 and it is the "Main" and the basis of all the other frames. I have two other frames that are not 800x600 because they need to be able to see some of the data on the "Main" frame. This frame is 800x232 and needs to be placed at the bottom of main frame. How do I control the other frames size and location based off of the code that is linked?

martineau
  • 119,623
  • 25
  • 170
  • 301
mScientist
  • 27
  • 7
  • All of the options for laying out widgets are well documented. Have you done any research and tried to solve the problem before asking this question? Start by creating a [mcve]. – Bryan Oakley Jul 21 '19 at 23:23
  • `Frames` in tkinter are containers, so a common way to do what you want is to nest one or more inside another. It's hard to be more concrete without seeing your code or some kind of graphic illustrating what you want to do. – martineau Jul 21 '19 at 23:23
  • set `width=` and `height=` and `pack()`/`grid()` frames one below another in the same window. – furas Jul 21 '19 at 23:31
  • The link I put on here is the code I am using. Sorry if I wasn't clear on that. I'm trying to get it to work the way i need it to before I waste my time trying to learn python. Yes, I've done research (as I stated that I have looked at all related questions, there is always one who asks that question instead of helping) I can change from one frame to another like the demo program, but cant get the width and height settings to work. – mScientist Jul 21 '19 at 23:46
  • there is more then one who ask questions to help. We all ask to help. – furas Jul 21 '19 at 23:49
  • so do you want to switch frames in one window and only one has to be visible - or you want to put two frames - one below another - and both should be visible ? – furas Jul 21 '19 at 23:52
  • both should be visible. The second one when called should appear at the bottom of the one that called it so that the user can still data that will be presented at the top of the one that does the calling. – mScientist Jul 21 '19 at 23:58

1 Answers1

0

EDIT: for first version probably you could use PannedWindow


I wasn't sure which version you need

enter image description here

or

enter image description here

First version uses pack(), pack_forget() to add and remove bottom frame below main frame.

import tkinter as tk

root = tk.Tk()

# - main frame -

f1 = tk.Frame(root, width=800, height=600, bg='green')
f1.pack_propagate(False) # dont change size
f1.pack() # visible

label1 = tk.Label(f1, text='800x600')
label1.place(relx=0.5, rely=0.5, anchor='c')

button1 = tk.Button(f1, text="Show")
button1.place(relx=1, rely=1, anchor='se')

# - bottom frame -

f2 = tk.Frame(root, width=800, height=230, bg='red')
f2.pack_propagate(False) # dont change size
#f2.pack() # hidden 

label2 = tk.Label(f2, text='800x230')
label2.place(relx=0.5, rely=0.5, anchor='c')

button2 = tk.Button(f2, text="Close", command=f2.pack_forget) # function to hide
button2.place(relx=1, rely=1, anchor='se')

# - assign function from bottom-frame to button in main-frame

button1['command'] = f2.pack # function to show

root.mainloop()   

Second method use place() to put bottom frame on top of main frame. It uses place_forget() to remove it.

import tkinter as tk

# --- main ---

root = tk.Tk()
root.geometry('800x600')

# - main frame -

f1 = tk.Frame(root, width=800, height=600, bg='green')
f1.place(x=0, y=0) # visible

label1 = tk.Label(f1, text='800x600')
label1.place(relx=0.5, rely=0.5, anchor='c')

button1 = tk.Button(f1, text="Show") #, command=lambda:f2.place(x=800, y=600, anchor='se'))
button1.place(relx=1, rely=1, anchor='se')

# - bottom frame -

f2 = tk.Frame(root, width=800, height=230, bg='red')
#f2.place(x=800, y=600, anchor='se') # hidden

label2 = tk.Label(f2, text='800x230')
label2.place(relx=0.5, rely=0.5, anchor='c')

button2 = tk.Button(f2, text="Close", command=f2.place_forget)
button2.place(relx=1, rely=1, anchor='se')

# - assign function from bottom-frame to button in main-frame

button1['command'] = lambda:f2.place(x=800, y=600, anchor='se')

root.mainloop()   

Inside frames I use place() but you can use pack() or grid()


EDIT: for second version I made code with classes - and I can show bottom frame and top frame on top of main frame.

import tkinter as tk

class MainFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self['bg'] = 'green'
        self['width'] = 800
        self['height'] = 600

        self.label = tk.Label(self, text='800x600')
        self.label.place(relx=0.5, rely=0.5, anchor='c')

        self.button1 = tk.Button(self, text="Show")
        self.button1.place(relx=1, rely=1, anchor='se')

        self.button2 = tk.Button(self, text="Show")
        self.button2.place(relx=1, rely=0, anchor='ne')

    def show(self):
        self.place(x=0, y=0, anchor='nw')

    #def hide(self):
    #    self.place_forget()


class BottomFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self['bg'] = 'red'
        self['width'] = 800
        self['height'] = 230

        self.label = tk.Label(self, text='800x230')
        self.label.place(relx=0.5, rely=0.5, anchor='c')

        self.button = tk.Button(self, text="Hide", command=self.hide)
        self.button.place(relx=1, rely=1, anchor='se')

    def show(self):
        self.place(relx=0, rely=1, anchor='sw')

    def hide(self):
        self.place_forget()


class TopFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self['bg'] = 'blue'
        self['width'] = 800
        self['height'] = 230

        self.label = tk.Label(self, text='800x230')
        self.label.place(relx=0.5, rely=0.5, anchor='c')

        self.button = tk.Button(self, text="Hide", command=self.hide)
        self.button.place(relx=1, rely=0, anchor='ne')

    def show(self):
        self.place(relx=0, rely=0, anchor='nw')

    def hide(self):
        self.place_forget()

# --- main ---

root = tk.Tk()
root.geometry('800x600')

f1 = MainFrame()
f1.show()

f2 = BottomFrame()
#f2.show() # hiddem

f3 = TopFrame()
#f3.show() # hiddem

f1.button1['command'] = f2.show
f1.button2['command'] = f3.show

root.mainloop()   
furas
  • 134,197
  • 12
  • 106
  • 148