<s> '''<STR_LIT>''' <EOL> from Tkinter import * <EOL> class Dialog ( Toplevel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , parent , title = None ) : <EOL> '''<STR_LIT>''' <EOL> Toplevel . __init__ ( self , parent ) <EOL> self . withdraw ( ) <EOL> if parent . winfo_viewable ( ) : <EOL> self . transient ( parent ) <EOL> if title : <EOL> self . title ( title ) <EOL> self . parent = parent <EOL> self . result = None <EOL> body = Frame ( self ) <EOL> self . initial_focus = self . body ( body ) <EOL> body . pack ( padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> self . buttonbox ( ) <EOL> if not self . initial_focus : <EOL> self . initial_focus = self <EOL> self . protocol ( "<STR_LIT>" , self . cancel ) <EOL> if self . parent is not None : <EOL> self . geometry ( "<STR_LIT>" % ( parent . winfo_rootx ( ) + <NUM_LIT:50> , <EOL> parent . winfo_rooty ( ) + <NUM_LIT:50> ) ) <EOL> self . deiconify ( ) <EOL> self . initial_focus . focus_set ( ) <EOL> self . wait_visibility ( ) <EOL> self . grab_set ( ) <EOL> self . wait_window ( self ) <EOL> def destroy ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . initial_focus = None <EOL> Toplevel . destroy ( self ) <EOL> def body ( self , master ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def buttonbox ( self ) : <EOL> '''<STR_LIT>''' <EOL> box = Frame ( self ) <EOL> w = Button ( box , text = "<STR_LIT:OK>" , width = <NUM_LIT:10> , command = self . ok , default = ACTIVE ) <EOL> w . pack ( side = LEFT , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> w = Button ( box , text = "<STR_LIT>" , width = <NUM_LIT:10> , command = self . cancel ) <EOL> w . pack ( side = LEFT , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> self . bind ( "<STR_LIT>" , self . ok ) <EOL> self . bind ( "<STR_LIT>" , self . cancel ) <EOL> box . pack ( ) <EOL> def ok ( self , event = None ) : <EOL> if not self . validate ( ) : <EOL> self . initial_focus . focus_set ( ) <EOL> return <EOL> self . withdraw ( ) <EOL> self . update_idletasks ( ) <EOL> try : <EOL> self . apply ( ) <EOL> finally : <EOL> self . cancel ( ) <EOL> def cancel ( self , event = None ) : <EOL> if self . parent is not None : <EOL> self . parent . focus_set ( ) <EOL> self . destroy ( ) <EOL> def validate ( self ) : <EOL> '''<STR_LIT>''' <EOL> return <NUM_LIT:1> <EOL> def apply ( self ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> class _QueryDialog ( Dialog ) : <EOL> def __init__ ( self , title , prompt , <EOL> initialvalue = None , <EOL> minvalue = None , maxvalue = None , <EOL> parent = None ) : <EOL> if not parent : <EOL> import Tkinter <EOL> parent = Tkinter . _default_root <EOL> self . prompt = prompt <EOL> self . minvalue = minvalue <EOL> self . maxvalue = maxvalue <EOL> self . initialvalue = initialvalue <EOL> Dialog . __init__ ( self , parent , title ) <EOL> def destroy ( self ) : <EOL> self . entry = None <EOL> Dialog . destroy ( self ) <EOL> def body ( self , master ) : <EOL> w = Label ( master , text = self . prompt , justify = LEFT ) <EOL> w . grid ( row = <NUM_LIT:0> , padx = <NUM_LIT:5> , sticky = W ) <EOL> self . entry = Entry ( master , name = "<STR_LIT>" ) <EOL> self . entry . grid ( row = <NUM_LIT:1> , padx = <NUM_LIT:5> , sticky = W + E ) <EOL> if self . initialvalue is not None : <EOL> self . entry . insert ( <NUM_LIT:0> , self . initialvalue ) <EOL> self . entry . select_range ( <NUM_LIT:0> , END ) <EOL> return self . entry <EOL> def validate ( self ) : <EOL> import tkMessageBox <EOL> try : <EOL> result = self . getresult ( ) <EOL> except ValueError : <EOL> tkMessageBox . showwarning ( <EOL> "<STR_LIT>" , <EOL> self . errormessage + "<STR_LIT>" , <EOL> parent = self <EOL> ) <EOL> return <NUM_LIT:0> <EOL> if self . minvalue is not None and result < self . minvalue : <EOL> tkMessageBox . showwarning ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . minvalue , <EOL> parent = self <EOL> ) <EOL> return <NUM_LIT:0> <EOL> if self . maxvalue is not None and result > self . maxvalue : <EOL> tkMessageBox . showwarning ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . maxvalue , <EOL> parent = self <EOL> ) <EOL> return <NUM_LIT:0> <EOL> self . result = result <EOL> return <NUM_LIT:1> <EOL> class _QueryInteger ( _QueryDialog ) : <EOL> errormessage = "<STR_LIT>" <EOL> def getresult ( self ) : <EOL> return int ( self . entry . get ( ) ) <EOL> def askinteger ( title , prompt , ** kw ) : <EOL> '''<STR_LIT>''' <EOL> d = _QueryInteger ( title , prompt , ** kw ) <EOL> return d . result <EOL> class _QueryFloat ( _QueryDialog ) : <EOL> errormessage = "<STR_LIT>" <EOL> def getresult ( self ) : <EOL> return float ( self . entry . get ( ) ) <EOL> def askfloat ( title , prompt , ** kw ) : <EOL> '''<STR_LIT>''' <EOL> d = _QueryFloat ( title , prompt , ** kw ) <EOL> return d . result <EOL> class _QueryString ( _QueryDialog ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> if "<STR_LIT>" in kw : <EOL> self . __show = kw [ "<STR_LIT>" ] <EOL> del kw [ "<STR_LIT>" ] <EOL> else : <EOL> self . __show = None <EOL> _QueryDialog . __init__ ( self , * args , ** kw ) <EOL> def body ( self , master ) : <EOL> entry = _QueryDialog . body ( self , master ) <EOL> if self . __show is not None : <EOL> entry . configure ( show = self . __show ) <EOL> return entry <EOL> def getresult ( self ) : <EOL> return self . entry . get ( ) <EOL> def askstring ( title , prompt , ** kw ) : <EOL> '''<STR_LIT>''' <EOL> d = _QueryString ( title , prompt , ** kw ) <EOL> return d . result <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> root = Tk ( ) <EOL> root . update ( ) <EOL> print askinteger ( "<STR_LIT>" , "<STR_LIT>" , initialvalue = <NUM_LIT:12> * <NUM_LIT:12> ) <EOL> print askfloat ( "<STR_LIT>" , "<STR_LIT>" , minvalue = <NUM_LIT:1> , maxvalue = <NUM_LIT:100> ) <EOL> print askstring ( "<STR_LIT>" , "<STR_LIT>" ) </s>
