Vrajesh Doshi
Vrajesh Doshi

Reputation: 764

Setting Frame width and height

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

Upvotes: 0

Views: 5216

Answers (1)

furas
furas

Reputation: 142631

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

Upvotes: 2

Related Questions