cpython/Lib/lib-tk/tkSimpleDialog.py

321 lines
7.4 KiB
Python
Raw Normal View History

1997-08-12 15:21:21 -03:00
#
# An Introduction to Tkinter
# tkSimpleDialog.py
#
# Copyright (c) 1997 by Fredrik Lundh
#
# fredrik@pythonware.com
# http://www.pythonware.com
#
# --------------------------------------------------------------------
# dialog base class
'''Dialog boxes
This module handles dialog boxes. It contains the following
public symbols:
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
'''
1997-08-12 15:21:21 -03:00
from Tkinter import *
import os
class Dialog(Toplevel):
'''Class to open dialogs.
This class is intended as a base class for custom dialogs
'''
1997-08-12 15:21:21 -03:00
def __init__(self, parent, title = None):
'''Initialize a dialog.
Arguments:
parent -- a parent window (the application window)
title -- the dialog title
'''
1997-08-12 15:21:21 -03:00
Toplevel.__init__(self, parent)
# If the master is not viewable, don't
# make the child transient, or else it
# would be opened withdrawn
2007-04-25 03:42:41 -03:00
if parent.winfo_viewable():
self.transient(parent)
2007-04-25 03:42:41 -03:00
1997-08-12 15:21:21 -03:00
if title:
self.title(title)
self.parent = parent
self.result = None
body = Frame(self)
self.initial_focus = self.body(body)
body.pack(padx=5, pady=5)
self.buttonbox()
self.wait_visibility() # window needs to be visible for the grab
1997-08-12 15:21:21 -03:00
self.grab_set()
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))
1997-08-12 15:21:21 -03:00
self.initial_focus.focus_set()
self.wait_window(self)
def destroy(self):
'''Destroy the window'''
self.initial_focus = None
Toplevel.destroy(self)
1997-08-12 15:21:21 -03:00
#
# construction hooks
def body(self, master):
'''create dialog body.
1997-08-12 15:21:21 -03:00
return widget that should have initial focus.
This method should be overridden, and is called
by the __init__ method.
'''
1997-08-12 15:21:21 -03:00
pass
def buttonbox(self):
'''add standard button box.
override if you do not want the standard buttons
'''
1997-08-12 15:21:21 -03:00
box = Frame(self)
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)
box.pack()
#
# standard button semantics
def ok(self, event=None):
if not self.validate():
self.initial_focus.focus_set() # put focus back
return
self.withdraw()
self.update_idletasks()
try:
self.apply()
finally:
self.cancel()
1997-08-12 15:21:21 -03:00
def cancel(self, event=None):
# put focus back to the parent window
if self.parent is not None:
self.parent.focus_set()
1997-08-12 15:21:21 -03:00
self.destroy()
#
# command hooks
def validate(self):
'''validate the data
This method is called automatically to validate the data before the
dialog is destroyed. By default, it always validates OK.
'''
1997-08-12 15:21:21 -03:00
return 1 # override
def apply(self):
'''process the data
This method is called automatically to process the data, *after*
the dialog is destroyed. By default, it does nothing.
'''
1997-08-12 15:21:21 -03:00
pass # override
# --------------------------------------------------------------------
# convenience dialogues
class _QueryDialog(Dialog):
def __init__(self, title, prompt,
1997-08-14 11:17:28 -03:00
initialvalue=None,
1997-08-12 15:21:21 -03:00
minvalue = None, maxvalue = None,
parent = None):
if not parent:
import Tkinter
parent = Tkinter._default_root
1997-08-12 15:21:21 -03:00
self.prompt = prompt
self.minvalue = minvalue
self.maxvalue = maxvalue
1997-08-14 11:17:28 -03:00
self.initialvalue = initialvalue
1997-08-12 15:21:21 -03:00
Dialog.__init__(self, parent, title)
def destroy(self):
self.entry = None
Dialog.destroy(self)
1997-08-12 15:21:21 -03:00
def body(self, master):
1997-08-14 11:17:28 -03:00
w = Label(master, text=self.prompt, justify=LEFT)
1997-08-12 15:21:21 -03:00
w.grid(row=0, padx=5, sticky=W)
self.entry = Entry(master, name="entry")
self.entry.grid(row=1, padx=5, sticky=W+E)
1997-08-14 11:17:28 -03:00
if self.initialvalue:
self.entry.insert(0, self.initialvalue)
self.entry.select_range(0, END)
1997-08-12 15:21:21 -03:00
return self.entry
def validate(self):
import tkMessageBox
try:
result = self.getresult()
except ValueError:
tkMessageBox.showwarning(
1997-08-14 11:17:28 -03:00
"Illegal value",
1997-08-12 15:21:21 -03:00
self.errormessage + "\nPlease try again",
parent = self
)
return 0
if self.minvalue is not None and result < self.minvalue:
tkMessageBox.showwarning(
"Too small",
"The allowed minimum value is %s. "
1997-08-14 11:17:28 -03:00
"Please try again." % self.minvalue,
1997-08-12 15:21:21 -03:00
parent = self
)
return 0
if self.maxvalue is not None and result > self.maxvalue:
tkMessageBox.showwarning(
1997-08-14 11:17:28 -03:00
"Too large",
1997-08-12 15:21:21 -03:00
"The allowed maximum value is %s. "
1997-08-14 11:17:28 -03:00
"Please try again." % self.maxvalue,
1997-08-12 15:21:21 -03:00
parent = self
)
return 0
1997-08-12 15:21:21 -03:00
self.result = result
return 1
class _QueryInteger(_QueryDialog):
1997-08-14 11:17:28 -03:00
errormessage = "Not an integer."
1997-08-12 15:21:21 -03:00
def getresult(self):
2001-02-09 07:51:27 -04:00
return int(self.entry.get())
1997-08-12 15:21:21 -03:00
def askinteger(title, prompt, **kw):
'''get an integer from the user
Arguments:
title -- the dialog title
prompt -- the label text
**kw -- see SimpleDialog class
Return value is an integer
'''
2003-04-06 06:01:11 -03:00
d = _QueryInteger(title, prompt, **kw)
1997-08-12 15:21:21 -03:00
return d.result
class _QueryFloat(_QueryDialog):
1997-08-14 11:17:28 -03:00
errormessage = "Not a floating point value."
1997-08-12 15:21:21 -03:00
def getresult(self):
2001-02-09 07:51:27 -04:00
return float(self.entry.get())
1997-08-12 15:21:21 -03:00
def askfloat(title, prompt, **kw):
'''get a float from the user
Arguments:
title -- the dialog title
prompt -- the label text
**kw -- see SimpleDialog class
Return value is a float
'''
2003-04-06 06:01:11 -03:00
d = _QueryFloat(title, prompt, **kw)
1997-08-12 15:21:21 -03:00
return d.result
class _QueryString(_QueryDialog):
def __init__(self, *args, **kw):
if kw.has_key("show"):
self.__show = kw["show"]
del kw["show"]
else:
self.__show = None
_QueryDialog.__init__(self, *args, **kw)
def body(self, master):
entry = _QueryDialog.body(self, master)
if self.__show is not None:
entry.configure(show=self.__show)
return entry
1997-08-12 15:21:21 -03:00
def getresult(self):
return self.entry.get()
def askstring(title, prompt, **kw):
'''get a string from the user
Arguments:
title -- the dialog title
prompt -- the label text
**kw -- see SimpleDialog class
Return value is a string
'''
2003-04-06 06:01:11 -03:00
d = _QueryString(title, prompt, **kw)
1997-08-12 15:21:21 -03:00
return d.result
if __name__ == "__main__":
1997-08-12 15:21:21 -03:00
root = Tk()
root.update()
1997-08-14 11:17:28 -03:00
print askinteger("Spam", "Egg count", initialvalue=12*12)
print askfloat("Spam", "Egg weight\n(in tons)", minvalue=1, maxvalue=100)
1997-08-12 15:21:21 -03:00
print askstring("Spam", "Egg label")