Tkinter: Too much space between label and button frame

tkinter space between buttons
tkinter margin
tkinter frame grid
tkinter button alignment
tkinter frame within frame
space between labels in tkinter
tkinter multiple frames
tkinter place

I'm pretty new to Tkinter and I build a little window with different widgets.

My Code looks like this:

import tkinter as tk
from tkinter import ttk


class Application(tk.Frame):

    def __init__(self, master):
        super().__init__(master)
        self.master = master
        self.master.geometry("800x600")
        self.master.title("Tkinter Sandbox")
        self.master.grid_rowconfigure(0, weight=1)
        self.master.grid_columnconfigure(1, weight=1)

       self._create_left_frame()
       self._create_button_bar()
       self._create_label_frame()

    def _create_left_frame(self):
       frame = tk.Frame(self.master, bg="red")

       tree_view = ttk.Treeview(frame)
       tree_view.column("#0", stretch=tk.NO)
       tree_view.heading("#0", text="Treeview")
       tree_view.pack(fill=tk.Y, expand=1)
       frame.grid(row=0, column=0, rowspan=2, sticky=tk.N + tk.S)

    def _create_button_bar(self):
       frame = tk.Frame(self.master, bg="blue")

       button_run_single = tk.Button(frame, text="Button 1")
       button_run_all = tk.Button(frame, text="Button 2")
       button_details = tk.Button(frame, text="Button 3")
       button_run_single.grid(row=0, column=0)
       button_run_all.grid(row=0, column=1, padx=(35, 35))
       button_details.grid(row=0, column=2)
       frame.grid(row=0, column=1, sticky=tk.N)

    def _create_label_frame(self):
       frame = tk.Frame(self.master, bg="blue")

       name_label = tk.Label(frame, text="Label 1")
       performance_label = tk.Label(frame, text="Label 2")
       name_entry = tk.Entry(frame)
       performance_entry = tk.Entry(frame)
       name_label.grid(row=0, column=0)
       name_entry.grid(row=0, column=1)
       performance_label.grid(row=1, column=0)
       performance_entry.grid(row=1, column=1)
       frame.grid(row=1, column=1)


if __name__ == '__main__':
    root = tk.Tk()
    app = Application(root)
    app.mainloop()

Between the three buttons and the label + entry frame is a huge space. I want the button and label + entry frame right under each other, without the huge space but the treeview should also expand vertically over the whole application window.

I think the problem might be my row and column configuration but I don't know how to solve this problem.

The way you've structured your code makes it hard to see the problem. As a good general rule of thumb, all calls to grid or pack for widgets within a single parent should be in one place. Otherwise, you create dependencies between functions that are hard to see and understand.

I recommend having each of your helper functions return the frame rather than calling grid on the frame. That way you give control to Application.__init__ for the layout of the main sections of the window.

For example:

left_frame = self._create_left_frame()
button_bar = self._create_button_bar()
label_frame = self._create_label_frame()

left_frame.pack(side="left", fill="y")
button_bar.pack(side="top", fill="x")
label_frame.pack(side="top", fill="both", expand=True)

I used pack here because it requires less code than grid for this type of layout. However, if you choose to switch to grid, or wish to add more widgets to the root window later, you only have to modify this one function rather than modify the grid calls in multiple functions.

Note: this requires that your functions each do return frame to pass the frame back to the __init__ method. You also need to remove frame.grid from each of your helper functions.

With just that simple change you end up with the button bar and label/entry combinations at the top of the section on the right. In the following screenshot I changed the background of the button_bar to green so you can see that it fills the top of the right side of the UI.

