[Tutor] tkSimpleDialog box question.

Abel Daniel abli@freemail.hu
Wed Jun 18 14:56:39 2003


Suresh  Kumar suresh_vsamy@rediffmail.com) wrote:
>     Anybody know how to place "tkSimpleDialog" at a given user defined
> location rather than the default one? I am calling "kSimpleDialog" in my
> application and it always display near to top-left ( say 100,100). But i
> want to place at center of the screen or somewhere else. How can i do it?
> My code is given below.
I don't think you will be able to. tkSimpleDialog is implemented in
python, take a look at it's source code:
tkSimpleDialog inherits from Tkinter.Toplevel. The __init__ of the class
ends with this:

        if self.parent is not None:
            self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
                                      parent.winfo_rooty()+50))

        self.initial_focus.focus_set()

        self.wait_window(self)

This means that if self.parent is not None, the geometry is explicitly
set. self.wait_window waits until the dialog is closed (the widget self
is destroyed.)

This means that without hacking tkSimpleDialog, you won't be able to do
this.
OTOH, this 'hacking' is pretty strait-forward. We need to make
tkSimpleDialog accept an optional geometry option, so the following
should work:

class hackedDialog(tkSimpleDialog.Dialog):
    def __init__(self, parent, title = None, geometry=None):
        #                   New stuff:       ^^^^^^^^^^^^^
        #
        # Copy-paste the code of tkSimpleDialog.Dialog.__init__ here,
        # except the last couple of lines:
        #
        if self.parent is not None:
            if geometry is None:
                self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
                                      parent.winfo_rooty()+50))
            else:
                self.geometry(geometry)

        self.initial_focus.focus_set()

        self.wait_window(self)

However, you are already using Pmw, so why not use Pmw.Dialog instead?
That way, you don't have to hack the source, just call
dialog.component('hull').geometry('100x100+50+50').
(I guess you will have to call it every time before you show the dialog
otherwise it will stay the same if the user resized it last time.)

(Read the docs on Tkinter.Toplevel.geometry() about the meaning of those
...geometry() calls)


Abel Daniel