From a5fdf514ed77acd1ae4bf1c3442d750a921d8735 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 28 Jul 2012 11:34:24 -0400 Subject: Added PyOhio2012 presentation source code --- pyohio2012/pyohio.prj | 1 + pyohio2012/src/all_widgets.py | 53 ++++++++++++ pyohio2012/src/canvas.py | 29 +++++++ pyohio2012/src/canvas_with_mouse.py | 43 ++++++++++ pyohio2012/src/notebook.py | 36 ++++++++ pyohio2012/src/treeview.py | 60 +++++++++++++ pyohio2012/src/ttk_widgets.py | 43 ++++++++++ pyohio2012/src/without_ttk.py | 39 +++++++++ pyohio2012/tkinter.textile | 165 ++++++++++++++++++++++++++++++++++++ 9 files changed, 469 insertions(+) create mode 100644 pyohio2012/pyohio.prj create mode 100644 pyohio2012/src/all_widgets.py create mode 100644 pyohio2012/src/canvas.py create mode 100644 pyohio2012/src/canvas_with_mouse.py create mode 100644 pyohio2012/src/notebook.py create mode 100644 pyohio2012/src/treeview.py create mode 100644 pyohio2012/src/ttk_widgets.py create mode 100644 pyohio2012/src/without_ttk.py create mode 100644 pyohio2012/tkinter.textile (limited to 'pyohio2012') diff --git a/pyohio2012/pyohio.prj b/pyohio2012/pyohio.prj new file mode 100644 index 0000000..3a5108f --- /dev/null +++ b/pyohio2012/pyohio.prj @@ -0,0 +1 @@ +{"Name":"pyohio (None)","Root":{"Name":"+pyohio (None)","Files":[],"Folders":[{"Name":"+Presentation","Files":[{"filename":"./tkinter.textile","enabled":"1"}],"Folders":[]},{"Name":"+Source","Files":[{"filename":"./src/notebook.py","enabled":"1"},{"filename":"./src/treeview.py","enabled":"1"},{"filename":"./src/canvas.py","enabled":"1"},{"filename":"./src/canvas_with_mouse.py","enabled":"1"},{"filename":"./src/without_ttk.py","enabled":"1"},{"filename":"./src/all_widgets.py","enabled":"1"},{"filename":"./src/ttk_widgets.py","enabled":"1"}],"Folders":[]}]},"Options":{"Type": 0,"Architecture": 0,"Auto Makefile": 1,"Windows GUI": 0,"Build Dependencies": 1,"Target":"None","Build Directory":"build","Module Directory":"modules","Working Directory":"","Command Line Arguments":"","Keep Console":"true","Build Before Launch":"true","Library Directories":["Default Library Directory"],"Include Directories":["Default Include Directory"],"Fortran Flags":"","C Flags":"","Link Flags":""},"Tools":[{"Name":"Python 2.7","Command":"C:/Python/Python27/python.exe","Directory":"src","Open Console":"1"},{"Name":"Python 3.2","Command":"C:/Python/Python32/python.exe","Directory":"src","Open Console":"1"},{"Name":"Build Presentation","Command":"cmd /c slideshow -o presentation -t c:/workspace/present/approx/approx.txt tkinter.textile","Directory":"","Open Console":"1"},{"Name":"Command Prompt","Command":"cmd.exe","Directory":"src","Open Console":"0"}]} \ No newline at end of file diff --git a/pyohio2012/src/all_widgets.py b/pyohio2012/src/all_widgets.py new file mode 100644 index 0000000..a406a52 --- /dev/null +++ b/pyohio2012/src/all_widgets.py @@ -0,0 +1,53 @@ +from Tkinter import * + +class AllWidgets(): + def __init__(self, root): + + self.frame = Frame(root) + + self.build_window() + + self.frame.pack() + + menubar = Menu(root) + root['menu'] = menubar + + menu_file = Menu(menubar) + menu_file.add_command(label="Quit", command=self.quit) + + menubar.add_cascade(menu=menu_file, label='File') + + + def build_window(self): + Label(self.frame, text="Label").pack(side=TOP) + Button(self.frame, text="Button").pack(side=TOP) + + Entry(self.frame, text="Entry").pack(side=TOP) + lb = Listbox(self.frame, height=3) + lb.pack(side=TOP) + for x in ('Listbox 1', 'Listbox 2', 'Listbox 3'): + lb.insert('end', x) + #RadioButton(self.frame, text="Radio Button").pack(side=TOP) + LabelFrame(self.frame, text="Label Frame").pack(side=TOP) + Scale(self.frame, from_=0, to=200, orient=HORIZONTAL).pack(side=TOP) + Scrollbar(self.frame, orient=HORIZONTAL).pack(side=TOP, fill=X) + Spinbox(self.frame, from_=0, to=200).pack(side=TOP) + p = PhotoImage(file="image.gif") + l=Label(self.frame, image=p) + l.image = p + l.pack(side=TOP) + + t = Text(self.frame, height=5, width=20) + t.pack(side=TOP) + t.insert(END, "An editable\ntext widget") + + + def quit(self): + self.frame.quit() + +if __name__ == '__main__': + root = Tk() + + app = AllWidgets(root) + + root.mainloop() diff --git a/pyohio2012/src/canvas.py b/pyohio2012/src/canvas.py new file mode 100644 index 0000000..54b4c72 --- /dev/null +++ b/pyohio2012/src/canvas.py @@ -0,0 +1,29 @@ +from tkinter import * +from tkinter.ttk import * + +class TkCanvas(): + def __init__(self, root): + + self.frame = Frame(root) + self.canvas = Canvas(self.frame) + self.canvas.grid(row=0, column=0) + + self.draw_rect() + + quit_button = Button(self.frame, text='Quit', command=self.quit) + quit_button.grid(row=1, column=0, sticky=(S,E)) + + self.frame.pack(fill=BOTH, expand=True) + + def draw_rect(self): + self.canvas.create_rectangle((100,100), (30,40)) + + def quit(self): + self.frame.quit() + +if __name__ == '__main__': + root = Tk() + + app = TkCanvas(root) + + root.mainloop() \ No newline at end of file diff --git a/pyohio2012/src/canvas_with_mouse.py b/pyohio2012/src/canvas_with_mouse.py new file mode 100644 index 0000000..41eac41 --- /dev/null +++ b/pyohio2012/src/canvas_with_mouse.py @@ -0,0 +1,43 @@ +from tkinter import * +from tkinter.ttk import * + +class TkCanvas(): + def __init__(self, root): + + self.frame = Frame(root) + self.canvas = Canvas(self.frame) + self.canvas.grid(row=0, column=0, sticky=(N,S,E,W)) + + self.draw_rect() + + quit_button = Button(self.frame, text='Quit', command=self.quit) + quit_button.grid(row=1, column=0, sticky=(S,E)) + + self.frame.columnconfigure(0, weight=2) + self.frame.rowconfigure(0, weight=2) + + self.frame.pack(fill=BOTH, expand=True) + + self.canvas.bind("", self.start_line) + self.canvas.bind("", self.draw_line) + self.initial = (0,0) + + def start_line(self, event): + self.initial = (event.x, event.y) + + def draw_line(self, event): + self.canvas.create_line((self.initial[0], self.initial[1], event.x, event.y)) + self.initial = (event.x, event.y) + + def draw_rect(self): + self.canvas.create_rectangle((100,100), (30,40)) + + def quit(self): + self.frame.quit() + +if __name__ == '__main__': + root = Tk() + + app = TkCanvas(root) + + root.mainloop() \ No newline at end of file diff --git a/pyohio2012/src/notebook.py b/pyohio2012/src/notebook.py new file mode 100644 index 0000000..326d0c7 --- /dev/null +++ b/pyohio2012/src/notebook.py @@ -0,0 +1,36 @@ +from tkinter import * +from tkinter.ttk import * + +class TkNotebook(): + def __init__(self, root): + + self.frame = Frame(root) + self.notebook = Notebook(self.frame) + + f1 = Frame(self.notebook, width=200, height=100) + f2 = Frame(self.notebook, width=200, height=100) + self.notebook.add(f1, text="Page 1") + self.notebook.add(f2, text="Page 2") + + self.label = Label(self.frame, text="A Notebook on the left") + + # Layout + self.frame.grid(row=0, column=0, sticky=(N, S, E, W)) + self.notebook.grid(row=0, column=0, sticky=(N, S, E, W)) + self.label.grid(row=0, column=1, sticky=(E,)) + + # Resize rules + root.columnconfigure(0, weight=1) + root.rowconfigure(0, weight=1) + self.frame.columnconfigure(0, weight=2) + self.frame.rowconfigure(0, weight=2) + + def quit(self): + self.frame.quit() + +if __name__ == '__main__': + root = Tk() + + app = TkNotebook(root) + + root.mainloop() \ No newline at end of file diff --git a/pyohio2012/src/treeview.py b/pyohio2012/src/treeview.py new file mode 100644 index 0000000..b545245 --- /dev/null +++ b/pyohio2012/src/treeview.py @@ -0,0 +1,60 @@ +from tkinter import * +from tkinter.ttk import * + +class TkTrees(): + def __init__(self, root): + self.root = root + + self.notebook = Notebook(root) + + f1 = Frame(self.notebook) + f2 = Frame(self.notebook) + self.notebook.add(f1, text="Columned List") + self.notebook.add(f2, text="Tree") + + self.column_treeview(f1) + self.tree_treeview(f2) + + # Layout + self.notebook.grid(row=0, column=0, sticky=(N, S, E, W)) + + # Resize rules + root.columnconfigure(0, weight=1) + root.rowconfigure(0, weight=1) + + def column_treeview(self, parent): + + columns = ('Name', 'Description') + + self.ctree = Treeview(parent, columns=columns, show="headings") + for col in columns: + self.ctree.column(col, width=90) + self.ctree.heading(col, text=col) + + self.ctree.insert('', 'end', values=('Example', 'One row...')) + self.ctree.insert('', 'end', values=('Example Part 2', 'Another row...')) + + self.ctree.pack(fill=BOTH, expand=1) + + def tree_treeview(self, parent): + + self.ttree = Treeview(parent) + + id = self.ttree.insert('', 'end', text='Example') + self.ttree.insert(id, 'end', text='First sub item') + + id = self.ttree.insert('', 'end', text='Example Part 2') + self.ttree.insert(id, 'end', text='A different sub item') + + self.ttree.pack(fill=BOTH, expand=1) + + + def quit(self): + self.root.quit() + +if __name__ == '__main__': + root = Tk() + + app = TkTrees(root) + + root.mainloop() \ No newline at end of file diff --git a/pyohio2012/src/ttk_widgets.py b/pyohio2012/src/ttk_widgets.py new file mode 100644 index 0000000..0aad592 --- /dev/null +++ b/pyohio2012/src/ttk_widgets.py @@ -0,0 +1,43 @@ +try: + from Tkinter import * + from ttk import * +except ImportError: + from tkinter import * + from tkinter.ttk import * + +class TtkWidgets(): + def __init__(self, root): + + self.frame = Frame(root) + + self.build_window() + + self.frame.pack() + + menubar = Menu(root) + root['menu'] = menubar + + menu_file = Menu(menubar) + menu_file.add_command(label="Quit", command=self.quit) + + menubar.add_cascade(menu=menu_file, label='File') + + def build_window(self): + cbv = StringVar() + Combobox(self.frame, textvariable=cbv, + values=('Canada', 'Sweden', 'Germany')).pack(side=TOP) + + Checkbutton(self.frame, text='Check').pack(side=TOP) + Radiobutton(self.frame, text='Radio').pack(side=TOP) + pb = Progressbar(self.frame, orient=HORIZONTAL, length=200, mode='indeterminate') + pb.start() + + def quit(self): + self.frame.quit() + +if __name__ == '__main__': + root = Tk() + + app = TtkWidgets(root) + + root.mainloop() diff --git a/pyohio2012/src/without_ttk.py b/pyohio2012/src/without_ttk.py new file mode 100644 index 0000000..b5da854 --- /dev/null +++ b/pyohio2012/src/without_ttk.py @@ -0,0 +1,39 @@ +from Tkinter import * + +class WithoutTtk(): + def __init__(self, root): + + self.frame = Frame(root) + + self.build_window() + + self.frame.pack(fill=BOTH) + + menubar = Menu(root) + root['menu'] = menubar + + menu_file = Menu(menubar) + menu_file.add_command(label="Quit", command=self.quit) + + menubar.add_cascade(menu=menu_file, label='File') + + + def build_window(self): + label = Label(self.frame, text="How do I look?") + label.pack(side=TOP) + + button_bad = Button(self.frame, text="Terrible", command=self.quit) + button_bad.pack(side=LEFT) + + button_good = Button(self.frame, text="Not too shabby", command=self.quit) + button_good.pack(side=RIGHT) + + def quit(self): + self.frame.quit() + +if __name__ == '__main__': + root = Tk() + + app = WithoutTtk(root) + + root.mainloop() diff --git a/pyohio2012/tkinter.textile b/pyohio2012/tkinter.textile new file mode 100644 index 0000000..e3afcff --- /dev/null +++ b/pyohio2012/tkinter.textile @@ -0,0 +1,165 @@ +title: An Introduction to Tkinter +subtitle: A User's Perspective +author: Jeffrey B. Armstrong +company: Approximatrix, LLC +footer: PyOhio +subfooter: 28 July 2012 + +h1. Introduction + +* Tkinter (or tkinter) +** Interface to Tcl/Tk +** The only GUI framework in the Python standard library +** Simple to use +* The speaker +** Casual Tkinter user +** Some experience with other GUI toolkits + +h1. Opinions + +* Three types of people here +** Never used it +** Use it and Like it +** Hate it + +h1. Why Care? + +* Tkinter is part of the Python standard library +* Lightweight +* _Good enough_ +** In fact, it can be great! + +h1. The Past + +* Limited widget set +* Unweildy _import_ +* Considered ugly + +h1. The Present + +* Python 3.x +** Changed to Pythonic namespaces, like _tkinter_ +** Introduced... + +h1. Themed Tk + +* `ttk` module +* Makes old Tkinter look pretty +** Kinda platform-specific... + +h1. Getting Started + +* Tk operates in "loop" that is transparent to the developer +* Widgets are assigned parents, which contain other widgets, etc. +* Layout managers handle widget placement +* Widgets can have events "bound" to them and respond accordingly + +h1. Layout Managers + +* Tkinter provides three geometry managers +** absolute (avoid this) +** pack +** grid + +h1. pack Layout + +* Extremely simple +* Specify a side +* Parent sizes itself to minimally fit everything +* Not particularly flexible for complex layouts + +h1. grid Layout + +* Closer to other toolkits +* Frames are laid out in a grid +** Resizing can be handled via _expand_ arguments + +h1. Widgets + +* tkinter historically provides common, simple widgets +* Considered a shortcoming +** Missing more advanced widgets + +h1. Widgets + +!{border:0px; float:right; height:14em}widgets.png! + + + + + + +
ButtonMenu
FrameScale
LabelScrollbar
Label with ImageSpinbox
ListboxText
+ +h1. ttk Widgets + +* "Overrides" drawing of tkinter widgets +** Drawing now handled by theme engine +* Introduces new, modern widgets +* Change the "style" +** _ttk.Style().theme_names()_ +** _ttk.Style().theme_use(style)_ + +h1. ttk Notebook + +* Provides modern paged notebook of frames, accesible via tabs +* Sensible functionality + +h1. ttk Treeview + +* Implements tree, list, columned list, etc. + +h1. ttk et cetera + +!{border:0px; float:right; height:10em}ttk_widgets.png! + +* Combobox +* CheckButton +* RadioButton +* Progressbar +* Separator +* Sizegrip + +h1. Canvas + +* Allows for simple primitive drawing within a Tk widget +* Opens up the door for custom widget behavior + +h1. Events + +* Tkinter events are relatively simple +** For buttons: _Button(text='Button Name', command=button_callback)_ +* For more complex components, _bind_ method can be used + +h1. Standard Dialogs + +* File dialog +** _tkinter.filedialog_ or _tkFileDialog_ +* Color chooser +** _tkinter.colorchooser_ or _tkColorChooser_ +* Message box +** _tkinter.messagebox_ or _tkMessageBox_ + +h1. 2.7 and 3.x + +* Python 2.7 and 3.x both contain Themed Tk +** Cross-version applications +* Python 2.x has different Tkinter namespace +** Python 3.x made tkinter slightly more Pythonic +** Need some ImportError exception magic for cross-version code + +h1. Documentation + +* Python library documentation on tkinter is arguably sparse +** ttk, however, is well-documented + +* http://www.tkdocs.com/ +** Reference for Tk in Tcl, Ruby, OCaml, and *Python* + +h1. Summary + +* Tkinter can be pretty sweet! +** Ships with (almost) all Python ports +** Now with modern widgets +* "Ugly" is gone with ttk +* Easy to build a quick GUI -- cgit v1.2.3