How to specify where a Tkinter window opens?
This answer is based on Rachel's answer. Her code did not work originally, but with some tweaking I was able to fix the mistakes.
import tkinter as tkroot = tk.Tk() # create a Tk root windoww = 800 # width for the Tk rooth = 650 # height for the Tk root# get screen width and heightws = root.winfo_screenwidth() # width of the screenhs = root.winfo_screenheight() # height of the screen# calculate x and y coordinates for the Tk root windowx = (ws/2) - (w/2)y = (hs/2) - (h/2)# set the dimensions of the screen # and where it is placedroot.geometry('%dx%d+%d+%d' % (w, h, x, y))root.mainloop() # starts the mainloop
Try this
import tkinter as tkdef center_window(width=300, height=200): # get screen width and height screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() # calculate position x and y coordinates x = (screen_width/2) - (width/2) y = (screen_height/2) - (height/2) root.geometry('%dx%d+%d+%d' % (width, height, x, y))root = tk.Tk()center_window(500, 400)root.mainloop()
root.geometry('250x150+0+0')
The first two parameters are the width and height of the window. The last two parameters are x and y screen coordinates. You can specify the required x and y coordinates