4d11ce0209481f57e1e9c20aa5094e9e0037a085
# An Introduction to Tkinter
# Copyright (c) 1997 by Fredrik Lundh
# http://www.pythonware.com
# --------------------------------------------------------------------
This module handles dialog boxes. It contains the following
Dialog -- a base class for dialogs
askinteger -- get an integer from the user
askfloat -- get a float from the user
askstring -- get a string from the user
'''Class to open dialogs.
This class is intended as a base class for custom dialogs
def __init__(self
, parent
, title
= None):
parent -- a parent window (the application window)
title -- the dialog title
Toplevel
.__init
__(self
, parent
)
self
.initial_focus
= self
.body(body
)
body
.pack(padx
=5, pady
=5)
self
.wait_visibility() # window needs to be visible for the grab
if not self
.initial_focus
:
self
.initial_focus
= self
self
.protocol("WM_DELETE_WINDOW", self
.cancel
)
if self
.parent
is not None:
self
.geometry("+%d+%d" % (parent
.winfo_rootx()+50,
parent
.winfo_rooty()+50))
self
.initial_focus
.focus_set()
self
.initial_focus
= None
return widget that should have initial focus.
This method should be overridden, and is called
'''add standard button box.
override if you do not want the standard buttons
w
= Button(box
, text
="OK", width
=10, command
=self
.ok
, default
=ACTIVE
)
w
.pack(side
=LEFT
, padx
=5, pady
=5)
w
= Button(box
, text
="Cancel", width
=10, command
=self
.cancel
)
w
.pack(side
=LEFT
, padx
=5, pady
=5)
self
.bind("<Return>", self
.ok
)
self
.bind("<Escape>", self
.cancel
)
# standard button semantics
def ok(self
, event
=None):
self
.initial_focus
.focus_set() # put focus back
def cancel(self
, event
=None):
# put focus back to the parent window
if self
.parent
is not None:
This method is called automatically to validate the data before the
dialog is destroyed. By default, it always validates OK.
This method is called automatically to process the data, *after*
the dialog is destroyed. By default, it does nothing.
# --------------------------------------------------------------------
class _QueryDialog(Dialog
):
def __init__(self
, title
, prompt
,
minvalue
= None, maxvalue
= None,
parent
= Tkinter
._default
_root
self
.initialvalue
= initialvalue
Dialog
.__init
__(self
, parent
, title
)
w
= Label(master
, text
=self
.prompt
, justify
=LEFT
)
w
.grid(row
=0, padx
=5, sticky
=W
)
self
.entry
= Entry(master
, name
="entry")
self
.entry
.grid(row
=1, padx
=5, sticky
=W
+E
)
self
.entry
.insert(0, self
.initialvalue
)
self
.entry
.select_range(0, END
)
result
= self
.getresult()
tkMessageBox
.showwarning(
self
.errormessage
+ "\nPlease try again",
if self
.minvalue
is not None and result
< self
.minvalue
:
tkMessageBox
.showwarning(
"The allowed minimum value is %s. "
"Please try again." % self
.minvalue
,
if self
.maxvalue
is not None and result
> self
.maxvalue
:
tkMessageBox
.showwarning(
"The allowed maximum value is %s. "
"Please try again." % self
.maxvalue
,
class _QueryInteger(_QueryDialog
):
errormessage
= "Not an integer."
return int(self
.entry
.get())
def askinteger(title
, prompt
, **kw
):
'''get an integer from the user
title -- the dialog title
**kw -- see SimpleDialog class
Return value is an integer
d
= _QueryInteger(title
, prompt
, **kw
)
class _QueryFloat(_QueryDialog
):
errormessage
= "Not a floating point value."
return float(self
.entry
.get())
def askfloat(title
, prompt
, **kw
):
'''get a float from the user
title -- the dialog title
**kw -- see SimpleDialog class
d
= _QueryFloat(title
, prompt
, **kw
)
class _QueryString(_QueryDialog
):
def __init__(self
, *args
, **kw
):
_QueryDialog
.__init
__(self
, *args
, **kw
)
entry
= _QueryDialog
.body(self
, master
)
if self
.__show
is not None:
entry
.configure(show
=self
.__show
)
def askstring(title
, prompt
, **kw
):
'''get a string from the user
title -- the dialog title
**kw -- see SimpleDialog class
d
= _QueryString(title
, prompt
, **kw
)
if __name__
== "__main__":
print askinteger("Spam", "Egg count", initialvalue
=12*12)
print askfloat("Spam", "Egg weight\n(in tons)", minvalue
=1, maxvalue
=100)
print askstring("Spam", "Egg label")