Tkinter grid spacing problems, You have several things conspiring against you in this little program. For one, frame is set to None , so you are actually putting all these widgets in the root one column to have a weight, so that tkinter knows how to allocate extra space. This prevents the columns that your buttons are in from expanding. Label( toolbar, . I'm pretty new to Tkinter and I build a little window with different widgets. My Code looks like this: import tkinter as tk from tkinter import ttk class Application(tk.Frame): def __init__

You need to change line

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

to

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

so that the second row takes all the space. Then you need to stick widgets from the label frame to its top by adding sticky parameter to the grid call in _create_label_frame:

frame.grid(row=1, column=1, sticky=tk.N)

Spacing Between two labels are very far and Top , Hey, I have been working with Tkinter code using grid and with some certain Spacing Between two labels are very far and Top label is not in Center using Tkinter b = tk.Button(Frame2, text = "Display" , command = self .loopCap) getting so much space while initializing "row=3 and column 0" between� Other 5 widgets are Frame, Label, Entry, Text, Canvas, Button, Radiobutton, 6 Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox 7 LabelFrame and PanedWindow. 8 9 Properties of the widgets are specified with keyword arguments. 10 Keyword arguments have the same name as the corresponding resource 11 under Tk. 12 13 Widgets are

I prefer to use the Pack Function since it gives a more open window - its easy to configure. When you use Pack() you can use labels with no text and just spaces to create a spacer, by doing this you won't run into the problem your facing.

Layout management in Tkinter - place, pack, grid managers, The label is placed on the frame at x=20 and y=20 coordinates. We change the border of the frame so that the frame is visible; by default it is flat. The padx puts some space between the button widgets and between the� The Loop Loops Too Much ; How to position labels of image and text in Python tkinter? A ticker for long messages (Python/Tkinter) desperate help with arrays ; i need my python code to be changed in java ; Python 2 vs. Python 3 ; from textboxes to listview ; How to re-start a loop in Python? Dynamically add components to a form by button click

How to pad space around the text of Python tkinter label, This lesson looks at how space can be added around the text within a label when using Duration: 5:59 Posted: Feb 12, 2018 Tkinter provides classes which allow the display, positioning and control of widgets. Toplevel widgets are Tk and Toplevel. Other widgets are Frame, Label, Entry, Text, Canvas, Button, Radiobutton, Checkbutton, Scale, Listbox, Scrollbar, OptionMenu, Spinbox LabelFrame and PanedWindow. Properties of the widgets are specified with keyword arguments.

Tkinter - why no space between buttons?, why doesnt this generate any space between the 1 and 4 button and 4and 7 etc? they are on row 5 and then 7 so shoiuld be some blanks in between. Some Text") """Display the main window" with a little bit of padding""" self.grid(padx=10, pady=10) self. lbText = Label(self, text="Enter numbers:") self. Master GUI Development with Tkinter and Python! Create multiple Tkinter projects, including forms, games, and even a chat app that interacts with a web API. Fully understand the two most important Geometry Managers in Tkinter: grid and pack. Learn how to use a wide variety of widgets, such as labels, entries, buttons, spinboxes, and even the

Using padding to add space around widgets, aspects of our widgets by adding a little space around them, so they can breathe. While tkinter might have had a reputation for creating ugly GUIs, this has and then we will use a loop to achieve the same thing in a much better way . Our LabelFrame looks a bit tight as it blends into the main window towards the bottom. Tkinter: I can't display any frames I've been learning Tkinter and I wanted to make a program but no matter what I do, I can't get any of the frames to show up. Could anyone tell me what is wrong with my code

Comments
  • Use also a right_frame for _button_bar and label_frame
  • Wow that is a really great solution and a very useful tipp. I'll do this in my future projects. Do you know some resources or books where I can find best practices regarding these design patterns?
  • Hey Brian. Just one last question. Can you provide the sourcecode for your frame? I want to dig deeper a little bit.
  • @NealMcBeal: what do you mean by "the sorucecode for your frame"? What frame are you talking about? The code I used was the code from the question with the changes described in the answer.
  • Ah interesting because I changed the code regarding your proposal and I have a different behavior in the Label section with label 1 and 2. They are centered. Do you know why I can't insert a progressbar at the bottom?
  • @NealMcBeal: there is nothing preventing you from putting a progressbar at the bottom. If you're using pack like in my example, you should pack it to the bottom before packing the left and right frames. See stackoverflow.com/a/57396569/7432