[Tutor] Tkinter modal dialogs

K.Weinert@gmx.net K.Weinert at gmx.net
Mon Nov 21 16:19:26 CET 2005


Hello tutors!

My file search dialog is working now! I attach a copy of the module. It
would be nice, if someone could look over it and make suggestions to improve
it. There are at least two points I find not very elegant in the program:

1. The dialog is ended by raising a SystemExit exception. (I found the idea
searching www.koders.com for Tkinter programs.) Here is a small example of
the technique:

--- snip ---
import Tix
from Tkconstants import *

class YesNo:
  def __init__(self, root, title="", question=""):
    self.top= Tix.Toplevel(root)
    self.top.title(title)
    self.top.withdraw() # don't show
    Tix.Label(self.top, text=question).pack()
    Tix.Button(self.top, text="Yes", command=self.yes_cmd).pack(side=LEFT)
    Tix.Button(self.top, text="No", command=self.no_cmd).pack(side=RIGHT)
  def ask(self):
    self.top.deiconify()
    self.top.tkraise()
    self.top.grab_set()
    try:
      self.top.mainloop()
    except SystemExit, answer:
      self.top.grab_release()
      self.top.withdraw()
    return answer
  def yes_cmd(self, event=None):
    raise SystemExit, True
  def no_cmd(self, event=None):
    raise SystemExit, False

if __name__=="__main__":
  def set_label():
    global yes_no_dlg, yes_no_label
    answer= yes_no_dlg.ask()
    yes_no_label["text"]= str(answer)
    yes_no_label.update_idletasks()   
    
  root = Tix.Tk()
  root.title("Modal Dialog Demo")

  yes_no_dlg = YesNo(root, "Question", "Do you find this approach elegant?")

  yes_no_label= Tix.Label(root, text="Unknown.")
  yes_no_label.pack()
  Tix.Button(root,  text = 'Question..', command = set_label).pack()
  Tix.Button(root, text = 'Exit', command = root.destroy).pack()
    
  root.mainloop()
--- snip ---

This is deceiving. A SystemExit exception should, in my opinion, exit the
program. Is there an other way to do this?

2. The Listbox in the file search dialog seems to have two selection modes.
When scrolling with the page-up/page-down keys, only the underlined
selection moves, while the other selection (indicated by a different
background color) remains where it is unless I press the space key. That is
imo too complicated. Is there a Listbox with only one selection type (or an
option to Tix.ScrolledListbox)?

Kind regards,
Karsten.

-- 
10 GB Mailbox, 100 FreeSMS/Monat http://www.gmx.net/de/go/topmail
+++ GMX - die erste Adresse für Mail, Message, More +++
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: searchdlg.py
Url: http://mail.python.org/pipermail/tutor/attachments/20051121/ccd2ca4b/searchdlg.asc
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: grep_mod.py
Url: http://mail.python.org/pipermail/tutor/attachments/20051121/ccd2ca4b/grep_mod.pot


More information about the Tutor mailing list