Mar-01-2022, 04:48 PM
(This post was last modified: Mar-01-2022, 04:48 PM by deanhystad.)
The dialog centers over the parent. If you don't specify a parent, the root window is used as the parent. When the dialog is created the parent is still in the upper left corner. It is not until the mainloop that the parent window is moved to the location specified in the geometry() call. You need to move the parent window before you create the dialog. See below.
import tkinter as tk from tkinter import simpledialog root = tk.Tk() windowWidth = root.winfo_reqwidth() windowHeight = root.winfo_reqheight() positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2) positionDown = int(root.winfo_screenheight()/2 - windowHeight/2) root.geometry("+{}+{}".format(positionRight, positionDown)) # Root does not move yet root.overrideredirect(1) root.withdraw() root.update_idletasks() # Run "mainloop" one time. Changes root location. Do before making dialog answer = simpledialog.askstring("File", "Path to file?") if answer is not None: print("File is ", answer) root.destroy()