Posts: 212
Threads: 94
Joined: Aug 2018
Mar-01-2022, 02:07 PM
(This post was last modified: Mar-01-2022, 06:41 PM by Winfried.)
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.
Posts: 1,032
Threads: 16
Joined: Dec 2016
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()
Posts: 212
Threads: 94
Joined: Aug 2018
Thanks. Not on my Windows host — more like somewhere in the upper left corner.
Posts: 6,790
Threads: 20
Joined: Feb 2020
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()
Posts: 212
Threads: 94
Joined: Aug 2018
Mar-01-2022, 05:55 PM
(This post was last modified: Mar-01-2022, 05:55 PM by Winfried.)
Thanks but… still somewhere in the upper left corner :-/
FWIW, it's Python 3.8.8 on Windows 7.
Posts: 6,790
Threads: 20
Joined: Feb 2020
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()
Posts: 212
Threads: 94
Joined: Aug 2018
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.
Posts: 6,790
Threads: 20
Joined: Feb 2020
Mar-01-2022, 06:47 PM
(This post was last modified: Mar-01-2022, 06:49 PM by deanhystad.)
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.
Posts: 212
Threads: 94
Joined: Aug 2018
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.
|