pythonuser-interfacetkinterwidget

Tkinter: Label pushes widgets


I am having a simple tkinter GUI. I have SpinBox and Combobox (on the same line), and then Label (below them), that displays data based on what is in the SpinBox and ComboBox. The Problem is, that when the Label updates, it pushes the other widgets (Spinbox and Combobox) to the right side. I do not want that. What should I do? I tried using both pack and grid.

There is an example (actual code have to much things not related):

from tkinter import *
from ttkbootstrap.constants import *
import ttkbootstrap as tb

root = tb.Window(themename="darkly")
root.geometry("1280x720")


string1 = list("sometext"+str(i) for i in range(0, 10))


def time_update():
    content.config(text=f'{string1}')

time = tb.Spinbox(root, from_=0, to=10, font=("Helvetica", 20), command=time_update)
time.grid(row=0, column=0, pady=10, padx=10)

values = ["a", "b", "c"]
channel = tb.Combobox(root, bootstyle="dark", values=values)
channel.grid(row=0, column=1, pady=10)


content = tb.Label(text="",
                     font=("Helvetica", 15), bootstyle='info')
content.grid(row=1, column=0, columnspan=2, padx=10, pady=10)




root.mainloop()

Solution

  • You are setting the spinbox and the combobox in the first row of a grid. When the label in the second row is filled, the content of the rows above get wider by the amount of the Label.

    To solve this behavior try to put the spinbox and the combobox in a separate frame. Then pack first the frame and then the label. This way the elements in the frame will not be affected by the width of the label.

    frame = tb.Frame(root)  # new frame
    
    time = tb.Spinbox(frame, from_=0, to=10, font=("Helvetica", 20), 
    command=time_update)
    time.grid(row=0, column=0, pady=10, padx=10)
    
    values = ["a", "b", "c"]
    channel = tb.Combobox(frame, bootstyle="dark", values=values)
    channel.grid(row=0, column=1, pady=10)
    
    frame.pack(anchor='w')  # justify to left
    
    content = tb.Label(text="", font=("Helvetica", 15), bootstyle='info')
    content.pack(padx=10, pady=10)  # pack below frame