Skip to content

Commit

Permalink
Merged revisions 69053 via svnmerge from
Browse files Browse the repository at this point in the history
svn+ssh://pythondev/python/trunk

........
  r69053 | guilherme.polo | 2009-01-28 13:56:01 -0200 (Wed, 28 Jan 2009) | 2 lines

  Demos for ttk added.
........
  • Loading branch information
gpolo committed Jan 28, 2009
1 parent 5f23848 commit a7d2797
Show file tree
Hide file tree
Showing 16 changed files with 1,196 additions and 0 deletions.
1 change: 1 addition & 0 deletions Demo/tkinter/README
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Subdirectories:

guido my original example set (fairly random collection)
matt Matt Conway's examples, to go with his lifesaver document
ttk Examples using the ttk module
46 changes: 46 additions & 0 deletions Demo/tkinter/ttk/combo_themes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Ttk Theme Selector.
Although it is a theme selector, you won't notice many changes since
there is only a combobox and a frame around.
"""
from tkinter import ttk

class App(ttk.Frame):
def __init__(self):
ttk.Frame.__init__(self)

self.style = ttk.Style()
self._setup_widgets()

def _change_theme(self, event):
if event.widget.current(): # value #0 is not a theme
newtheme = event.widget.get()
# change to the new theme and refresh all the widgets
self.style.theme_use(newtheme)

def _setup_widgets(self):
themes = list(self.style.theme_names())
themes.insert(0, "Pick a theme")
# Create a readonly Combobox which will display 4 values at max,
# which will cause it to create a scrollbar if there are more
# than 4 values in total.
themes_combo = ttk.Combobox(self, values=themes, state="readonly",
height=4)
themes_combo.set(themes[0]) # sets the combobox value to "Pick a theme"
# Combobox widget generates a <<ComboboxSelected>> virtual event
# when the user selects an element. This event is generated after
# the listbox is unposted (after you select an item, the combobox's
# listbox disappears, then it is said that listbox is now unposted).
themes_combo.bind("<<ComboboxSelected>>", self._change_theme)
themes_combo.pack(fill='x')

self.pack(fill='both', expand=1)


def main():
app = App()
app.master.title("Ttk Combobox")
app.mainloop()

if __name__ == "__main__":
main()
93 changes: 93 additions & 0 deletions Demo/tkinter/ttk/dirbrowser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""A directory browser using Ttk Treeview.
Based on the demo found in Tk 8.5 library/demos/browse
"""
import os
import glob
import tkinter
from tkinter import ttk

def populate_tree(tree, node):
if tree.set(node, "type") != 'directory':
return

path = tree.set(node, "fullpath")
tree.delete(*tree.get_children(node))

parent = tree.parent(node)
special_dirs = [] if parent else glob.glob('.') + glob.glob('..')

for p in special_dirs + os.listdir(path):
ptype = None
p = os.path.join(path, p).replace('\\', '/')
if os.path.isdir(p): ptype = "directory"
elif os.path.isfile(p): ptype = "file"

fname = os.path.split(p)[1]
id = tree.insert(node, "end", text=fname, values=[p, ptype])

if ptype == 'directory':
if fname not in ('.', '..'):
tree.insert(id, 0, text="dummy")
tree.item(id, text=fname)
elif ptype == 'file':
size = os.stat(p).st_size
tree.set(id, "size", "%d bytes" % size)


def populate_roots(tree):
dir = os.path.abspath('.').replace('\\', '/')
node = tree.insert('', 'end', text=dir, values=[dir, "directory"])
populate_tree(tree, node)

def update_tree(event):
tree = event.widget
populate_tree(tree, tree.focus())

def change_dir(event):
tree = event.widget
node = tree.focus()
if tree.parent(node):
path = os.path.abspath(tree.set(node, "fullpath"))
if os.path.isdir(path):
os.chdir(path)
tree.delete(tree.get_children(''))
populate_roots(tree)

def autoscroll(sbar, first, last):
"""Hide and show scrollbar as needed."""
first, last = float(first), float(last)
if first <= 0 and last >= 1:
sbar.grid_remove()
else:
sbar.grid()
sbar.set(first, last)

root = tkinter.Tk()

vsb = ttk.Scrollbar(orient="vertical")
hsb = ttk.Scrollbar(orient="horizontal")

