mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Give in to the tab police.
This commit is contained in:
parent
6f73c1a2ac
commit
c457048744
6 changed files with 286 additions and 276 deletions
|
@ -78,41 +78,41 @@ class CanvasItem:
|
||||||
return self.canvas.type(self.id)
|
return self.canvas.type(self.id)
|
||||||
|
|
||||||
class Arc(CanvasItem):
|
class Arc(CanvasItem):
|
||||||
def __init__(self, canvas, *args, **kw):
|
def __init__(self, canvas, *args, **kw):
|
||||||
apply(CanvasItem.__init__, (self, canvas, 'arc') + args, kw)
|
apply(CanvasItem.__init__, (self, canvas, 'arc') + args, kw)
|
||||||
|
|
||||||
class Bitmap(CanvasItem):
|
class Bitmap(CanvasItem):
|
||||||
def __init__(self, canvas, *args, **kw):
|
def __init__(self, canvas, *args, **kw):
|
||||||
apply(CanvasItem.__init__, (self, canvas, 'bitmap') + args, kw)
|
apply(CanvasItem.__init__, (self, canvas, 'bitmap') + args, kw)
|
||||||
|
|
||||||
class ImageItem(CanvasItem):
|
class ImageItem(CanvasItem):
|
||||||
def __init__(self, canvas, *args, **kw):
|
def __init__(self, canvas, *args, **kw):
|
||||||
apply(CanvasItem.__init__, (self, canvas, 'image') + args, kw)
|
apply(CanvasItem.__init__, (self, canvas, 'image') + args, kw)
|
||||||
|
|
||||||
class Line(CanvasItem):
|
class Line(CanvasItem):
|
||||||
def __init__(self, canvas, *args, **kw):
|
def __init__(self, canvas, *args, **kw):
|
||||||
apply(CanvasItem.__init__, (self, canvas, 'line') + args, kw)
|
apply(CanvasItem.__init__, (self, canvas, 'line') + args, kw)
|
||||||
|
|
||||||
class Oval(CanvasItem):
|
class Oval(CanvasItem):
|
||||||
def __init__(self, canvas, *args, **kw):
|
def __init__(self, canvas, *args, **kw):
|
||||||
apply(CanvasItem.__init__, (self, canvas, 'oval') + args, kw)
|
apply(CanvasItem.__init__, (self, canvas, 'oval') + args, kw)
|
||||||
|
|
||||||
class Polygon(CanvasItem):
|
class Polygon(CanvasItem):
|
||||||
def __init__(self, canvas, *args, **kw):
|
def __init__(self, canvas, *args, **kw):
|
||||||
apply(CanvasItem.__init__, (self, canvas, 'polygon') + args, kw)
|
apply(CanvasItem.__init__, (self, canvas, 'polygon') + args,kw)
|
||||||
|
|
||||||
class Rectangle(CanvasItem):
|
class Rectangle(CanvasItem):
|
||||||
def __init__(self, canvas, *args, **kw):
|
def __init__(self, canvas, *args, **kw):
|
||||||
apply(CanvasItem.__init__, (self, canvas, 'rectangle') + args, kw)
|
apply(CanvasItem.__init__, (self, canvas, 'rectangle')+args,kw)
|
||||||
|
|
||||||
# XXX "Text" is taken by the Text widget...
|
# XXX "Text" is taken by the Text widget...
|
||||||
class CanvasText(CanvasItem):
|
class CanvasText(CanvasItem):
|
||||||
def __init__(self, canvas, *args, **kw):
|
def __init__(self, canvas, *args, **kw):
|
||||||
apply(CanvasItem.__init__, (self, canvas, 'text') + args, kw)
|
apply(CanvasItem.__init__, (self, canvas, 'text') + args, kw)
|
||||||
|
|
||||||
class Window(CanvasItem):
|
class Window(CanvasItem):
|
||||||
def __init__(self, canvas, *args, **kw):
|
def __init__(self, canvas, *args, **kw):
|
||||||
apply(CanvasItem.__init__, (self, canvas, 'window') + args, kw)
|
apply(CanvasItem.__init__, (self, canvas, 'window') + args, kw)
|
||||||
|
|
||||||
class Group:
|
class Group:
|
||||||
def __init__(self, canvas, tag=None):
|
def __init__(self, canvas, tag=None):
|
||||||
|
|
|
@ -27,7 +27,7 @@ class FileDialog:
|
||||||
d = FileDialog(master)
|
d = FileDialog(master)
|
||||||
file = d.go(dir_or_file, pattern, default, key)
|
file = d.go(dir_or_file, pattern, default, key)
|
||||||
if file is None: ...canceled...
|
if file is None: ...canceled...
|
||||||
else: ...open file...
|
else: ...open file...
|
||||||
|
|
||||||
All arguments to go() are optional.
|
All arguments to go() are optional.
|
||||||
|
|
||||||
|
@ -44,172 +44,172 @@ class FileDialog:
|
||||||
title = "File Selection Dialog"
|
title = "File Selection Dialog"
|
||||||
|
|
||||||
def __init__(self, master, title=None):
|
def __init__(self, master, title=None):
|
||||||
if title is None: title = self.title
|
if title is None: title = self.title
|
||||||
self.master = master
|
self.master = master
|
||||||
self.directory = None
|
self.directory = None
|
||||||
|
|
||||||
self.top = Toplevel(master)
|
self.top = Toplevel(master)
|
||||||
self.top.title(title)
|
self.top.title(title)
|
||||||
self.top.iconname(title)
|
self.top.iconname(title)
|
||||||
|
|
||||||
self.botframe = Frame(self.top)
|
self.botframe = Frame(self.top)
|
||||||
self.botframe.pack(side=BOTTOM, fill=X)
|
self.botframe.pack(side=BOTTOM, fill=X)
|
||||||
|
|
||||||
self.selection = Entry(self.top)
|
self.selection = Entry(self.top)
|
||||||
self.selection.pack(side=BOTTOM, fill=X)
|
self.selection.pack(side=BOTTOM, fill=X)
|
||||||
self.selection.bind('<Return>', self.ok_event)
|
self.selection.bind('<Return>', self.ok_event)
|
||||||
|
|
||||||
self.filter = Entry(self.top)
|
self.filter = Entry(self.top)
|
||||||
self.filter.pack(side=TOP, fill=X)
|
self.filter.pack(side=TOP, fill=X)
|
||||||
self.filter.bind('<Return>', self.filter_command)
|
self.filter.bind('<Return>', self.filter_command)
|
||||||
|
|
||||||
self.midframe = Frame(self.top)
|
self.midframe = Frame(self.top)
|
||||||
self.midframe.pack(expand=YES, fill=BOTH)
|
self.midframe.pack(expand=YES, fill=BOTH)
|
||||||
|
|
||||||
self.filesbar = Scrollbar(self.midframe)
|
self.filesbar = Scrollbar(self.midframe)
|
||||||
self.filesbar.pack(side=RIGHT, fill=Y)
|
self.filesbar.pack(side=RIGHT, fill=Y)
|
||||||
self.files = Listbox(self.midframe, exportselection=0,
|
self.files = Listbox(self.midframe, exportselection=0,
|
||||||
yscrollcommand=(self.filesbar, 'set'))
|
yscrollcommand=(self.filesbar, 'set'))
|
||||||
self.files.pack(side=RIGHT, expand=YES, fill=BOTH)
|
self.files.pack(side=RIGHT, expand=YES, fill=BOTH)
|
||||||
btags = self.files.bindtags()
|
btags = self.files.bindtags()
|
||||||
self.files.bindtags(btags[1:] + btags[:1])
|
self.files.bindtags(btags[1:] + btags[:1])
|
||||||
self.files.bind('<ButtonRelease-1>', self.files_select_event)
|
self.files.bind('<ButtonRelease-1>', self.files_select_event)
|
||||||
self.files.bind('<Double-ButtonRelease-1>', self.files_double_event)
|
self.files.bind('<Double-ButtonRelease-1>', self.files_double_event)
|
||||||
self.filesbar.config(command=(self.files, 'yview'))
|
self.filesbar.config(command=(self.files, 'yview'))
|
||||||
|
|
||||||
self.dirsbar = Scrollbar(self.midframe)
|
self.dirsbar = Scrollbar(self.midframe)
|
||||||
self.dirsbar.pack(side=LEFT, fill=Y)
|
self.dirsbar.pack(side=LEFT, fill=Y)
|
||||||
self.dirs = Listbox(self.midframe, exportselection=0,
|
self.dirs = Listbox(self.midframe, exportselection=0,
|
||||||
yscrollcommand=(self.dirsbar, 'set'))
|
yscrollcommand=(self.dirsbar, 'set'))
|
||||||
self.dirs.pack(side=LEFT, expand=YES, fill=BOTH)
|
self.dirs.pack(side=LEFT, expand=YES, fill=BOTH)
|
||||||
self.dirsbar.config(command=(self.dirs, 'yview'))
|
self.dirsbar.config(command=(self.dirs, 'yview'))
|
||||||
btags = self.dirs.bindtags()
|
btags = self.dirs.bindtags()
|
||||||
self.dirs.bindtags(btags[1:] + btags[:1])
|
self.dirs.bindtags(btags[1:] + btags[:1])
|
||||||
self.dirs.bind('<ButtonRelease-1>', self.dirs_select_event)
|
self.dirs.bind('<ButtonRelease-1>', self.dirs_select_event)
|
||||||
self.dirs.bind('<Double-ButtonRelease-1>', self.dirs_double_event)
|
self.dirs.bind('<Double-ButtonRelease-1>', self.dirs_double_event)
|
||||||
|
|
||||||
self.ok_button = Button(self.botframe,
|
self.ok_button = Button(self.botframe,
|
||||||
text="OK",
|
text="OK",
|
||||||
command=self.ok_command)
|
command=self.ok_command)
|
||||||
self.ok_button.pack(side=LEFT)
|
self.ok_button.pack(side=LEFT)
|
||||||
self.filter_button = Button(self.botframe,
|
self.filter_button = Button(self.botframe,
|
||||||
text="Filter",
|
text="Filter",
|
||||||
command=self.filter_command)
|
command=self.filter_command)
|
||||||
self.filter_button.pack(side=LEFT, expand=YES)
|
self.filter_button.pack(side=LEFT, expand=YES)
|
||||||
self.cancel_button = Button(self.botframe,
|
self.cancel_button = Button(self.botframe,
|
||||||
text="Cancel",
|
text="Cancel",
|
||||||
command=self.cancel_command)
|
command=self.cancel_command)
|
||||||
self.cancel_button.pack(side=RIGHT)
|
self.cancel_button.pack(side=RIGHT)
|
||||||
|
|
||||||
self.top.protocol('WM_DELETE_WINDOW', self.cancel_command)
|
self.top.protocol('WM_DELETE_WINDOW', self.cancel_command)
|
||||||
# XXX Are the following okay for a general audience?
|
# XXX Are the following okay for a general audience?
|
||||||
self.top.bind('<Alt-w>', self.cancel_command)
|
self.top.bind('<Alt-w>', self.cancel_command)
|
||||||
self.top.bind('<Alt-W>', self.cancel_command)
|
self.top.bind('<Alt-W>', self.cancel_command)
|
||||||
|
|
||||||
def go(self, dir_or_file=os.curdir, pattern="*", default="", key=None):
|
def go(self, dir_or_file=os.curdir, pattern="*", default="", key=None):
|
||||||
if key and dialogstates.has_key(key):
|
if key and dialogstates.has_key(key):
|
||||||
self.directory, pattern = dialogstates[key]
|
self.directory, pattern = dialogstates[key]
|
||||||
else:
|
else:
|
||||||
dir_or_file = os.path.expanduser(dir_or_file)
|
dir_or_file = os.path.expanduser(dir_or_file)
|
||||||
if os.path.isdir(dir_or_file):
|
if os.path.isdir(dir_or_file):
|
||||||
self.directory = dir_or_file
|
self.directory = dir_or_file
|
||||||
else:
|
else:
|
||||||
self.directory, default = os.path.split(dir_or_file)
|
self.directory, default = os.path.split(dir_or_file)
|
||||||
self.set_filter(self.directory, pattern)
|
self.set_filter(self.directory, pattern)
|
||||||
self.set_selection(default)
|
self.set_selection(default)
|
||||||
self.filter_command()
|
self.filter_command()
|
||||||
self.selection.focus_set()
|
self.selection.focus_set()
|
||||||
self.top.grab_set()
|
self.top.grab_set()
|
||||||
self.how = None
|
self.how = None
|
||||||
self.master.mainloop() # Exited by self.quit(how)
|
self.master.mainloop() # Exited by self.quit(how)
|
||||||
if key: dialogstates[key] = self.get_filter()
|
if key: dialogstates[key] = self.get_filter()
|
||||||
self.top.destroy()
|
self.top.destroy()
|
||||||
return self.how
|
return self.how
|
||||||
|
|
||||||
def quit(self, how=None):
|
def quit(self, how=None):
|
||||||
self.how = how
|
self.how = how
|
||||||
self.master.quit() # Exit mainloop()
|
self.master.quit() # Exit mainloop()
|
||||||
|
|
||||||
def dirs_double_event(self, event):
|
def dirs_double_event(self, event):
|
||||||
self.filter_command()
|
self.filter_command()
|
||||||
|
|
||||||
def dirs_select_event(self, event):
|
def dirs_select_event(self, event):
|
||||||
dir, pat = self.get_filter()
|
dir, pat = self.get_filter()
|
||||||
subdir = self.dirs.get('active')
|
subdir = self.dirs.get('active')
|
||||||
dir = os.path.normpath(os.path.join(self.directory, subdir))
|
dir = os.path.normpath(os.path.join(self.directory, subdir))
|
||||||
self.set_filter(dir, pat)
|
self.set_filter(dir, pat)
|
||||||
|
|
||||||
def files_double_event(self, event):
|
def files_double_event(self, event):
|
||||||
self.ok_command()
|
self.ok_command()
|
||||||
|
|
||||||
def files_select_event(self, event):
|
def files_select_event(self, event):
|
||||||
file = self.files.get('active')
|
file = self.files.get('active')
|
||||||
self.set_selection(file)
|
self.set_selection(file)
|
||||||
|
|
||||||
def ok_event(self, event):
|
def ok_event(self, event):
|
||||||
self.ok_command()
|
self.ok_command()
|
||||||
|
|
||||||
def ok_command(self):
|
def ok_command(self):
|
||||||
self.quit(self.get_selection())
|
self.quit(self.get_selection())
|
||||||
|
|
||||||
def filter_command(self, event=None):
|
def filter_command(self, event=None):
|
||||||
dir, pat = self.get_filter()
|
dir, pat = self.get_filter()
|
||||||
try:
|
try:
|
||||||
names = os.listdir(dir)
|
names = os.listdir(dir)
|
||||||
except os.error:
|
except os.error:
|
||||||
self.master.bell()
|
self.master.bell()
|
||||||
return
|
return
|
||||||
self.directory = dir
|
self.directory = dir
|
||||||
self.set_filter(dir, pat)
|
self.set_filter(dir, pat)
|
||||||
names.sort()
|
names.sort()
|
||||||
subdirs = [os.pardir]
|
subdirs = [os.pardir]
|
||||||
matchingfiles = []
|
matchingfiles = []
|
||||||
for name in names:
|
for name in names:
|
||||||
fullname = os.path.join(dir, name)
|
fullname = os.path.join(dir, name)
|
||||||
if os.path.isdir(fullname):
|
if os.path.isdir(fullname):
|
||||||
subdirs.append(name)
|
subdirs.append(name)
|
||||||
elif fnmatch.fnmatch(name, pat):
|
elif fnmatch.fnmatch(name, pat):
|
||||||
matchingfiles.append(name)
|
matchingfiles.append(name)
|
||||||
self.dirs.delete(0, END)
|
self.dirs.delete(0, END)
|
||||||
for name in subdirs:
|
for name in subdirs:
|
||||||
self.dirs.insert(END, name)
|
self.dirs.insert(END, name)
|
||||||
self.files.delete(0, END)
|
self.files.delete(0, END)
|
||||||
for name in matchingfiles:
|
for name in matchingfiles:
|
||||||
self.files.insert(END, name)
|
self.files.insert(END, name)
|
||||||
head, tail = os.path.split(self.get_selection())
|
head, tail = os.path.split(self.get_selection())
|
||||||
if tail == os.curdir: tail = ''
|
if tail == os.curdir: tail = ''
|
||||||
self.set_selection(tail)
|
self.set_selection(tail)
|
||||||
|
|
||||||
def get_filter(self):
|
def get_filter(self):
|
||||||
filter = self.filter.get()
|
filter = self.filter.get()
|
||||||
filter = os.path.expanduser(filter)
|
filter = os.path.expanduser(filter)
|
||||||
if filter[-1:] == os.sep or os.path.isdir(filter):
|
if filter[-1:] == os.sep or os.path.isdir(filter):
|
||||||
filter = os.path.join(filter, "*")
|
filter = os.path.join(filter, "*")
|
||||||
return os.path.split(filter)
|
return os.path.split(filter)
|
||||||
|
|
||||||
def get_selection(self):
|
def get_selection(self):
|
||||||
file = self.selection.get()
|
file = self.selection.get()
|
||||||
file = os.path.expanduser(file)
|
file = os.path.expanduser(file)
|
||||||
return file
|
return file
|
||||||
|
|
||||||
def cancel_command(self, event=None):
|
def cancel_command(self, event=None):
|
||||||
self.quit()
|
self.quit()
|
||||||
|
|
||||||
def set_filter(self, dir, pat):
|
def set_filter(self, dir, pat):
|
||||||
if not os.path.isabs(dir):
|
if not os.path.isabs(dir):
|
||||||
try:
|
try:
|
||||||
pwd = os.getcwd()
|
pwd = os.getcwd()
|
||||||
except os.error:
|
except os.error:
|
||||||
pwd = None
|
pwd = None
|
||||||
if pwd:
|
if pwd:
|
||||||
dir = os.path.join(pwd, dir)
|
dir = os.path.join(pwd, dir)
|
||||||
dir = os.path.normpath(dir)
|
dir = os.path.normpath(dir)
|
||||||
self.filter.delete(0, END)
|
self.filter.delete(0, END)
|
||||||
self.filter.insert(END, os.path.join(dir or os.curdir, pat or "*"))
|
self.filter.insert(END, os.path.join(dir or os.curdir, pat or "*"))
|
||||||
|
|
||||||
def set_selection(self, file):
|
def set_selection(self, file):
|
||||||
self.selection.delete(0, END)
|
self.selection.delete(0, END)
|
||||||
self.selection.insert(END, os.path.join(self.directory, file))
|
self.selection.insert(END, os.path.join(self.directory, file))
|
||||||
|
|
||||||
|
|
||||||
class LoadFileDialog(FileDialog):
|
class LoadFileDialog(FileDialog):
|
||||||
|
@ -219,11 +219,11 @@ class LoadFileDialog(FileDialog):
|
||||||
title = "Load File Selection Dialog"
|
title = "Load File Selection Dialog"
|
||||||
|
|
||||||
def ok_command(self):
|
def ok_command(self):
|
||||||
file = self.get_selection()
|
file = self.get_selection()
|
||||||
if not os.path.isfile(file):
|
if not os.path.isfile(file):
|
||||||
self.master.bell()
|
self.master.bell()
|
||||||
else:
|
else:
|
||||||
self.quit(file)
|
self.quit(file)
|
||||||
|
|
||||||
|
|
||||||
class SaveFileDialog(FileDialog):
|
class SaveFileDialog(FileDialog):
|
||||||
|
@ -233,25 +233,25 @@ class SaveFileDialog(FileDialog):
|
||||||
title = "Save File Selection Dialog"
|
title = "Save File Selection Dialog"
|
||||||
|
|
||||||
def ok_command(self):
|
def ok_command(self):
|
||||||
file = self.get_selection()
|
file = self.get_selection()
|
||||||
if os.path.exists(file):
|
if os.path.exists(file):
|
||||||
if os.path.isdir(file):
|
if os.path.isdir(file):
|
||||||
self.master.bell()
|
self.master.bell()
|
||||||
return
|
return
|
||||||
d = Dialog(self.top,
|
d = Dialog(self.top,
|
||||||
title="Overwrite Existing File Question",
|
title="Overwrite Existing File Question",
|
||||||
text="Overwrite existing file %s?" % `file`,
|
text="Overwrite existing file %s?" % `file`,
|
||||||
bitmap='questhead',
|
bitmap='questhead',
|
||||||
default=1,
|
default=1,
|
||||||
strings=("Yes", "Cancel"))
|
strings=("Yes", "Cancel"))
|
||||||
if d.num != 0:
|
if d.num != 0:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
head, tail = os.path.split(file)
|
head, tail = os.path.split(file)
|
||||||
if not os.path.isdir(head):
|
if not os.path.isdir(head):
|
||||||
self.master.bell()
|
self.master.bell()
|
||||||
return
|
return
|
||||||
self.quit(file)
|
self.quit(file)
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
|
|
|
@ -7,99 +7,99 @@ from Tkinter import *
|
||||||
class SimpleDialog:
|
class SimpleDialog:
|
||||||
|
|
||||||
def __init__(self, master,
|
def __init__(self, master,
|
||||||
text='', buttons=[], default=None, cancel=None,
|
text='', buttons=[], default=None, cancel=None,
|
||||||
title=None, class_=None):
|
title=None, class_=None):
|
||||||
if class_:
|
if class_:
|
||||||
self.root = Toplevel(master, class_=class_)
|
self.root = Toplevel(master, class_=class_)
|
||||||
else:
|
else:
|
||||||
self.root = Toplevel(master)
|
self.root = Toplevel(master)
|
||||||
if title:
|
if title:
|
||||||
self.root.title(title)
|
self.root.title(title)
|
||||||
self.root.iconname(title)
|
self.root.iconname(title)
|
||||||
self.message = Message(self.root, text=text, aspect=400)
|
self.message = Message(self.root, text=text, aspect=400)
|
||||||
self.message.pack(expand=1, fill=BOTH)
|
self.message.pack(expand=1, fill=BOTH)
|
||||||
self.frame = Frame(self.root)
|
self.frame = Frame(self.root)
|
||||||
self.frame.pack()
|
self.frame.pack()
|
||||||
self.num = default
|
self.num = default
|
||||||
self.cancel = cancel
|
self.cancel = cancel
|
||||||
self.default = default
|
self.default = default
|
||||||
self.root.bind('<Return>', self.return_event)
|
self.root.bind('<Return>', self.return_event)
|
||||||
for num in range(len(buttons)):
|
for num in range(len(buttons)):
|
||||||
s = buttons[num]
|
s = buttons[num]
|
||||||
b = Button(self.frame, text=s,
|
b = Button(self.frame, text=s,
|
||||||
command=(lambda self=self, num=num: self.done(num)))
|
command=(lambda self=self, num=num: self.done(num)))
|
||||||
if num == default:
|
if num == default:
|
||||||
b.config(relief=RIDGE, borderwidth=8)
|
b.config(relief=RIDGE, borderwidth=8)
|
||||||
b.pack(side=LEFT, fill=BOTH, expand=1)
|
b.pack(side=LEFT, fill=BOTH, expand=1)
|
||||||
self.root.protocol('WM_DELETE_WINDOW', self.wm_delete_window)
|
self.root.protocol('WM_DELETE_WINDOW', self.wm_delete_window)
|
||||||
self._set_transient(master)
|
self._set_transient(master)
|
||||||
|
|
||||||
def _set_transient(self, master, relx=0.5, rely=0.3):
|
def _set_transient(self, master, relx=0.5, rely=0.3):
|
||||||
widget = self.root
|
widget = self.root
|
||||||
widget.withdraw() # Remain invisible while we figure out the geometry
|
widget.withdraw() # Remain invisible while we figure out the geometry
|
||||||
widget.transient(master)
|
widget.transient(master)
|
||||||
widget.update_idletasks() # Actualize geometry information
|
widget.update_idletasks() # Actualize geometry information
|
||||||
if master.winfo_ismapped():
|
if master.winfo_ismapped():
|
||||||
m_width = master.winfo_width()
|
m_width = master.winfo_width()
|
||||||
m_height = master.winfo_height()
|
m_height = master.winfo_height()
|
||||||
m_x = master.winfo_rootx()
|
m_x = master.winfo_rootx()
|
||||||
m_y = master.winfo_rooty()
|
m_y = master.winfo_rooty()
|
||||||
else:
|
else:
|
||||||
m_width = master.winfo_screenwidth()
|
m_width = master.winfo_screenwidth()
|
||||||
m_height = master.winfo_screenheight()
|
m_height = master.winfo_screenheight()
|
||||||
m_x = m_y = 0
|
m_x = m_y = 0
|
||||||
w_width = widget.winfo_reqwidth()
|
w_width = widget.winfo_reqwidth()
|
||||||
w_height = widget.winfo_reqheight()
|
w_height = widget.winfo_reqheight()
|
||||||
x = m_x + (m_width - w_width) * relx
|
x = m_x + (m_width - w_width) * relx
|
||||||
y = m_y + (m_height - w_height) * rely
|
y = m_y + (m_height - w_height) * rely
|
||||||
if x+w_width > master.winfo_screenwidth():
|
if x+w_width > master.winfo_screenwidth():
|
||||||
x = master.winfo_screenwidth() - w_width
|
x = master.winfo_screenwidth() - w_width
|
||||||
elif x < 0:
|
elif x < 0:
|
||||||
x = 0
|
x = 0
|
||||||
if y+w_height > master.winfo_screenheight():
|
if y+w_height > master.winfo_screenheight():
|
||||||
y = master.winfo_screenheight() - w_height
|
y = master.winfo_screenheight() - w_height
|
||||||
elif y < 0:
|
elif y < 0:
|
||||||
y = 0
|
y = 0
|
||||||
widget.geometry("+%d+%d" % (x, y))
|
widget.geometry("+%d+%d" % (x, y))
|
||||||
widget.deiconify() # Become visible at the desired location
|
widget.deiconify() # Become visible at the desired location
|
||||||
|
|
||||||
def go(self):
|
def go(self):
|
||||||
self.root.grab_set()
|
self.root.grab_set()
|
||||||
self.root.mainloop()
|
self.root.mainloop()
|
||||||
self.root.destroy()
|
self.root.destroy()
|
||||||
return self.num
|
return self.num
|
||||||
|
|
||||||
def return_event(self, event):
|
def return_event(self, event):
|
||||||
if self.default is None:
|
if self.default is None:
|
||||||
self.root.bell()
|
self.root.bell()
|
||||||
else:
|
else:
|
||||||
self.done(self.default)
|
self.done(self.default)
|
||||||
|
|
||||||
def wm_delete_window(self):
|
def wm_delete_window(self):
|
||||||
if self.cancel is None:
|
if self.cancel is None:
|
||||||
self.root.bell()
|
self.root.bell()
|
||||||
else:
|
else:
|
||||||
self.done(self.cancel)
|
self.done(self.cancel)
|
||||||
|
|
||||||
def done(self, num):
|
def done(self, num):
|
||||||
self.num = num
|
self.num = num
|
||||||
self.root.quit()
|
self.root.quit()
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
root = Tk()
|
root = Tk()
|
||||||
def doit(root=root):
|
def doit(root=root):
|
||||||
d = SimpleDialog(root,
|
d = SimpleDialog(root,
|
||||||
text="This is a test dialog. "
|
text="This is a test dialog. "
|
||||||
"Would this have been an actual dialog, "
|
"Would this have been an actual dialog, "
|
||||||
"the buttons below would have been glowing "
|
"the buttons below would have been glowing "
|
||||||
"in soft pink light.\n"
|
"in soft pink light.\n"
|
||||||
"Do you believe this?",
|
"Do you believe this?",
|
||||||
buttons=["Yes", "No", "Cancel"],
|
buttons=["Yes", "No", "Cancel"],
|
||||||
default=0,
|
default=0,
|
||||||
cancel=2,
|
cancel=2,
|
||||||
title="Test Dialog")
|
title="Test Dialog")
|
||||||
print d.go()
|
print d.go()
|
||||||
t = Button(root, text='Test', command=doit)
|
t = Button(root, text='Test', command=doit)
|
||||||
t.pack()
|
t.pack()
|
||||||
q = Button(root, text='Quit', command=t.quit)
|
q = Button(root, text='Quit', command=t.quit)
|
||||||
|
|
|
@ -51,8 +51,14 @@ def _cnfmerge(cnfs):
|
||||||
class Event:
|
class Event:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
_support_default_root = 1
|
||||||
_default_root = None
|
_default_root = None
|
||||||
|
|
||||||
|
def NoDefaultRoot():
|
||||||
|
global _support_default_root
|
||||||
|
_support_default_root = 0
|
||||||
|
del _default_root
|
||||||
|
|
||||||
def _tkerror(err):
|
def _tkerror(err):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -536,7 +542,7 @@ class Misc:
|
||||||
if needcleanup:
|
if needcleanup:
|
||||||
if self._tclCommands is None:
|
if self._tclCommands is None:
|
||||||
self._tclCommands = []
|
self._tclCommands = []
|
||||||
self._tclCommands.append(name)
|
self._tclCommands.append(name)
|
||||||
#print '+ Tkinter created command', name
|
#print '+ Tkinter created command', name
|
||||||
return name
|
return name
|
||||||
register = _register
|
register = _register
|
||||||
|
@ -702,6 +708,7 @@ class Misc:
|
||||||
|
|
||||||
# Support for the "event" command, new in Tk 4.2.
|
# Support for the "event" command, new in Tk 4.2.
|
||||||
# By Case Roole.
|
# By Case Roole.
|
||||||
|
# XXX Why is this using the default root?
|
||||||
|
|
||||||
def event_add(self,virtual, *sequences):
|
def event_add(self,virtual, *sequences):
|
||||||
args = ('event', 'add', virtual) + sequences
|
args = ('event', 'add', virtual) + sequences
|
||||||
|
@ -795,7 +802,7 @@ class Wm:
|
||||||
def positionfrom(self, who=None):
|
def positionfrom(self, who=None):
|
||||||
return self.tk.call('wm', 'positionfrom', self._w, who)
|
return self.tk.call('wm', 'positionfrom', self._w, who)
|
||||||
def protocol(self, name=None, func=None):
|
def protocol(self, name=None, func=None):
|
||||||
if callable(func):
|
if callable(func):
|
||||||
command = self._register(func)
|
command = self._register(func)
|
||||||
else:
|
else:
|
||||||
command = func
|
command = func
|
||||||
|
@ -857,14 +864,14 @@ class Tk(Misc, Wm):
|
||||||
self.tk.createcommand('tkerror', _tkerror)
|
self.tk.createcommand('tkerror', _tkerror)
|
||||||
self.tk.createcommand('exit', _exit)
|
self.tk.createcommand('exit', _exit)
|
||||||
self.readprofile(baseName, className)
|
self.readprofile(baseName, className)
|
||||||
if not _default_root:
|
if _support_default_root and not _default_root:
|
||||||
_default_root = self
|
_default_root = self
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
for c in self.children.values(): c.destroy()
|
for c in self.children.values(): c.destroy()
|
||||||
self.tk.call('destroy', self._w)
|
self.tk.call('destroy', self._w)
|
||||||
Misc.destroy(self)
|
Misc.destroy(self)
|
||||||
global _default_root
|
global _default_root
|
||||||
if _default_root is self:
|
if _support_default_root and _default_root is self:
|
||||||
_default_root = None
|
_default_root = None
|
||||||
def readprofile(self, baseName, className):
|
def readprofile(self, baseName, className):
|
||||||
import os
|
import os
|
||||||
|
@ -994,13 +1001,14 @@ class Grid:
|
||||||
|
|
||||||
class BaseWidget(Misc):
|
class BaseWidget(Misc):
|
||||||
def _setup(self, master, cnf):
|
def _setup(self, master, cnf):
|
||||||
global _default_root
|
if _support_default_root:
|
||||||
if not master:
|
global _default_root
|
||||||
|
if not master:
|
||||||
|
if not _default_root:
|
||||||
|
_default_root = Tk()
|
||||||
|
master = _default_root
|
||||||
if not _default_root:
|
if not _default_root:
|
||||||
_default_root = Tk()
|
_default_root = master
|
||||||
master = _default_root
|
|
||||||
if not _default_root:
|
|
||||||
_default_root = master
|
|
||||||
self.master = master
|
self.master = master
|
||||||
self.tk = master.tk
|
self.tk = master.tk
|
||||||
name = None
|
name = None
|
||||||
|
@ -1693,10 +1701,12 @@ class OptionMenu(Menubutton):
|
||||||
self.__menu = None
|
self.__menu = None
|
||||||
|
|
||||||
class Image:
|
class Image:
|
||||||
def __init__(self, imgtype, name=None, cnf={}, **kw):
|
def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
|
||||||
self.name = None
|
self.name = None
|
||||||
master = _default_root
|
if not master:
|
||||||
if not master: raise RuntimeError, 'Too early to create image'
|
master = _default_root
|
||||||
|
if not master:
|
||||||
|
raise RuntimeError, 'Too early to create image'
|
||||||
self.tk = master.tk
|
self.tk = master.tk
|
||||||
if not name:
|
if not name:
|
||||||
name = `id(self)`
|
name = `id(self)`
|
||||||
|
@ -1741,8 +1751,8 @@ class Image:
|
||||||
self.tk.call('image', 'width', self.name))
|
self.tk.call('image', 'width', self.name))
|
||||||
|
|
||||||
class PhotoImage(Image):
|
class PhotoImage(Image):
|
||||||
def __init__(self, name=None, cnf={}, **kw):
|
def __init__(self, name=None, cnf={}, master=None, **kw):
|
||||||
apply(Image.__init__, (self, 'photo', name, cnf), kw)
|
apply(Image.__init__, (self, 'photo', name, cnf, master), kw)
|
||||||
def blank(self):
|
def blank(self):
|
||||||
self.tk.call(self.name, 'blank')
|
self.tk.call(self.name, 'blank')
|
||||||
def cget(self, option):
|
def cget(self, option):
|
||||||
|
@ -1784,8 +1794,8 @@ class PhotoImage(Image):
|
||||||
apply(self.tk.call, args)
|
apply(self.tk.call, args)
|
||||||
|
|
||||||
class BitmapImage(Image):
|
class BitmapImage(Image):
|
||||||
def __init__(self, name=None, cnf={}, **kw):
|
def __init__(self, name=None, cnf={}, master=None, **kw):
|
||||||
apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
|
apply(Image.__init__, (self, 'bitmap', name, cnf, master), kw)
|
||||||
|
|
||||||
def image_names(): return _default_root.tk.call('image', 'names')
|
def image_names(): return _default_root.tk.call('image', 'names')
|
||||||
def image_types(): return _default_root.tk.call('image', 'types')
|
def image_types(): return _default_root.tk.call('image', 'types')
|
||||||
|
|
|
@ -25,7 +25,7 @@ class Dialog:
|
||||||
if TkVersion < 4.2:
|
if TkVersion < 4.2:
|
||||||
raise TclError, "this module requires Tk 4.2 or newer"
|
raise TclError, "this module requires Tk 4.2 or newer"
|
||||||
|
|
||||||
self.master = master
|
self.master = master
|
||||||
self.options = options
|
self.options = options
|
||||||
|
|
||||||
def _fixoptions(self):
|
def _fixoptions(self):
|
||||||
|
@ -37,8 +37,8 @@ class Dialog:
|
||||||
def show(self, **options):
|
def show(self, **options):
|
||||||
|
|
||||||
# update instance options
|
# update instance options
|
||||||
for k, v in options.items():
|
for k, v in options.items():
|
||||||
self.options[k] = v
|
self.options[k] = v
|
||||||
|
|
||||||
self._fixoptions()
|
self._fixoptions()
|
||||||
|
|
||||||
|
|
|
@ -41,14 +41,14 @@ class _Dialog(Dialog):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _fixresult(self, widget, result):
|
def _fixresult(self, widget, result):
|
||||||
if result:
|
if result:
|
||||||
# keep directory and filename until next time
|
# keep directory and filename until next time
|
||||||
import os
|
import os
|
||||||
path, file = os.path.split(result)
|
path, file = os.path.split(result)
|
||||||
self.options["initialdir"] = path
|
self.options["initialdir"] = path
|
||||||
self.options["initialfile"] = file
|
self.options["initialfile"] = file
|
||||||
self.filename = result # compatibility
|
self.filename = result # compatibility
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue