mirror of
https://github.com/python/cpython.git
synced 2025-07-23 03:05:38 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ........ r53545 | andrew.kuchling | 2007-01-24 21:06:41 +0100 (Wed, 24 Jan 2007) | 1 line Strengthen warning about using lock() ........ r53556 | thomas.heller | 2007-01-25 19:34:14 +0100 (Thu, 25 Jan 2007) | 3 lines Fix for #1643874: When calling SysAllocString, create a PyCObject which will eventually call SysFreeString to free the BSTR resource. ........ r53563 | andrew.kuchling | 2007-01-25 21:02:13 +0100 (Thu, 25 Jan 2007) | 1 line Add item ........ r53564 | brett.cannon | 2007-01-25 21:22:02 +0100 (Thu, 25 Jan 2007) | 8 lines Fix time.strptime's %U support. Basically rewrote the algorithm to be more generic so that one only has to shift certain values based on whether the week was specified to start on Monday or Sunday. Cut out a lot of edge case code compared to the previous version. Also broke algorithm out into its own function (that is private to the module). Fixes bug #1643943 (thanks Biran Nahas for the report). ........ r53570 | brett.cannon | 2007-01-26 00:30:39 +0100 (Fri, 26 Jan 2007) | 4 lines Remove specific mention of my name and email address from modules. Not really needed and all bug reports should go to the bug tracker, not directly to me. Plus I am not the only person to have edited these files at this point. ........ r53573 | fred.drake | 2007-01-26 17:28:44 +0100 (Fri, 26 Jan 2007) | 1 line fix typo (extraneous ")") ........ r53575 | georg.brandl | 2007-01-27 18:43:02 +0100 (Sat, 27 Jan 2007) | 4 lines Patch #1638243: the compiler package is now able to correctly compile a with statement; previously, executing code containing a with statement compiled by the compiler package crashed the interpreter. ........ r53578 | georg.brandl | 2007-01-27 18:59:42 +0100 (Sat, 27 Jan 2007) | 3 lines Patch #1634778: add missing encoding aliases for iso8859_15 and iso8859_16. ........ r53579 | georg.brandl | 2007-01-27 20:38:50 +0100 (Sat, 27 Jan 2007) | 2 lines Bug #1645944: os.access now returns bool but docstring is not updated ........ r53590 | brett.cannon | 2007-01-28 21:58:00 +0100 (Sun, 28 Jan 2007) | 2 lines Use the thread lock's context manager instead of a try/finally statement. ........ r53591 | brett.cannon | 2007-01-29 05:41:44 +0100 (Mon, 29 Jan 2007) | 2 lines Add a test for slicing an exception. ........ r53594 | andrew.kuchling | 2007-01-29 21:21:43 +0100 (Mon, 29 Jan 2007) | 1 line Minor edits to the curses HOWTO ........ r53596 | andrew.kuchling | 2007-01-29 21:55:40 +0100 (Mon, 29 Jan 2007) | 1 line Various minor edits ........ r53597 | andrew.kuchling | 2007-01-29 22:28:48 +0100 (Mon, 29 Jan 2007) | 1 line More edits ........ r53601 | tim.peters | 2007-01-30 04:03:46 +0100 (Tue, 30 Jan 2007) | 2 lines Whitespace normalization. ........ r53603 | georg.brandl | 2007-01-30 21:21:30 +0100 (Tue, 30 Jan 2007) | 2 lines Bug #1648191: typo in docs. ........ r53605 | brett.cannon | 2007-01-30 22:34:36 +0100 (Tue, 30 Jan 2007) | 8 lines No more raising of string exceptions! The next step of PEP 352 (for 2.6) causes raising a string exception to trigger a TypeError. Trying to catch a string exception raises a DeprecationWarning. References to string exceptions has been removed from the docs since they are now just an error. ........ r53618 | raymond.hettinger | 2007-02-01 22:02:59 +0100 (Thu, 01 Feb 2007) | 1 line Bug #1648179: set.update() not recognizing __iter__ overrides in dict subclasses. ........
320 lines
7.4 KiB
Python
320 lines
7.4 KiB
Python
#
|
|
# 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
|
|
'''
|
|
|
|
from Tkinter import *
|
|
import os
|
|
|
|
class Dialog(Toplevel):
|
|
|
|
'''Class to open dialogs.
|
|
|
|
This class is intended as a base class for custom dialogs
|
|
'''
|
|
|
|
def __init__(self, parent, title = None):
|
|
|
|
'''Initialize a dialog.
|
|
|
|
Arguments:
|
|
|
|
parent -- a parent window (the application window)
|
|
|
|
title -- the dialog title
|
|
'''
|
|
Toplevel.__init__(self, parent)
|
|
|
|
# If the master is not viewable, don't
|
|
# make the child transient, or else it
|
|
# would be opened withdrawn
|
|
if parent.winfo_viewable():
|
|
self.transient(parent)
|
|
|
|
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
|
|
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))
|
|
|
|
self.initial_focus.focus_set()
|
|
|
|
self.wait_window(self)
|
|
|
|
def destroy(self):
|
|
'''Destroy the window'''
|
|
self.initial_focus = None
|
|
Toplevel.destroy(self)
|
|
|
|
#
|
|
# construction hooks
|
|
|
|
def body(self, master):
|
|
'''create dialog body.
|
|
|
|
return widget that should have initial focus.
|
|
This method should be overridden, and is called
|
|
by the __init__ method.
|
|
'''
|
|
pass
|
|
|
|
def buttonbox(self):
|
|
'''add standard button box.
|
|
|
|
override if you do not want the standard buttons
|
|
'''
|
|
|
|
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()
|
|
|
|
def cancel(self, event=None):
|
|
|
|
# put focus back to the parent window
|
|
if self.parent is not None:
|
|
self.parent.focus_set()
|
|
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.
|
|
'''
|
|
|
|
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.
|
|
'''
|
|
|
|
pass # override
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
# convenience dialogues
|
|
|
|
class _QueryDialog(Dialog):
|
|
|
|
def __init__(self, title, prompt,
|
|
initialvalue=None,
|
|
minvalue = None, maxvalue = None,
|
|
parent = None):
|
|
|
|
if not parent:
|
|
import Tkinter
|
|
parent = Tkinter._default_root
|
|
|
|
self.prompt = prompt
|
|
self.minvalue = minvalue
|
|
self.maxvalue = maxvalue
|
|
|
|
self.initialvalue = initialvalue
|
|
|
|
Dialog.__init__(self, parent, title)
|
|
|
|
def destroy(self):
|
|
self.entry = None
|
|
Dialog.destroy(self)
|
|
|
|
def body(self, master):
|
|
|
|
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)
|
|
|
|
if self.initialvalue:
|
|
self.entry.insert(0, self.initialvalue)
|
|
self.entry.select_range(0, END)
|
|
|
|
return self.entry
|
|
|
|
def validate(self):
|
|
|
|
import tkMessageBox
|
|
|
|
try:
|
|
result = self.getresult()
|
|
except ValueError:
|
|
tkMessageBox.showwarning(
|
|
"Illegal value",
|
|
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. "
|
|
"Please try again." % self.minvalue,
|
|
parent = self
|
|
)
|
|
return 0
|
|
|
|
if self.maxvalue is not None and result > self.maxvalue:
|
|
tkMessageBox.showwarning(
|
|
"Too large",
|
|
"The allowed maximum value is %s. "
|
|
"Please try again." % self.maxvalue,
|
|
parent = self
|
|
)
|
|
return 0
|
|
|
|
self.result = result
|
|
|
|
return 1
|
|
|
|
|
|
class _QueryInteger(_QueryDialog):
|
|
errormessage = "Not an integer."
|
|
def getresult(self):
|
|
return int(self.entry.get())
|
|
|
|
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
|
|
'''
|
|
d = _QueryInteger(title, prompt, **kw)
|
|
return d.result
|
|
|
|
class _QueryFloat(_QueryDialog):
|
|
errormessage = "Not a floating point value."
|
|
def getresult(self):
|
|
return float(self.entry.get())
|
|
|
|
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
|
|
'''
|
|
d = _QueryFloat(title, prompt, **kw)
|
|
return d.result
|
|
|
|
class _QueryString(_QueryDialog):
|
|
def __init__(self, *args, **kw):
|
|
if "show" in kw:
|
|
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
|
|
|
|
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
|
|
'''
|
|
d = _QueryString(title, prompt, **kw)
|
|
return d.result
|
|
|
|
if __name__ == "__main__":
|
|
|
|
root = Tk()
|
|
root.update()
|
|
|
|
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")
|