Python Forum
[S0LVED] [Tkinter] Center dialog?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[S0LVED] [Tkinter] Center dialog?
#1
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.
Reply
#2
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()
Reply
#3
Thanks. Not on my Windows host — more like somewhere in the upper left corner.
Reply
#4
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()
Reply
#5
Thanks but… still somewhere in the upper left corner :-/

FWIW, it's Python 3.8.8 on Windows 7.

[Image: image.png]
Reply
#6
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()
Reply
#7
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.
Reply
#8
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.
Reply
#9
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hard disk structure like a file selection dialog malonn 2 802 Aug-09-2023, 09:14 PM
Last Post: malonn
  (OpenCV) Help to improve code for object detection and finding center of it saoko 0 1,209 May-14-2022, 05:34 PM
Last Post: saoko
  Program to move a dot towards a circle center plumberpy 10 4,229 Dec-03-2021, 12:20 PM
Last Post: BashBedlam
  how to view all installed software in a dialog Tyrel 6 2,150 Oct-09-2021, 08:11 PM
Last Post: Tyrel
  Set Text in Open Dialog Box giddyhead 0 1,681 May-21-2021, 06:31 PM
Last Post: giddyhead
  Subprocesses not opening File Select Dialog teut 2 2,422 Feb-22-2021, 08:07 PM
Last Post: teut
  Center align Kristenl2784 1 1,970 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  Image : center and resizing vvv 1 12,079 May-14-2017, 03:41 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020