python-2.5.2/win32/Lib/lib-tk/tkSimpleDialog.py
changeset 0 ae805ac0140d
equal deleted inserted replaced
-1:000000000000 0:ae805ac0140d
       
     1 #
       
     2 # An Introduction to Tkinter
       
     3 # tkSimpleDialog.py
       
     4 #
       
     5 # Copyright (c) 1997 by Fredrik Lundh
       
     6 #
       
     7 # fredrik@pythonware.com
       
     8 # http://www.pythonware.com
       
     9 #
       
    10 
       
    11 # --------------------------------------------------------------------
       
    12 # dialog base class
       
    13 
       
    14 '''Dialog boxes
       
    15 
       
    16 This module handles dialog boxes. It contains the following
       
    17 public symbols:
       
    18 
       
    19 Dialog -- a base class for dialogs
       
    20 
       
    21 askinteger -- get an integer from the user
       
    22 
       
    23 askfloat -- get a float from the user
       
    24 
       
    25 askstring -- get a string from the user
       
    26 '''
       
    27 
       
    28 from Tkinter import *
       
    29 import os
       
    30 
       
    31 class Dialog(Toplevel):
       
    32 
       
    33     '''Class to open dialogs.
       
    34 
       
    35     This class is intended as a base class for custom dialogs
       
    36     '''
       
    37 
       
    38     def __init__(self, parent, title = None):
       
    39 
       
    40         '''Initialize a dialog.
       
    41 
       
    42         Arguments:
       
    43 
       
    44             parent -- a parent window (the application window)
       
    45 
       
    46             title -- the dialog title
       
    47         '''
       
    48         Toplevel.__init__(self, parent)
       
    49 
       
    50         # If the master is not viewable, don't
       
    51         # make the child transient, or else it
       
    52         # would be opened withdrawn
       
    53         if parent.winfo_viewable():
       
    54             self.transient(parent)
       
    55 
       
    56         if title:
       
    57             self.title(title)
       
    58 
       
    59         self.parent = parent
       
    60 
       
    61         self.result = None
       
    62 
       
    63         body = Frame(self)
       
    64         self.initial_focus = self.body(body)
       
    65         body.pack(padx=5, pady=5)
       
    66 
       
    67         self.buttonbox()
       
    68 
       
    69         self.wait_visibility() # window needs to be visible for the grab
       
    70         self.grab_set()
       
    71 
       
    72         if not self.initial_focus:
       
    73             self.initial_focus = self
       
    74 
       
    75         self.protocol("WM_DELETE_WINDOW", self.cancel)
       
    76 
       
    77         if self.parent is not None:
       
    78             self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
       
    79                                       parent.winfo_rooty()+50))
       
    80 
       
    81         self.initial_focus.focus_set()
       
    82 
       
    83         self.wait_window(self)
       
    84 
       
    85     def destroy(self):
       
    86         '''Destroy the window'''
       
    87         self.initial_focus = None
       
    88         Toplevel.destroy(self)
       
    89 
       
    90     #
       
    91     # construction hooks
       
    92 
       
    93     def body(self, master):
       
    94         '''create dialog body.
       
    95 
       
    96         return widget that should have initial focus.
       
    97         This method should be overridden, and is called
       
    98         by the __init__ method.
       
    99         '''
       
   100         pass
       
   101 
       
   102     def buttonbox(self):
       
   103         '''add standard button box.
       
   104 
       
   105         override if you do not want the standard buttons
       
   106         '''
       
   107 
       
   108         box = Frame(self)
       
   109 
       
   110         w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
       
   111         w.pack(side=LEFT, padx=5, pady=5)
       
   112         w = Button(box, text="Cancel", width=10, command=self.cancel)
       
   113         w.pack(side=LEFT, padx=5, pady=5)
       
   114 
       
   115         self.bind("<Return>", self.ok)
       
   116         self.bind("<Escape>", self.cancel)
       
   117 
       
   118         box.pack()
       
   119 
       
   120     #
       
   121     # standard button semantics
       
   122 
       
   123     def ok(self, event=None):
       
   124 
       
   125         if not self.validate():
       
   126             self.initial_focus.focus_set() # put focus back
       
   127             return
       
   128 
       
   129         self.withdraw()
       
   130         self.update_idletasks()
       
   131 
       
   132         try:
       
   133             self.apply()
       
   134         finally:
       
   135             self.cancel()
       
   136 
       
   137     def cancel(self, event=None):
       
   138 
       
   139         # put focus back to the parent window
       
   140         if self.parent is not None:
       
   141             self.parent.focus_set()
       
   142         self.destroy()
       
   143 
       
   144     #
       
   145     # command hooks
       
   146 
       
   147     def validate(self):
       
   148         '''validate the data
       
   149 
       
   150         This method is called automatically to validate the data before the
       
   151         dialog is destroyed. By default, it always validates OK.
       
   152         '''
       
   153 
       
   154         return 1 # override
       
   155 
       
   156     def apply(self):
       
   157         '''process the data
       
   158 
       
   159         This method is called automatically to process the data, *after*
       
   160         the dialog is destroyed. By default, it does nothing.
       
   161         '''
       
   162 
       
   163         pass # override
       
   164 
       
   165 
       
   166 # --------------------------------------------------------------------
       
   167 # convenience dialogues
       
   168 
       
   169 class _QueryDialog(Dialog):
       
   170 
       
   171     def __init__(self, title, prompt,
       
   172                  initialvalue=None,
       
   173                  minvalue = None, maxvalue = None,
       
   174                  parent = None):
       
   175 
       
   176         if not parent:
       
   177             import Tkinter
       
   178             parent = Tkinter._default_root
       
   179 
       
   180         self.prompt   = prompt
       
   181         self.minvalue = minvalue
       
   182         self.maxvalue = maxvalue
       
   183 
       
   184         self.initialvalue = initialvalue
       
   185 
       
   186         Dialog.__init__(self, parent, title)
       
   187 
       
   188     def destroy(self):
       
   189         self.entry = None
       
   190         Dialog.destroy(self)
       
   191 
       
   192     def body(self, master):
       
   193 
       
   194         w = Label(master, text=self.prompt, justify=LEFT)
       
   195         w.grid(row=0, padx=5, sticky=W)
       
   196 
       
   197         self.entry = Entry(master, name="entry")
       
   198         self.entry.grid(row=1, padx=5, sticky=W+E)
       
   199 
       
   200         if self.initialvalue:
       
   201             self.entry.insert(0, self.initialvalue)
       
   202             self.entry.select_range(0, END)
       
   203 
       
   204         return self.entry
       
   205 
       
   206     def validate(self):
       
   207 
       
   208         import tkMessageBox
       
   209 
       
   210         try:
       
   211             result = self.getresult()
       
   212         except ValueError:
       
   213             tkMessageBox.showwarning(
       
   214                 "Illegal value",
       
   215                 self.errormessage + "\nPlease try again",
       
   216                 parent = self
       
   217             )
       
   218             return 0
       
   219 
       
   220         if self.minvalue is not None and result < self.minvalue:
       
   221             tkMessageBox.showwarning(
       
   222                 "Too small",
       
   223                 "The allowed minimum value is %s. "
       
   224                 "Please try again." % self.minvalue,
       
   225                 parent = self
       
   226             )
       
   227             return 0
       
   228 
       
   229         if self.maxvalue is not None and result > self.maxvalue:
       
   230             tkMessageBox.showwarning(
       
   231                 "Too large",
       
   232                 "The allowed maximum value is %s. "
       
   233                 "Please try again." % self.maxvalue,
       
   234                 parent = self
       
   235             )
       
   236             return 0
       
   237 
       
   238         self.result = result
       
   239 
       
   240         return 1
       
   241 
       
   242 
       
   243 class _QueryInteger(_QueryDialog):
       
   244     errormessage = "Not an integer."
       
   245     def getresult(self):
       
   246         return int(self.entry.get())
       
   247 
       
   248 def askinteger(title, prompt, **kw):
       
   249     '''get an integer from the user
       
   250 
       
   251     Arguments:
       
   252 
       
   253         title -- the dialog title
       
   254         prompt -- the label text
       
   255         **kw -- see SimpleDialog class
       
   256 
       
   257     Return value is an integer
       
   258     '''
       
   259     d = _QueryInteger(title, prompt, **kw)
       
   260     return d.result
       
   261 
       
   262 class _QueryFloat(_QueryDialog):
       
   263     errormessage = "Not a floating point value."
       
   264     def getresult(self):
       
   265         return float(self.entry.get())
       
   266 
       
   267 def askfloat(title, prompt, **kw):
       
   268     '''get a float from the user
       
   269 
       
   270     Arguments:
       
   271 
       
   272         title -- the dialog title
       
   273         prompt -- the label text
       
   274         **kw -- see SimpleDialog class
       
   275 
       
   276     Return value is a float
       
   277     '''
       
   278     d = _QueryFloat(title, prompt, **kw)
       
   279     return d.result
       
   280 
       
   281 class _QueryString(_QueryDialog):
       
   282     def __init__(self, *args, **kw):
       
   283         if kw.has_key("show"):
       
   284             self.__show = kw["show"]
       
   285             del kw["show"]
       
   286         else:
       
   287             self.__show = None
       
   288         _QueryDialog.__init__(self, *args, **kw)
       
   289 
       
   290     def body(self, master):
       
   291         entry = _QueryDialog.body(self, master)
       
   292         if self.__show is not None:
       
   293             entry.configure(show=self.__show)
       
   294         return entry
       
   295 
       
   296     def getresult(self):
       
   297         return self.entry.get()
       
   298 
       
   299 def askstring(title, prompt, **kw):
       
   300     '''get a string from the user
       
   301 
       
   302     Arguments:
       
   303 
       
   304         title -- the dialog title
       
   305         prompt -- the label text
       
   306         **kw -- see SimpleDialog class
       
   307 
       
   308     Return value is a string
       
   309     '''
       
   310     d = _QueryString(title, prompt, **kw)
       
   311     return d.result
       
   312 
       
   313 if __name__ == "__main__":
       
   314 
       
   315     root = Tk()
       
   316     root.update()
       
   317 
       
   318     print askinteger("Spam", "Egg count", initialvalue=12*12)
       
   319     print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100)
       
   320     print askstring("Spam", "Egg label")