tree = ttk.Treeview(columns=("fullpath", "type", "size"),
displaycolumns="size", yscrollcommand=lambda f, l: autoscroll(vsb, f, l),
xscrollcommand=lambda f, l:autoscroll(hsb, f, l))

vsb['command'] = tree.yview
hsb['command'] = tree.xview

tree.heading("#0", text="Directory Structure", anchor='w')
tree.heading("size", text="File Size", anchor='w')
tree.column("size", stretch=0, width=100)

populate_roots(tree)
tree.bind('<<TreeviewOpen>>', update_tree)
tree.bind('<Double-Button-1>', change_dir)

# Arrange the tree and its scrollbars in the toplevel
tree.grid(column=0, row=0, sticky='nswe')
vsb.grid(column=1, row=0, sticky='ns')
hsb.grid(column=0, row=1, sticky='ew')
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)

root.mainloop()
Binary file added Demo/tkinter/ttk/img/close.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Demo/tkinter/ttk/img/close_active.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Demo/tkinter/ttk/img/close_pressed.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions Demo/tkinter/ttk/listbox_scrollcmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Sample taken from: http://www.tkdocs.com/tutorial/morewidgets.html and
converted to Python, mainly to demonstrate xscrollcommand option.
grid [tk::listbox .l -yscrollcommand ".s set" -height 5] -column 0 -row 0 -sticky nwes
grid [ttk::scrollbar .s -command ".l yview" -orient vertical] -column 1 -row 0 -sticky ns
grid [ttk::label .stat -text "Status message here" -anchor w] -column 0 -row 1 -sticky we
grid [ttk::sizegrip .sz] -column 1 -row 1 -sticky se
grid columnconfigure . 0 -weight 1; grid rowconfigure . 0 -weight 1
for {set i 0} {$i<100} {incr i} {
.l insert end "Line $i of 100"
}
"""
import tkinter
from tkinter import ttk

root = tkinter.Tk()

l = tkinter.Listbox(height=5)
l.grid(column=0, row=0, sticky='nwes')

s = ttk.Scrollbar(command=l.yview, orient='vertical')
l['yscrollcommand'] = s.set
s.grid(column=1, row=0, sticky="ns")

stat = ttk.Label(text="Status message here", anchor='w')
stat.grid(column=0, row=1, sticky='we')

sz = ttk.Sizegrip()
sz.grid(column=1, row=1, sticky='se')

root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)

for i in range(100):
l.insert('end', "Line %d of 100" % i)

root.mainloop()
78 changes: 78 additions & 0 deletions Demo/tkinter/ttk/mac_searchentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Mac style search widget
Translated from Tcl code by Schelte Bron, http://wiki.tcl.tk/18188
"""
import tkinter
from tkinter import ttk

root = tkinter.Tk()

