0

The code below is created using tkinter in Python. It renders a frame on which it provides link to next frame and on next frame it provides link to previous frame. I am not getting how to set width and height of the frame. Please help.

import tkinter as tk
LARGE_FONT = ("Verdana", 12)
class ImgComp(tk.Tk):

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

        tk.Tk.__init__(self, *args, **kwargs)
        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, PageOne, PageTwo):

            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()

def qf(param):
    print(param)

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)
        button1 = tk.Button(self, text="Visit Page 1",
                           command=lambda: controller.show_frame(PageOne))
        button1.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page One", font=LARGE_FONT)
        label.pack(pady=10,padx=10)
        button1 = tk.Button(self, text="Back to Home",
                            command=lambda:controller.show_frame(StartPage))
        button1.pack()   

app = ImgComp()
app.mainloop()
6
  • 2
    Frame( width=..., height=...) but it will change size if elements inside are smaller or if element outside is bigger and you use fill, resize - Stop a Tkinter frame from resizing according to widget sizes Commented Dec 15, 2017 at 10:05
  • I removed the properties fill, expand from the code. On which Line of my code should I write Frame(width = 500, height = 300) Commented Dec 15, 2017 at 10:12
  • 1
    I don't see anywhere in your code where you're trying to set the width and height. Do you want each frame to be a different size? Commented Dec 15, 2017 at 12:27
  • If possible guide me for both. To keep all frames of same size and frames of different size. Usually I use to set width and height while creating Frame object but in this code I am not getting where to initialize those properties Commented Dec 15, 2017 at 12:57
  • 1
    Are you certain that you want to resize frames and not the actual window? Commented Dec 15, 2017 at 14:45

1 Answer 1

2

To change size of container you cat set width and height
and you have to use grid_propagate(False) to keep it.

    container = tk.Frame(self, width=500, height=250)
    #container['width'] = 500
    #container['height'] = 250
    container.grid_propagate(False)

enter image description here

If you add bacground to all Pages then you will see they use full size

frame = F(container, self)
frame['bg'] = 'red'

enter image description here

And now you can organize elements inside to use all area.

For example you can put button on the bottom using pack(side='bottom')

   button1 = tk.Button(self, text="Visit Page 1", ...)
    button1.pack(side='bottom')

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.