Control position of Tkinter common dialogs?
Python 2.7 under Windows: How can we control the position of Tkinter's common dialogs? Here's what we've discovered: * Certain common dialogs always open up relative to their parent window * Certain common dialogs always open up centered on the user's desktop * All common dialogs appear to ignore the optional parent= parameter Questions: * How can we force a dialog to open up relative to its parent window? * How can we force a dialog to open up centered on the user's desktop? Background: import tkColorChooser as colorchooser import tkFileDialog as filedialog import tkMessageBox as messagebox # always open up relative to parent windows fileOpen = filedialog.askopenfilename() fileOpens = filedialog.askopenfilenames() fileSaveAs = filedialog.asksaveasfilename() color = colorchooser.askcolor() # always open up centered on desktop folderOpen = filedialog.askdirectory() messagebox.askquestion() Malcolm
Hello, I do not know whether there is an easier way to accomplish what you want, but maybe you can create a new toplevel window in the middle of the screen, then make it invisible and position the dialog windows relative to this new toplevel... Like this: http://paste-it.net/public/q2a5594/ I know, this is not nice. Maybe someone else will offer a more "palatable" alternative. Firat 2010/10/25 <python@bdurham.com>
Python 2.7 under Windows: How can we control the position of Tkinter's common dialogs?
Here's what we've discovered:
* Certain common dialogs always open up relative to their parent window
* Certain common dialogs always open up centered on the user's desktop
* All common dialogs appear to ignore the optional parent= parameter
Questions:
* How can we force a dialog to open up relative to its parent window?
* How can we force a dialog to open up centered on the user's desktop?
Background:
import tkColorChooser as colorchooser import tkFileDialog as filedialog import tkMessageBox as messagebox
# always open up relative to parent windows fileOpen = filedialog.askopenfilename() fileOpens = filedialog.askopenfilenames() fileSaveAs = filedialog.asksaveasfilename() color = colorchooser.askcolor()
# always open up centered on desktop folderOpen = filedialog.askdirectory() messagebox.askquestion()
Malcolm _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Firat,
You can create a new toplevel window in the middle of the screen, then make it invisible and position the dialog windows relative to this new toplevel... Like this: [1]http://paste-it.net/public/q2a5594/
Thank you very much for your solution - that's an excellent workaround for me. Best regards, Malcolm References 1. http://paste-it.net/public/q2a5594/
Hi, I have a similar issue, however not for common dialogs but for a custom toplevel transient dialogs. As you create your dialog it's dimensions are not known until all idletasks have completed for child widgets being added to the dialog. Now if you call top.widthdraw() before adding the children, the dimensions of the toplevel is not calculated. e.g. class MyDialog(TopLevel): def __init__(self, parent=None, title=None): TopLevel.__init__(self, parent) self.title(title) self.parent = parent self.BuildGUI(master=self) self.CenterDialog() self.transient(parent) def BuildGUI(self, master): # Add widgets here frames etc... pass def CenterDialog(self): self.update_idletasks() w= self["width"]!=0 and self["width"] or self.winfo_width() h= self["height"]!=0 and self["height"] or self.winfo_height() ws,hs = self.winfo_screenwidth(),self.winfo_screenheight() self.geometry('%dx%d+%d+%d' % (w, h, (ws/2) - (w/2), (hs/2) - (h/2))) When above is called, my new toplevel is created, BuildGUI adds all the widgets and the toplevel is displayed, however when CenterDialog is called the toplevel jumps to new centered geometry. Placing self.withdraw() before self.BuildGUI() and then placing self.deiconify() after self.CenterDialog() I thought would do the trick. But I end up with a width and height of 1 in CenterDialog(), withdraw somehow stops calculation taking place. iconify has same effect as withdraw. Any ideas what I am doing wrong ? thanks Matt On 10/26/10 06:10 PM, python@bdurham.com wrote:
You can create a new toplevel window in the middle of the screen,
Firat, then make it invisible and position the dialog windows relative to this new toplevel... Like this: http://paste-it.net/public/q2a5594/ Thank you very much for your solution - that's an excellent workaround for me. Best regards, Malcolm
_______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Hi Matt,
I have a similar issue, however not for common dialogs but for a custom toplevel transient dialogs. As you create your dialog it's dimensions are not known until all idletasks have completed for child widgets being added to the dialog.
Disclaimer: I'm new to Tkinter development so take this untested idea with a grain of salt ... Have you tried positioning your toplevel window off screen (negative coordinates) so that it can be fully rendered (and you can capture its dimensions after all layout calculations have completed), then center your window afterwards? If you come up with a working solution, I would love to hear about it. I'll do the same from my end. Malcolm
Malcolm, Nice idea, but unfortunately don't work... tkinter appears to be smart enough to ensure you cannot place a dialog outside the bounds of the root display size. Using negative values, positions dialog initially at 0,0. Using massive positive values, positions dialog bottom right of screen. Still looking for a nice solution :-) cheers Matt On 10/27/10 12:07 PM, python@bdurham.com wrote:
Hi Matt,
I have a similar issue, however not for common dialogs but for a custom toplevel transient dialogs. As you create your dialog it's dimensions are not known until all idletasks have completed for child widgets being added to the dialog.
Disclaimer: I'm new to Tkinter development so take this untested idea with a grain of salt ...
Have you tried positioning your toplevel window off screen (negative coordinates) so that it can be fully rendered (and you can capture its dimensions after all layout calculations have completed), then center your window afterwards?
If you come up with a working solution, I would love to hear about it. I'll do the same from my end.
Malcolm
On 27/10/10 22:27, Matt Keenan wrote:
Malcolm,
Nice idea, but unfortunately don't work... tkinter appears to be smart enough to ensure you cannot place a dialog outside the bounds of the root display size.
Using negative values, positions dialog initially at 0,0. Using massive positive values, positions dialog bottom right of screen.
Still looking for a nice solution :-)
cheers
Matt
On 10/27/10 12:07 PM, python@bdurham.com wrote:
Hi Matt,
I have a similar issue, however not for common dialogs but for a custom toplevel transient dialogs. As you create your dialog it's dimensions are not known until all idletasks have completed for child widgets being added to the dialog.
Disclaimer: I'm new to Tkinter development so take this untested idea with a grain of salt ...
Have you tried positioning your toplevel window off screen (negative coordinates) so that it can be fully rendered (and you can capture its dimensions after all layout calculations have completed), then center your window afterwards?
If you come up with a working solution, I would love to hear about it. I'll do the same from my end.
Malcolm
You need to use the winfo_reqwidth() and winfo_reqheight() methods to determine the windows "requested" width and height. http://www.pythonware.com/library/tkinter/introduction/x9548-window-related-... Regards, John .
Thanks John, that works a treat... On 10/27/10 10:26 PM, John McMonagle wrote:
On 27/10/10 22:27, Matt Keenan wrote:
Malcolm,
Nice idea, but unfortunately don't work... tkinter appears to be smart enough to ensure you cannot place a dialog outside the bounds of the root display size.
Using negative values, positions dialog initially at 0,0. Using massive positive values, positions dialog bottom right of screen.
Still looking for a nice solution :-)
cheers
Matt
On 10/27/10 12:07 PM, python@bdurham.com wrote:
Hi Matt,
I have a similar issue, however not for common dialogs but for a custom toplevel transient dialogs. As you create your dialog it's dimensions are not known until all idletasks have completed for child widgets being added to the dialog.
Disclaimer: I'm new to Tkinter development so take this untested idea with a grain of salt ...
Have you tried positioning your toplevel window off screen (negative coordinates) so that it can be fully rendered (and you can capture its dimensions after all layout calculations have completed), then center your window afterwards?
If you come up with a working solution, I would love to hear about it. I'll do the same from my end.
Malcolm
You need to use the winfo_reqwidth() and winfo_reqheight() methods to determine the windows "requested" width and height.
http://www.pythonware.com/library/tkinter/introduction/x9548-window-related-...
Regards,
John .
All common dialogs appear to ignore the optional parent = parameter It seems it is because your code invoke the winfo_root() before the grid manager finish is calculations. and this command return a Null decimal string. According to Tk manual "https://www.tcl.tk/man/tcl8.4/TkCmd/winfo.htm#M52" If you need the true width immediately after creating a widget, invoke update to force the geometry manager to arrange it, or use winfo reqwidth to get the window's requested width instead of its actual width. This code works perfectly self.update() self.geometry("+%d+%d" % (self.parent.winfo_rootx()+50, self.parent.winfo_rooty()+50 ) ) -- View this message in context: http://python.6.x6.nabble.com/Control-position-of-Tkinter-common-dialogs-tp1... Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
Hi, On Wed, 7 Dec 2016 18:36:00 -0700 (MST) "gerard.roger.laine@free.fr" <gerard.roger.laine@free.fr> wrote:
All common dialogs appear to ignore the optional parent = parameter
It seems it is because your code invoke the winfo_root() before the grid manager finish is calculations. and this command return a Null decimal string. (...)
I am a bit confused about this, which dialogs do you mean exactly? Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Sometimes a man will tell his bartender things he'll never tell his doctor. -- Dr. Phillip Boyce, "The Menagerie" ("The Cage"), stardate unknown.
participants (6)
-
Firat Ozgul -
gerard.roger.laine@free.fr -
John McMonagle -
Matt Keenan -
Michael Lange -
python@bdurham.com