Skip to content

Commit dbce6d0

Browse files
author
guido
committed
New dialog routines (Fred Lundh)
git-svn-id: http://svn.python.org/projects/python/trunk@8346 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 53d984e commit dbce6d0

8 files changed

Lines changed: 722 additions & 0 deletions

File tree

Lib/lib-tk/tkColorChooser.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#
2+
# Instant Python
3+
# $Id$
4+
#
5+
# tk common colour chooser dialogue
6+
#
7+
# this module provides an interface to the native color dialogue
8+
# available in Tk 4.2 and newer.
9+
#
10+
# written by Fredrik Lundh, May 1997
11+
#
12+
13+
#
14+
# options (all have default values):
15+
#
16+
# - initialcolor: colour to mark as selected when dialog is displayed
17+
# (given as an RGB triplet or a Tk color string)
18+
#
19+
# - parent: which window to place the dialog on top of
20+
#
21+
# - title: dialog title
22+
#
23+
24+
# FIXME: as of Tk 8.0a2, the Unix colour picker is really ugly, and
25+
# doesn't seem to work properly on a true colour display. maybe we
26+
# should use the instant python version instead?
27+
28+
from tkCommonDialog import Dialog
29+
30+
31+
#
32+
# color chooser class
33+
34+
class Chooser(Dialog):
35+
"Ask for a color"
36+
37+
command = "tk_chooseColor"
38+
39+
def _fixoptions(self):
40+
try:
41+
# make sure initialcolor is a tk color string
42+
color = self.options["initialcolor"]
43+
if type(color) == type(()):
44+
# assume an RGB triplet
45+
self.options["initialcolor"] = "%02x%02x%02x" % color
46+
except KeyError:
47+
pass
48+
49+
def _fixresult(self, widget, result):
50+
# to simplify application code, the color chooser returns
51+
# an RGB tuple together with the Tk color string
52+
if not result:
53+
return None, None # cancelled
54+
r, g, b = widget.winfo_rgb(result)
55+
return (r/256, g/256, b/256), result
56+
57+
58+
#
59+
# convenience stuff
60+
61+
def askcolor(color = None, **options):
62+
"Ask for a color"
63+
64+
return apply(Chooser, (), options).show()
65+
66+
67+
# --------------------------------------------------------------------
68+
# test stuff
69+
70+
if __name__ == "__main__":
71+
72+
print "color", askcolor()

Lib/lib-tk/tkCommonDialog.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#
2+
# Instant Python
3+
# $Id$
4+
#
5+
# base class for tk common dialogues
6+
#
7+
# this module provides a base class for accessing the common
8+
# dialogues available in Tk 4.2 and newer. use tkFileDialog,
9+
# tkColorChooser, and tkMessageBox to access the individual
10+
# dialogs.
11+
#
12+
# written by Fredrik Lundh, May 1997
13+
#
14+
15+
from Tkinter import *
16+
import os
17+
18+
class Dialog:
19+
20+
command = None
21+
22+
def __init__(self, master=None, **options):
23+
24+
# FIXME: should this be placed on the module level instead?
25+
if TkVersion < 4.2:
26+
raise TclError, "this module requires Tk 4.2 or newer"
27+
28+
self.master = master
29+
self.options = options
30+
31+
def _fixoptions(self):
32+
pass # hook
33+
34+
def _fixresult(self, widget, result):
35+
return result # hook
36+
37+
def show(self, **options):
38+
39+
# update instance options
40+
for k, v in options.items():
41+
self.options[k] = v
42+
43+
self._fixoptions()
44+
45+
# we need a stub widget to properly process the options
46+
# (at least as long as we use Tkinter 1.63)
47+
w = Frame(self.master)
48+
49+
try:
50+
51+
s = apply(w.tk.call, (self.command,) + w._options(self.options))
52+
53+
s = self._fixresult(w, s)
54+
55+
finally:
56+
57+
try:
58+
# get rid of the widget
59+
w.destroy()
60+
except:
61+
pass
62+
63+
return s

