Python Forum
[S0LVED] [Tkinter] Center dialog? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [S0LVED] [Tkinter] Center dialog? (/thread-36528.html)



[S0LVED] [Tkinter] Center dialog? - Winfried - Mar-01-2022

Hello,

I've tried a few things, but still can't display this simple dialog at the center of the screen:

import tkinter as tk
from tkinter import simpledialog

#hide root window by using ".pyw" extension

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.overrideredirect(1)
root.withdraw()

answer = simpledialog.askstring("File", "Path to file?")
if answer is not None:
    print("File is ", answer)

root.destroy()
Any idea?

Thank you.


RE: [Tkinter] Center dialog? - Axel_Erfurt - Mar-01-2022

in Linux it is centered, also if I remove position and geometry

import tkinter as tk
from tkinter import simpledialog
 
#hide root window by using ".pyw" extension
 
root = tk.Tk()
 
root.overrideredirect(1)
root.withdraw()
 
answer = simpledialog.askstring("File", "Path to file?")
if answer is not None:
    print("File is ", answer)
 
root.destroy()



RE: [Tkinter] Center dialog? - Winfried - Mar-01-2022

Thanks. Not on my Windows host — more like somewhere in the upper left corner.


RE: [Tkinter] Center dialog? - deanhystad - Mar-01-2022

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()



RE: [Tkinter] Center dialog? - Winfried - Mar-01-2022

Thanks but… still somewhere in the upper left corner :-/

FWIW, it's Python 3.8.8 on Windows 7.

[Image: image.png]


RE: [Tkinter] Center dialog? - deanhystad - Mar-01-2022

That is not my experience. When I run the code on Window 10, I get the dialog right in the middle of the window. I ran with Python 3.6 and 3.9. I have no way to test with Windows 7.

Leave the parent window visible. Is the parent window appearing in the center or the upper left?
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)  # Where does root appear?
# root.withdraw()
root.update_idletasks()  # Run "mainloop" one time.  Changes root location.  Do before making dialog

answer = simpledialog.askstring("File", "Path to file?", parent=root)  # Should not need parent=root
if answer is not None:
    print("File is ", answer)

root.destroy()



RE: [Tkinter] Center dialog? - Winfried - Mar-01-2022

Yes, it's centered now.

But if I click elsewhere on the screen, I can no longer see the window, it's minimzed in the task bar, and the only way to close it is by killing the process

I'll read up on Tkinter. It's a bit more involved than I expected just to prompt the user for a string.

Thank you.


RE: [Tkinter] Center dialog? - deanhystad - Mar-01-2022

Not my experience with Windows 10. I tried running from VS Code, from the command line, changed extension to .pyw and double clicked on the file. None work as you describe. Tried on a Windows 11 machine. Does not work as you describe. Please post your code.


RE: [S0LVED] [Tkinter] Center dialog? - Winfried - Mar-01-2022

I copy/pasted your code exactly.

Using ".pyw" as the extension, ie. with no visible terminal window, if I minimize all windows, I can't re-maximize the icon sitting in the task bar. Neither does right-clicking on its minimzed icon in the task bar > "Close window" work: I have to kill the process to close it.

Using ".py", I can't re-maximize the GUI window, but at least, I can close the application by closing the terminal window (clicking on its X button.)

Maybe there's something different between Windows 7 and 10/11 in that regard.

No worries, I'll just read a book. I needed to learn Tkinter anyway.

[Image: image.png]