[Tutor] Tkinter problem with withdraw() by upgrdaing from Python 2.2.2 to 2.2.3

Steckel, Ralf Ralf.Steckel at AtosOrigin.com
Thu Jan 29 09:47:00 EST 2004


Dear Tutor,

I have a problem with the withdraw() method for the root window Tk from
Tkinter. I use an application with dialogs derived from the dialog example
by Fredrik Lundh's 'An Introduction to Tkinter'. In my application the
dialog has buttons, which modify the layout of the dialog itself and
therefore, after pressing each button the dialog is closed and opened again
in an infinite loop until a certain result is returned.

To hide the root window of Tkinter and to show only the dialog I use the
withdraw method of Tk as suggested by earlier e-mails of this list. This
works fine on Suse Linux 7.2, Python 2.2.2, Tcl/Tk 8.3.3-23. But on Suse
Linux 8.2, Python 2.2.3, Tcl/Tk 8.4.2 there is just a short flickering of
the root and of the dialog window and none of both windows are visible.

Do I something wrong or results the withdrawing of the parent window in
newer Tkinter versions in hiding the child window too? (The same effect is
on Windows XP SP 1 with Python 2.2.3 for Windows)

Please find reduced source code below.

Thanks and best wishes,

Ralf

#! /usr/bin/env python
#
# dialog basis class
#
import time
from Tkinter import *

class rstDialog(Toplevel):

   def __init__(self, parent, title = None):
      self.result = None

      self.top = Toplevel.__init__(self, parent)
      self.transient(parent)

      if title:
         self.title(title)

      self.parent = parent

      body = Frame(self)
      self.initial_focus = self.body(body)
      body.pack(padx = 5, pady = 5)

      self.buttonbox()

      self.grab_set()

      if not self.initial_focus:
         self.initial_focus = self

      self.protocol('WM_DELETE_WINDOW', self.cancel)

      print 'parent x/y', self.parent.winfo_rootx(),
self.parent.winfo_rooty()

      #self.geometry('+%d+%d' % (50 ,  50 ) )

      self.geometry('+%d+%d' % (self.parent.winfo_rootx() + 50,
                              self.parent.winfo_rooty() + 50))

      self.initial_focus.focus_set()

      self.wait_window(self)

      #
      # construction hooks
      #
   def body(self, master):
      # create dialog body, return widget that should have
      # initial focus. this method should be overwritten

      #Label(master, text = 'Dialog').grid()

      pass

   def buttonbox(self):
      # add standard button box, override if you don't want the
      # standard buttons

      box = Frame(self)

      w = Button(box, text = 'Finish', width = 10,
                  command = self.ok,
                  default = ACTIVE)
      w.pack(side = LEFT, padx = 5, pady = 5)

      w = Button(box, text = 'Again', width = 10,
                  command = self.cancel)
      w.pack(side = LEFT, padx = 5, pady = 5)

      self.bind('<Return>', self.ok)
      self.bind('<Escape>', self.cancel)

      box.pack()

   #
   # standard buttons semantics
   #

   def ok(self, event = None):
      if not self.validate():
         # put focus back
         self.initial_focus.focus_set()
         return

      self.withdraw()
      self.update_idletasks()

      self.apply()

      self.result = 1

      self.cancel()
   def cancel(self, event = None):
      # put focus back to parent window
      self.parent.focus_set()
      self.destroy()

      return self.result

   #
   # command hooks
   #

   def validate(self):
      # override
      return 1

   def apply(self):
      # override
      pass


def run():
   root = Tk()
   root.title('Root')

   width=root.winfo_screenwidth()
   heigth=root.winfo_screenheight()

   root_width = 200
   root_height = 100

   root.geometry('%dx%d+%d+%d' % ( root_width, root_height, \
      (width-root_width)/2, (heigth-root_height)/2 ) )

   root.initial_focus = root
   root.initial_focus.focus_set()
   root.update()
   # this 'withdraw' works only with Python 2.2.2 Tcl/Tk 8.3.3-23
   # this 'withdraw' doesn't work with Python 2.2.3 Tcl/Tk 8.4.2
   root.withdraw()

   while 1:
      dlg = rstDialog(root, 'Dialog')
      if None <> dlg.result:
         return

run()



More information about the Tutor mailing list