1

I have a problem where when I create a window in Tkinter, it is offset a bit to the side instead of centered. I was wondering if there was some way to center the window without using too many lines of code. I've tried using .eval('tk::PlaceWindow . center'), but it just sets the top left edge of the window to the center. I've also tried calculating the size of the window and placing it with .geometry(). Here I have a button that creates a window when clicked:

from Tkinter import *

button = Button(lambda:newWind(title))

#function being called

def newWind(bPressed):
    newApp = tk.Tk()
    newApp.title(bPressed)
    newApp.geometry('1920x1080')

The window is offset by about 10 pixels when created. Any easy and short way I can fix this?

Anonymous
  • 21
  • 7

2 Answers2

0

I recommend you to use the PlaceWindow method again correctly. I think it will place a tkinter window at the center of the screen where you can pass the top level window as an argument and add it into the center.

newApp.eval('tk::PlaceWindow . center')
ranjitraut
  • 11
  • 4
0

If .eval("tk::PlaceWindow . center") does not work, you can calculate the required position (x, y) based on the window and screen size and use .geometry() to place the window at the calculated position:

def center_window(win):
    # make sure window is updated
    win.update()
    # get the screen resolution
    scr_width, scr_height = win.winfo_screenwidth(), win.winfo_screenheight()
    # get the window resolution
    border_width = win.winfo_rootx() - win.winfo_x()
    title_height = win.winfo_rooty() - win.winfo_y()
    win_width = win.winfo_width() + border_width + border_width
    win_height = win.winfo_height() + title_height + border_width
    # calculate the position
    x = (scr_width - win_width) // 2
    y = (scr_height - win_height) // 2
    # place the window at the calculated position
    win.geometry("+%d+%d" % (x, y))

def newWind(bPressed):
    newApp = tk.Tk()
    newApp.title(bPressed)
    newApp.geometry('1920x1080')
    # center the window
    center_window(newApp)

Note that it is better to use tk.Toplevel instead of tk.Tk for windows other than the root/main window.

acw1668
  • 40,144
  • 5
  • 22
  • 34
  • hmm. `center_win` seems to be overwriting the `newApp.geometry('1920x1080')` or something. regardless, the window is still slightly off center. – Anonymous Jul 31 '23 at 00:59
  • Only position is used in `.geometry()` inside `center_window()`, so it won't overwrite the window size. The offset may be caused by that the window size may not include the height of the title bar. – acw1668 Jul 31 '23 at 01:03
  • the window is still offset on the x-axis. should i tweak the width of the window as well to fix that? – Anonymous Jul 31 '23 at 01:24
  • The horizontal offset is due to the width of the window border. You can refer [this answer](https://stackoverflow.com/a/10018670/5317403) to calculate the size of the window border and the title bar. – acw1668 Jul 31 '23 at 01:48