Control position of Tkinter common dialogs?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Control position of Tkinter common dialogs?

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
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Control position of Tkinter common dialogs?

Firat Ozgul
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 <[hidden email]>
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
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss


_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Control position of Tkinter common dialogs?

python@bdurham.com
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: 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
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Control position of Tkinter common dialogs?

Matt Keenan-5
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, [hidden email] wrote:

> 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: 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
> [hidden email]
> http://mail.python.org/mailman/listinfo/tkinter-discuss

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Control position of Tkinter common dialogs?

python@bdurham.com
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
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Control position of Tkinter common dialogs?

Matt Keenan-5
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, [hidden email] 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

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Control position of Tkinter common dialogs?

John McMonagle-5
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, [hidden email] 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-information.htm

Regards,

John
.
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Control position of Tkinter common dialogs?

Matt Keenan-5
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, [hidden email] 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-information.htm
>
> Regards,
>
> John
> .

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Control position of Tkinter common dialogs?

gerard.roger.laine@free.fr
This post has NOT been accepted by the mailing list yet.
In reply to this post by python@bdurham.com
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 ) )
Loading...