Search code examples
pythontkinterlabeltkinter-layout

I need to make a label expand based on the window size


I wanted to make a title label that spans the top of the screen with the text in the middle. this works when the window opens but if I go into fullscreen, the label only spans half the length. if I make the label bigger, the text is not in the middle. it's a win-lose situation. any way to make both of them work?

import tkinter as tk
from tkinter import *

Title_Font = ("Hallo Sans", 20, "bold")
MyBlue = '#30D5C8'

Home = tk.Tk()
Home.title("Guitar Bud")
Home.geometry('1000x700')
Home.configure(bg='grey')

ExersisesLbl = tk.Label(Home, width=60, height=1, bg='black', fg=MyBlue, text='Exersises', font=Title_Font, anchor=CENTER)
ExersisesLbl.grid(row=0, column=0, columnspan=5, sticky='ew')

Solution

  • like @JacksonPro said, use grid_columnconfigure().

    import tkinter as tk
    from tkinter import *
    
    Title_Font = ("Hallo Sans", 20, "bold")
    MyBlue = '#30D5C8'
    
    Home = tk.Tk()
    Home.title("Guitar Bud")
    Home.geometry('1000x700')
    Home.configure(bg='grey')
    Home.grid_columnconfigure(0, weight=1)
    
    ExersisesLbl = tk.Label(Home, width=60, height=1, bg='black', fg=MyBlue, text='Exersises', font=Title_Font, anchor=CENTER)
    ExersisesLbl.grid(row=0, column=0, columnspan=5, sticky='ew')
    
    mainloop()