Lib/lib-tk/tkFileDialog.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#
2+
# Instant Python
3+
# $Id$
4+
#
5+
# tk common file dialogues
6+
#
7+
# this module provides interfaces to the native file dialogues
8+
# available in Tk 4.2 and newer.
9+
#
10+
# written by Fredrik Lundh, May 1997.
11+
#
12+
13+
#
14+
# options (all have default values):
15+
#
16+
# - defaultextension: added to filename if not explicitly given
17+
#
18+
# - filetypes: sequence of (label, pattern) tuples. the same pattern
19+
# may occur with several patterns. use "*" as pattern to indicate
20+
# all files.
21+
#
22+
# - initialdir: initial directory. preserved by dialog instance.
23+
#
24+
# - initialfile: initial file (ignored by the open dialog). preserved
25+
# by dialog instance.
26+
#
27+
# - parent: which window to place the dialog on top of
28+
#
29+
# - title: dialog title
30+
#
31+
32+
from tkCommonDialog import Dialog
33+
34+
class _Dialog(Dialog):
35+
36+
def _fixoptions(self):
37+
try:
38+
# make sure "filetypes" is a tuple
39+
self.options["filetypes"] = tuple(self.options["filetypes"])
40+
except KeyError:
41+
pass
42+
43+
def _fixresult(self, widget, result):
44+
if result:
45+
# keep directory and filename until next time
46+
import os
47+
path, file = os.path.split(result)
48+
self.options["initialdir"] = path
49+
self.options["initialfile"] = file
50+
self.filename = result # compatibility
51+
return result
52+
53+
54+
#
55+
# file dialogs
56+
57+
class Open(_Dialog):
58+
"Ask for a filename to open"
59+
60+
command = "tk_getOpenFile"
61+
62+
class SaveAs(_Dialog):
63+
"Ask for a filename to save as"
64+
65+
command = "tk_getSaveFile"
66+
67+
68+
#
69+
# convenience stuff
70+
71+
def askopenfilename(**options):
72+
"Ask for a filename to open"
73+
74+
return apply(Open, (), options).show()
75+
76+
def asksaveasfilename(**options):
77+
"Ask for a filename to save as"
78+
79+
return apply(SaveAs, (), options).show()
80+
81+
# FIXME: are the following two perhaps a bit too convenient?
82+
83+
def askopenfile(mode = "r", **options):
84+
"Ask for a filename to open, and returned the opened file"
85+
86+
filename = apply(Open, (), options).show()
87+
if filename:
88+
return open(filename, mode)
89+
return None
90+
91+
def asksaveasfile(mode = "w", **options):
92+
"Ask for a filename to save as, and returned the opened file"
93+
94+
filename = apply(SaveAs, (), options).show()
95+
if filename:
96+
return open(filename, mode)
97+
return None
98+
99+
100+
# --------------------------------------------------------------------
101+
# test stuff
102+
103+
if __name__ == "__main__":
104+
105+
print "open", askopenfilename(filetypes=[("all filez", "*")])
106+
print "saveas", asksaveasfilename()

Lib/lib-tk/tkMessageBox.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#
2+
# Instant Python
3+
# $Id$
4+
#
5+
# tk common message boxes
6+
#
7+
# this module provides an interface to the native message boxes
8+
# available in Tk 4.2 and newer.
9+
#
10+
# written by Fredrik Lundh, May 1997
11+
#
12+
13+
#
14+
# options (all have default values):
15+
#
16+
# - default: which button to make default (one of the reply codes)
17+
#
18+
# - icon: which icon to display (see below)
19+
#
20+
# - message: the message to display
21+
#
22+
# - parent: which window to place the dialog on top of
23+
#
24+
# - title: dialog title
25+
#
26+
# - type: dialog type; that is, which buttons to display (see below)
27+
#
28+
29+
from tkCommonDialog import Dialog
30+
31+
#
32+
# constants
33+
34+
# icons
35+
ERROR = "error"
36+
INFO = "info"
37+
QUESTION = "question"
38+
WARNING = "warning"
39+
40+
# types
41+
ABORTRETRYIGNORE = "abortretryignore"
42+
OK = "ok"
43+
OKCANCEL = "okcancel"
44+
RETRYCANCEL = "retrycancel"
45+
YESNO = "yesno"
46+
YESNOCANCEL = "yesnocancel"
47+
48+
# replies
49+
ABORT = "abort"
50+
RETRY = "retry"
51+
IGNORE = "ignore"
52+
OK = "ok"
53+
CANCEL = "cancel"
54+
YES = "yes"
55+
NO = "no"
56+
57+
58+
#
59+
# message dialog class
60+
61+
class Message(Dialog):
62+
"A message box"
63+
64+
command = "tk_messageBox"
65+
66+
67+
#
68+
# convenience stuff
69+
70+
def _show(title=None, message=None, icon=None, type=None, **options):
71+
if icon: options["icon"] = icon
72+
if type: options["type"] = type
73+
if title: options["title"] = title
74+
if message: options["message"] = message
75+
return apply(Message, (), options).show()
76+
77+
def showinfo(title=None, message=None, **options):
78+
"Show an info message"
79+
return apply(_show, (title, message, INFO, OK), options)
80+
81+
def showwarning(title=None, message=None, **options):
82+
"Show a warning message"
83+
return apply(_show, (title, message, WARNING, OK), options)
84+
85+
def showerror(title=None, message=None, **options):
86+
"Show an error message"
87+
return apply(_show, (title, message, ERROR, OK), options)
88+
89+
def askquestion(title=None, message=None, **options):
90+
"Ask a question"
91+
return apply(_show, (title, message, QUESTION, YESNO), options)
92+
93+
def askokcancel(title=None, message=None, **options):
94+
"Ask if operation should proceed; return true if the answer is ok"
95+
s = apply(_show, (title, message, QUESTION, OKCANCEL), options)
96+
return s == OK
97+
98+
def askyesno(title=None, message=None, **options):
99+
"Ask a question; return true if the answer is yes"
100+
s = apply(_show, (title, message, QUESTION, YESNO), options)
101+
return s == YES
102+
103+
def askretrycancel(title=None, message=None, **options):
104+
"Ask if operation should be retried; return true if the answer is yes"
105+
s = apply(_show, (title, message, WARNING, RETRYCANCEL), options)
106+
return s == RETRY
107+
108+
109+
# --------------------------------------------------------------------
110+
# test stuff
111+
112+
if __name__ == "__main__":
113+
114+
print "info", showinfo("Spam", "Egg Information")
115+
print "warning", showwarning("Spam", "Egg Warning")
116+
print "error", showerror("Spam", "Egg Alert")
117+
print "question", askquestion("Spam", "Question?")
118+
print "proceed", askokcancel("Spam", "Proceed?")
119+
print "yes/no", askyesno("Spam", "Got it?")
120+
print "try again", askretrycancel("Spam", "Try again?")

0 commit comments

Comments
 (0)