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
|
|
|
|
|
2000-02-24 11:01:43 -04:00
|
|
|
'''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):
|
|
|
|
|
2000-02-24 11:01:43 -04:00
|
|
|
'''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):
|
|
|
|
|
2000-02-24 11:01:43 -04:00
|
|
|
'''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)
|
|
|
|
|
2006-11-18 14:00:34 -04:00
|
|
|
# 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():
|
2006-11-18 14:00:34 -04:00
|
|
|
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()
|
|
|
|
|
2004-08-03 15:36:25 -03:00
|
|
|
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)
|
|
|
|
|
2001-12-06 12:51:41 -04:00
|
|
|
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)
|
|
|
|
|
1999-06-25 12:53:54 -03:00
|
|
|
def destroy(self):
|
2000-02-24 11:01:43 -04:00
|
|
|
'''Destroy the window'''
|
1999-06-25 12:53:54 -03:00
|
|
|
self.initial_focus = None
|
|
|
|
Toplevel.destroy(self)
|
|
|
|
|
1997-08-12 15:21:21 -03:00
|
|
|
#
|
|
|
|
# construction hooks
|
|
|
|
|
|
|
|
def body(self, master):
|
2000-02-24 11:01:43 -04:00
|
|
|
'''create dialog body.
|
1997-08-12 15:21:21 -03:00
|
|
|
|
2000-10-23 15:31:14 -03:00
|
|
|
return widget that should have initial focus.
|
2000-02-24 11:01:43 -04:00
|
|
|
This method should be overridden, and is called
|
|
|
|
by the __init__ method.
|
|
|
|
'''
|
1997-08-12 15:21:21 -03:00
|
|
|
pass
|
|
|
|
|
|
|
|
def buttonbox(self):
|
2000-10-23 15:31:14 -03:00
|
|
|
'''add standard button box.
|
2000-02-24 11:01:43 -04:00
|
|
|
|
2001-12-06 12:51:41 -04:00
|
|
|
override if you do not want the standard buttons
|
2000-02-24 11:01:43 -04:00
|
|
|
'''
|
2000-10-23 15:31:14 -03:00
|
|
|
|
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()
|
|
|
|
|
2006-11-18 14:05:57 -04:00
|
|
|
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
|
2001-12-06 12:51:41 -04:00
|
|
|
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):
|
2000-02-24 11:01:43 -04:00
|
|
|
'''validate the data
|
|
|
|
|
2000-10-23 15:31:14 -03:00
|
|
|
This method is called automatically to validate the data before the
|
2000-02-24 11:01:43 -04:00
|
|
|
dialog is destroyed. By default, it always validates OK.
|
|
|
|
'''
|
1997-08-12 15:21:21 -03:00
|
|
|
|
|
|
|
return 1 # override
|
|
|
|
|
|
|
|
def apply(self):
|
2000-02-24 11:01:43 -04:00
|
|
|
'''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:
|
1998-10-12 17:40:09 -03:00
|
|
|
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)
|
|
|
|
|
1999-06-25 12:53:54 -03:00
|
|
|
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
|
2000-10-23 15:31:14 -03:00
|
|
|
|
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):
|
2000-02-24 11:01:43 -04:00
|
|
|
'''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):
|
2000-02-24 11:01:43 -04:00
|
|
|
'''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):
|
2001-12-06 12:51:41 -04:00
|
|
|
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):
|
2000-02-24 11:01:43 -04:00
|
|
|
'''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
|
|
|
|
|
2000-10-23 15:31:14 -03:00
|
|
|
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")
|