data = """
R0lGODlhKgAaAOfnAFdZVllbWFpcWVtdWlxeW11fXF9hXmBiX2ZnZWhpZ2lraGxua25wbXJ0
cXR2c3V3dHZ4dXh6d3x+e31/fH6AfYSGg4eJhoiKh4qMiYuNio2PjHmUqnqVq3yXrZGTkJKU
kX+asJSWk32cuJWXlIGcs5aYlX6euZeZloOetZial4SftpqbmIWgt4GhvYahuIKivpudmYei
uYOjv5yem4ijuoSkwIWlwYmlu56gnYamwp+hnoenw4unvaCin4ioxJCnuZykrImpxZmlsoaq
zI2pv6KkoZGouoqqxpqms4erzaOloo6qwYurx5Kqu5untIiszqSmo5CrwoysyJeqtpOrvJyo
tZGsw42typSsvaaopZKtxJWtvp6qt4+uy6epppOuxZCvzKiqp5quuZSvxoyx06mrqJWwx42y
1JKxzpmwwaqsqZaxyI6z1ZqxwqutqpOzz4+01qyuq56yvpizypS00Jm0y5W10Zq1zJa20rCy
rpu3zqizwbGzr6C3yZy4z7K0saG4yp250LO1sqK5y5660Z+70qO7zKy4xaC806S8zba4taG9
1KW9zq66x6+7yLi6t6S/1rC8yrm7uLO8xLG9y7q8ubS9xabB2anB07K+zLW+xrO/za7CzrTA
zrjAyLXBz77BvbbC0K/G2LjD0bnE0rLK28TGw8bIxcLL07vP28HN28rMycvOyr/T38DU4cnR
2s/RztHT0NLU0cTY5MrW5MvX5dHX2c3Z59bY1dPb5Nbb3dLe7Nvd2t3f3NXh797g3d3j5dnl
9OPl4eTm4+Ln6tzo9uXn5Obo5eDp8efp5uHq8uXq7ejq5+nr6OPs9Ovu6unu8O3v6+vw8+7w
7ezx9O/x7vDy7/Hz8O/19/P18vT38/L3+fb49Pf59vX6/fj69/b7/vn7+Pr8+ff9//v9+vz/
+/7//P//////////////////////////////////////////////////////////////////
/////////////////////////////////yH/C05FVFNDQVBFMi4wAwEAAAAh+QQJZAD/ACwC
AAIAKAAWAAAI/gD/CRz4bwUGCg8eQFjIsGHDBw4iTLAQgqBFgisuePCiyJOpUyBDihRpypMi
Lx8qaLhIMIyGFZ5sAUsmjZrNmzhzWpO2DJgtTysqfGDpxoMbW8ekeQsXzty4p1CjRjUXrps3
asJsuclQ4uKKSbamMR3n1JzZs2jRkh1HzuxVXX8y4CDYAwqua+DInVrRwMGJU2kDp31KThy1
XGWGDlxhi1rTPAUICBBAoEAesoIzn6Vm68MKgVAUHftmzhOCBCtQwQKSoABgzZnJdSMmyIPA
FbCotdUQAIhNa9B6DPCAGbZac+SowVIMRVe4pwkA4GpqDlwuAAmMZx4nTtfnf1mO5JEDNy46
MHJkxQEDgKC49rPjwC0bqGaZuOoZAKjBPE4NgAzUvYcWOc0QZF91imAnCDHJ5JFAAJN0I2Ba
4iRDUC/gOEVNDwIUcEABCAgAAATUTIgWOMBYRFp80ghiAQIIVAAEAwJIYI2JZnUji0XSYAYO
NcsQA8wy0hCTwAASXGOiONFcxAtpTokTHznfiLMNMAkcAMuE43jDC0vLeGOWe2R5o4sn1LgH
GzkWsvTPMgEOaA433Ag4TjjMuDkQMNi0tZ12sqWoJ0HATMPNffAZZ6U0wLAyqJ62RGoLLrhI
aqmlpzwaEAAh+QQJZAD/ACwAAAAAKgAaAAAI/gD/CRw40JEhQoEC+fGjcOHCMRAjRkxDsKLF
f5YcAcID582ZjyBDJhmZZIjJIUySEDHiBMhFghrtdNnRAgSHmzhz6sTZQcSLITx+CHn5bxSk
Nz5MCMGy55CjTVCjbuJEtSrVQ3uwqDBRQwrFi476SHHxow8qXcemVbPGtm21t3CnTaP27Jgu
VHtuiIjBsuImQkRiiEEFTNo2cOTMKV7MuLE5cN68QUOGSgwKG1EqJqJDY8+rZt8UjxtNunTj
cY3DgZOWS46KIFgGjiI0ZIsqaqNNjWjgYMUpx8Adc3v2aosNMAI1DbqyI9WycOb4IAggQEAB
A3lQBxet/TG4cMpI/tHwYeSfIzxM0uTKNs7UgAQrYL1akaDA7+3bueVqY4NJlUhIcQLNYx8E
AIQ01mwjTQ8DeNAdfouNA8440GBCQxJY3MEGD6p4Y844CQCAizcSgpMLAAlAuJ03qOyQRBR3
nEHEK+BMGKIui4kDDAAIPKiiYuSYSMQQRCDCxhiziPMYBgDkEaEaAGQA3Y+MjUPOLFoMoUUh
cKxRC4ngeILiH8Qkk0cCAUzSDZWpzbLEE1EwggcYqWCj2DNADFDAAQUgIAAAEFDDJmPYqNJF
F1s4cscTmCDjDTjdSPOHBQggUAEQDAgggTWDPoYMJkFoUdRmddyyjWLeULMMMcAsIw0x4wkM
IME1g25zyxpHxFYUHmyIggw4H4ojITnfiLMNMAkcAAub4BQjihRdDGTJHmvc4Qo1wD6Imje6
eILbj+BQ4wqu5Q3ECSJ0FOKKMtv4mBg33Pw4zjbKuBIIE1xYpIkhdQQiyi7OtAucj6dt48wu
otQhBRa6VvSJIRwhIkotvgRTzMUYZ6xxMcj4QkspeKDxxRhEmUfIHWjAgQcijEDissuXvCyz
zH7Q8YQURxDhUsn/bCInR3AELfTQZBRt9BBJkCGFFVhMwTNBlnBCSCGEIJQQIAklZMXWRBAR
RRRWENHwRQEBADs="""


s1 = tkinter.PhotoImage("search1", data=data, format="gif -index 0")
s2 = tkinter.PhotoImage("search2", data=data, format="gif -index 1")

style = ttk.Style()

style.element_create("Search.field", "image", "search1",
("focus", "search2"), border=[22, 7, 14], sticky="ew")

style.layout("Search.entry", [
("Search.field", {"sticky": "nswe", "border": 1, "children":
[("Entry.padding", {"sticky": "nswe", "children":
[("Entry.textarea", {"sticky": "nswe"})]
})]
})]
)

style.configure("Search.entry", background="#b2b2b2")

root.configure(background="#b2b2b2")

e1 = ttk.Entry(style="Search.entry", width=20)
e2 = ttk.Entry(style="Search.entry", width=20)

e1.grid(padx=10, pady=10)
e2.grid(padx=10, pady=10)

root.mainloop()
78 changes: 78 additions & 0 deletions Demo/tkinter/ttk/notebook_closebtn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""A Ttk Notebook with close buttons.
Based on an example by patthoyts, http://paste.tclers.tk/896
"""
import os
import tkinter
from tkinter import ttk

root = tkinter.Tk()

imgdir = os.path.join(os.path.dirname(__file__), 'img')
i1 = tkinter.PhotoImage("img_close", file=os.path.join(imgdir, 'close.gif'))
i2 = tkinter.PhotoImage("img_closeactive",
file=os.path.join(imgdir, 'close_active.gif'))
i3 = tkinter.PhotoImage("img_closepressed",
file=os.path.join(imgdir, 'close_pressed.gif'))

style = ttk.Style()

style.element_create("close", "image", "img_close",
("active", "pressed", "!disabled", "img_closepressed"),
("active", "!disabled", "img_closeactive"), border=8, sticky='')

style.layout("ButtonNotebook", [("ButtonNotebook.client", {"sticky": "nswe"})])
style.layout("ButtonNotebook.Tab", [
("ButtonNotebook.tab", {"sticky": "nswe", "children":
[("ButtonNotebook.padding", {"side": "top", "sticky": "nswe",
"children":
[("ButtonNotebook.focus", {"side": "top", "sticky": "nswe",
"children":
[("ButtonNotebook.label", {"side": "left", "sticky": ''}),
("ButtonNotebook.close", {"side": "left", "sticky": ''})]
})]
})]
})]
)

def btn_press(event):
x, y, widget = event.x, event.y, event.widget
elem = widget.identify(x, y)
index = widget.index("@%d,%d" % (x, y))

if "close" in elem:
widget.state(['pressed'])
widget.pressed_index = index

def btn_release(event):
x, y, widget = event.x, event.y, event.widget

if not widget.instate(['pressed']):
return

elem = widget.identify(x, y)
index = widget.index("@%d,%d" % (x, y))

if "close" in elem and widget.pressed_index == index:
widget.forget(index)
widget.event_generate("<<NotebookClosedTab>>")

widget.state(["!pressed"])
widget.pressed_index = None


root.bind_class("TNotebook", "<ButtonPress-1>", btn_press, True)
root.bind_class("TNotebook", "<ButtonRelease-1>", btn_release)

# create a ttk notebook with our custom style, and add some tabs to it
nb = ttk.Notebook(width=200, height=200, style="ButtonNotebook")
nb.pressed_index = None
f1 = tkinter.Frame(nb, background="red")
f2 = tkinter.Frame(nb, background="green")
f3 = tkinter.Frame(nb, background="blue")
nb.add(f1, text='Red', padding=3)
nb.add(f2, text='Green', padding=3)
nb.add(f3, text='Blue', padding=3)
nb.pack(expand=1, fill='both')

root.mainloop()
Loading

0 comments on commit a7d2797

Please sign in to comment.