Hung Truong
Hung Truong

Reputation: 369

Tkinter : How to center the window title

I am creating a project using tkinter and when I create a window, I couldn't seem to get the window title to center itself (Like most programs nowadays). Here's the example code:

from tkinter import *

root = Tk()
root.title("Window Title".center(110))# Doesn't seem to work

root.mainloop()

Is there a way to center the window title up ? Thanks in advance

Upvotes: 3

Views: 16193

Answers (5)

Sanidhya Sahu
Sanidhya Sahu

Reputation: 37

Just add a full stop or any special character at starting so that blank spaces are considered and then add blank spaces.

title='UNI-CARD'root.title(f'. {title}')

Upvotes: 0

kanishka kumarasiri
kanishka kumarasiri

Reputation: 23

width=root.winfo_screenwidth()
spacer=(" "*(int(width)//6))
root.title(spacer+"Your title")

This is not that much perfect but this will work.

Upvotes: 0

Steven Summers
Steven Summers

Reputation: 5384

More adding onto what Billal suggested is this example that adjust depending on the window size. I still wouldn't recommend it since it's just a hack for visual aesthetics but if you really want to have it.

import tkinter as tk

def center(e):
    w = int(root.winfo_width() / 3.5) # get root width and scale it ( in pixels )
    s = 'Hello Word'.rjust(w//2)
    root.title(s)

root = tk.Tk()
root.bind("<Configure>", center) # called when window resized
root.mainloop()

Upvotes: 1

Andath
Andath

Reputation: 22724

I came up with a trick that does the job and it consists in simply adding as much blank space before the title:

import tkinter as tk

root = tk.Tk()
root.title("                                                                          Window Title")# Add the blank space
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)

root.mainloop()

Output:

enter image description here

Alternatively, you can use a string consisting of an empty space and concatenate it to the title after multiplication. I mean:

import tkinter as tk

root = tk.Tk()
blank_space =" " # One empty space
root.title(80*blank_space+"Window Title")# Easier to add the blank space 
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)

root.mainloop()

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 385900

There is nothing you can do. Tkinter has no control over how the window manager or OS displays the titles of windows other than to specify the text.

Upvotes: 3

Related Questions