Bora Semiz
Bora Semiz

Reputation: 223

Arranging size of a frame in tkinter

I am making a simple toolbox for my app. I have used the class method, which inherits Frame as its super class. In my main file I import this class.

It will be a main window which all widgets will be in it. But there is a problem, here is the source code:

from tkinter import *

class ToolBox(Frame):
    def __init__(self, master=None,
                 width=100, height=300):
        Frame.__init__(self, master,
                       width=100, height=300)
        self.pack()
        Button(self, text="B").grid(row=0, sticky=(N,E,W,S))
        Button(self, text="B").grid(row=0, column=1, sticky=(N,E,W,S))
        Button(self, text="B").grid(row=1, column=0,sticky=(N,E,W,S))
        Button(self, text="B").grid(row=1, column=1, sticky=(N,E,W,S))
        Button(self, text="B").grid(row=2, column=0, sticky=(N,E,W,S))
        Button(self, text="B").grid(row=2, column=1, sticky=(N,E,W,S))

I import this in here:

from tkinter import *
import toolbox as tl

root = Tk()

frame = Frame(root, width=400, height=400)
frame.pack()
tl.ToolBox(frame).pack()

root.mainloop()

Main window, which is the root who has the frame, must be 400 in widht and height. But it appears in dimensions of my toolbox. I want the toolbox to be in the main window. How can I solve this?

Upvotes: 0

Views: 1904

Answers (1)

Kevin
Kevin

Reputation: 76194

You can force the root window to have specific dimensions using the geometry method.

root = Tk()
root.geometry("400x400")

If you would also like the buttons to stretch evenly to fill the whole root window, you need to do two things:

  1. Call rowconfigure and columnconfigure to set the weight of the root and each frame that is a parent of your buttons.
  2. specify the sticky parameter for every button and frame that is a child of your root.

Here's an example. I removed your frame Frame, since it didn't seem to be doing anything. Toolbox is already a frame, after all, and there's not much point putting a frame inside a frame.

from tkinter import *

class ToolBox(Frame):
    def __init__(self, master=None,
                 width=100, height=300):
        Frame.__init__(self, master,
                       width=width, height=height)
        for i in range(2):
            self.grid_columnconfigure(i, weight=1)
        for j in range(3):
            self.grid_rowconfigure(j, weight=1)

        Button(self, text="B").grid(row=0, sticky=(N,E,W,S))
        Button(self, text="B").grid(row=0, column=1, sticky=(N,E,W,S))
        Button(self, text="B").grid(row=1, column=0,sticky=(N,E,W,S))
        Button(self, text="B").grid(row=1, column=1, sticky=(N,E,W,S))
        Button(self, text="B").grid(row=2, column=0, sticky=(N,E,W,S))
        Button(self, text="B").grid(row=2, column=1, sticky=(N,E,W,S))

root = Tk()
root.geometry("400x400")

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

ToolBox(root).grid(sticky="news")

root.mainloop()

Now your root is properly sized, and your buttons stretch to fill it.

enter image description here

Upvotes: 1

Related Questions