gui design with python – examples from crystallography bernhard lohkamp karolinska institute...

78
GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

Post on 20-Dec-2015

226 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

GUI design with Python –examples from crystallography

Bernhard Lohkamp

Karolinska Institute

Stockholm

Sweden

Page 2: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Coot - Who are we – why am I here?

Paul Emsley

(Oxford; everything)

Bernhard Lohkamp

(Stockholm; Python, GUI, Windows)

Kevin Cowtan (York;

crystallographic libraries & tools)

Eugene Krissinel, Stuart McNicholas, and others...

Page 3: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

COOT

Graphical (Crystallographic-Object)-Oriented Toolkit

Used in macromolecular (X-ray) crystallography

Main aims:Model buildingModel completionModel validation

Graphical, interactive, intuitive, …

Page 4: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

G(raphical) U(ser) I(nterface)GUI <-> UI Allow end users to interact with application Interface can be simple (e.g. command line) to

complex (graphical) Good UI:

IntuitiveEasy to use/learn

Note: applications with good UI are preferred to ones with inferior ones (independent of quality of application!?)

Page 5: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

What do you prefer?

>>> delete_residue(10)

Page 6: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

GUIs

Good things about GUIIntuitive (if designed properly)More efficient (e.g., putting the cursor at a particular

location)Multiple activities simultaneously

Bad things about GUIInefficient (if mis-designed)Difficult to automate (e.g. difficult to write a program

to push a button in another application)Speed

Page 7: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

The challenge of GUI programming

programming model must address the following issues:user allowed to perform different things (e.g. type a character,

type a hot-key, click on a button, resize the window, obscure the window with another application, minimize it, etc.)

program needs to adapt to different window size (and with it all its content)

Solution:event-driven modelwidget systems (controlled by a geometry manager)

Use Toolkits == TK

Page 8: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Forms of interactionevents in context

Human Interface Device (HID)

control

Windows manager/Desktop

Page 9: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Windowing system/hierarchy Lots of layers Not very efficient Slow (not necessary to be fast?!)

Our script (in Python): Real application

TK (in Python): glue to TK

TK Python plugin (usually in C): translate to TK calls

TK widgets (usually in C): Widget implementation

TK library (usually C): glue to windowing system

Windows managing system (e.g. X)

Page 10: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Connect GUI with Python

Direct via script GUI design tools and

IDEs (Integrated Development Environments)GUI builderGUI to build a GUI

applicationUseful especially for larger

projectsOutput usually xml

Use directly Translate to Python script

Page 11: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

GUI modules for Python (I)(Toolkits, cross-platform) PyGTK (Gnome): Coot

TK: GTK+ (GIMP Tool Kit)Well supportedBuilder (Glade)

xml directNot native on Mac (yet)

PyQt (KDE): CCP4mg – molecular graphicsTK: Qt (“cu-te”)Licence issue?NativeBuilder (Designer)

xml->python

Page 12: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

GUI modules for Python (II)

Tk(Inter): PyMOLTK: TkTkinter Python Tk interfaceDistributed with pythonNot OOBuilder (GUI builder)

wxPython: PhenixTK: wxWidgetsBuilder (wxGlade)Native?

Page 13: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Nomenclature/Widgets

Widget: “windowing gadget”, which means “a useful building block (gadget) to make a windowing system”.

Hierarchy (exemplified):

Page 14: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Widget examples

Top level (independent widgets):Main windowVarious (pre-

defined) dialogs

Page 15: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Dialogs (I)

Info (Message) dialog

Dialog box

Page 16: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Dialogs (II)

About dialog

Assistant

Page 17: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Dialogs (III)

Selection dialogsFilesColoursFonts….

Page 18: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Containers/Layout

BoxesVerticalHorizontalCombined (grid, table)

Scrolled window Tabbed

widgets/notebook Frames Panes

Page 19: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Boxed layout

Page 20: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Tabbed window/Notebook tabs

Page 21: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Buttons

Button Toggle button Check button Spin button Radio button Pre-defined buttons

FilesColoursFonts…

Page 22: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Input & Entries (I)

Menu

Toolbar

Page 23: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Menus

Submenus Icons Accelerators

Page 24: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Context menu

Shown upon event (usually mouse click)

Page 25: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Toolbars

Like menus but contain icons/buttons Short-cut for frequently used functions

Page 26: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Toolbar

Page 27: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Change

Style

Page 28: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Text

only

Page 29: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Text

and

Icons

Page 30: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Toolbar

Detouch/

Handle

Page 31: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

detouched

Page 32: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Input & Entries (I)

Combobox – spin button

Simple Entry:

Page 33: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Input & Entries (II)

Entry:Auto-completion possible

Sliders

Page 34: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

List/Tree structures

Page 35: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Can include: icons, buttons, …

Page 36: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Tree-> expansion

Page 37: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Miscellaneous widgets

Progress bar

Status bar

Page 38: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Canvas

Drawing area Canvas/cairo drawing

(e.g. PyGoocanvas) OpenGL drawing (e.g.

PyOpenGL, PyGtkGLExt)

Can be interactive

Page 39: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Tooltips

Pop ups with text (upon mouse over)

Helps to explore the GUI without reading help files

Use whenever you can (!?)

Page 40: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

GUI designWhat makes a good UI?

Simple Intuitive Respects the commonly accepted conventions Visually organized Native look

Page 41: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Designing UIs

Two levels:Graphical: visual aspectEvents: Functionality

Basic blocks = widgets Making an application react to events = binding

Page 42: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

General concepts

Main loop and events Packing Showing

How to do.

Page 43: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Main loop and events

TK mainloop:Event loop Idle until an event happens (e.g. a button is pushed, a menu

is pulled, etc.)Quit when program (GUI) finishesCan have multiple loops (count!!)

PyGTK: gtk.main() TKinter: tk.mainloop() PyQT:application.exec_()

Page 44: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Main loop and events

Events (examples):Keyboard (key-pressed, key-released)Mouse (button-pressed, button-released, motion, wheel, …) Window/widget (resize, destroy, visibility, activate, deactivate, …)Etc.

Event modifiers (Shift, Alt, …) Events emit signals Can be connected with callbacks (functions, methods)

Page 45: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Callbacks and signals

Callback functions/bindings: functions, methods do something when event happens (e.g. button pressed)

PyGTK:object.connect(signal_name, callback_func, func_data)def callback_func(widget, callback_data):def callback_meth(self, widget, callback_data):

Tkinter:Button(master, text="OK", command=callback)def callback_func():

PyQT:connect(widget, signal, callback_func)

Page 46: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Synergy between objects and widgets

Variables are passed automatically within class refer to them as self.whatever No need to pass variables

Callback functionsEasy to handleNo need to define callbacks on the fly (lambda functions)

Page 47: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Packing Add widget to container Pack widget in boxes

PyGTK:container_widget.add(widget)box_object.pack_start(child, expand, fill, padding)

Tkinter:object.pack(various_options)

PyQT:Layout.addWidget(widget)

Page 48: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Packing

Homogeneous (equal space for everyone) HBox(True/False, 0)

Expand, fill box.pack(expand=, fill=)

Page 49: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Showing Widgets itself do not show (neither do they do anything else ->

connect signals!) Need to show every (!) widget Can hide widgets Usually collective show for all

PyGTK:widget.show()widget.hide()widget.show_all()

Tkinter:Done via mainloop

PyQT:widget.show()widget.hide()

Page 50: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Simple example

GUI command input

Schematic in terms of widgets

window

VBox

HBoxLabel Entry

ScrolledWindowTextBuffer

Button

Page 51: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Simple example- make window

# import gtk module

import gtk

# create a main window

window = gtk.Window(gtk.WINDOW_TOPLEVEL)

# set the title

window.set_title("Coot Python Scripting")

# set some more properties (if we wish)

window.set_default_size(400,250)

Page 52: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Simple example- create widgets

# create other needed widgets: boxes, label, entry, …

vbox = gtk.VBox(homogeneous=False, spacing=0)

hbox = gtk.HBox(False, 0)

label = gtk.Label("Command: ")

entry = gtk.Entry()

scrolled_win = gtk.ScrolledWindow()

text = gtk.TextView()

textbuffer = text.get_buffer()

close_button = gtk.Button(" Close ")

Page 53: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Simple example- pack it in

# add vbox in window

window.add(vbox)

# pack the hbox and pack into the vbox

hbox.pack_start(child=label, expand=False, fill=False, padding=0)

hbox.pack_start(entry, True, True, 0)

vbox.pack_start(hbox, False, False, 5)

window

VBox

HBoxLabel Entry

ScrolledWindowTextBuffer

Button

Page 54: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Simple example- pack it in

# pack the scrolled text area

scrolled_win.add(text)

vbox.add(scrolled_win)

# and finally the button

vbox.pack_end(close_button, False, False, 5)

window

VBox

HBoxLabel Entry

ScrolledWindowTextBuffer

Button

Page 55: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Simple example- show it

# show everything connected to the window

window.show_all()

# run the main loop

gtk.main()

Page 56: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Simple example- making it functional (close button)

Does not perform any function (yet) Main loop never stops connect callbacks and signals

# connect close_button (no further args)

close_button.connect(“clicked”, close_callback)

# define callback: destroy window, quit gtk loop

def close_callback(widget):

window.destroy()

gtk.main_quit()

Page 57: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Simple example- making it functional (destroy window)

Closing window does not finish gtk loop Deal with delete event

# connect delete_event (two args, but no extra)

window.connect(“delete_event”, delete_event_cb)

# define callback: quit gtk main loop

def delete_event_cb(widget, event):

gtk.main_quit()

Page 58: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Simple example- making it functional (entry)

# do something with entry when enter is pressed

entry.connect(“activate”, do_callback)

# what to do

def do_callback(widget):

entry_text = widget.get_text()

print “input is”, entry_text

# erase the entry

widget.set_text(“”)

# do something with the text,

# e.g. put in text

# scrolled window

end = textbuffer.get_end_iter()

textbuffer.insert(end, str(entry_text + "\n"))

Page 59: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Glade (GUI builders)

Editor

Properties

Inspector

Palette

Page 60: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Glade

Output xml file Two different ‘formats’ (libglade, builder)

==> my_test3.libglade <==

<?xml version="1.0"?>

<glade-interface>

<!-- interface-requires gtk+ 2.16 -->

<!-- interface-naming-policy project-wide -->

<widget class="GtkWindow" id="window1">

<child>

<widget class="GtkVBox" id="vbox1">

<property name="visible">True</property>

<property name="orientation">vertical</property>

==> my_test3.glade <==

<?xml version="1.0"?>

<interface>

<requires lib="gtk+" version="2.16"/>

<!-- interface-naming-policy project-wide -->

<object class="GtkWindow" id="window1">

<child>

<object class="GtkVBox" id="vbox1">

<property name="visible">True</property>

<property name="orientation">vertical</property>

Page 61: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Connect to python script Keep GUI/widgets and events separate Useful for larger projects (easy to add, remove, clean)

import pygtk, gtk

builder = gtk.Builder()

builder.add_from_file(“my_file.glade”)

# get the main window and connect event

window = builder.get_object(“window1”)

window.connect("destroy", gtk.main_quit)

# show and run

window.show_all()

gtk.main()

Page 62: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Glade – connect more signals

Use automatic connection

# dictionary of callbacks (name of signal,

# callback function)

dic = { "on_button1_clicked" : button1_cb,

"on_button2_toggled" : button2_cb,

"on_window1_destroy" : gtk.main_quit }

# define some callbacks

def button1_cb(widget): print “button 1 pressed”

def button2_cb(widget): print “button 2 toggled”

# connect the signals

builder.connect_signals(dic)

Page 63: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

(lib)glade

same principle (as for gtk.Builder) slightly different syntax

Page 64: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

The higher end implementationsCombining C/C++/Python/GTK

Python

Page 65: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Pythoninterface

Pythonfunctions

SWIG

Coot and Python

C(++)-functions

Python functions(objects)

Gtk+ objects(graphics)

call

return value

Pythonscripting functions

Page 66: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Main Menubar

Main Menubar

Page 67: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Addition of individual menu items

Accessing existing menus

Page 68: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Main Toolbar

Main Toolbar

Page 69: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Extra toolbuttons

Main Toolbar

Pop-up menu to manage toolbuttons

Page 70: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Some hints for your own design:Organize

Page 71: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Some hints for your own design:group related items

Page 72: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Some hints for your own design:keep it simple

Page 73: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Some hints for your own design:don’t reinvent the wheel – keep conventions

Use standard menu items Use standard icons Carefully choose colours:

colour blindness!!

Page 74: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Further information on GUI/Python/TKs GUI

http://www.guidebookgallery.org/index http://www.asktog.com/basics/firstPrinciples.html http://web.cs.wpi.edu/~matt/courses/cs563/talks/smartin/int_design.html http://www-01.ibm.com/software/ucd/designconcepts.html http://library.gnome.org/devel/hig-book/stable/intro.html.en

PyGTK, Glade http://www.pygtk.org/ http://glade.gnome.org/ http://live.gnome.org/Glade/Tutorials http://www.micahcarrick.com/12-24-2007/gtk-glade-tutorial-part-1.html

PyQT http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html http://www.commandprompt.com/community/pyqt/ http://www.cs.usfca.edu/~afedosov/qttut/

Page 75: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Acknowledgements

Paul Emsley Kevin Cowtan Eleanor Dodson Keith Wilson

BBSRC & CCP4 funding

Libraries, dictionaries Alexei Vagin, Eugene Krissinel, Stuart McNicholas Dunbrack, Richardsons Coot Builders and Testers William Scott, Ezra Peisach York YSBL, Dundee, Glasgow (early adopters) Coot Mailing List subscribers

http://www.biop.ox.ac.uk/coot/

or

Google: Coot

or

http://www.ysbl.ac.uk/~lohkamp/coot

Page 76: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

DEMO?

Page 77: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]

Hierarchy

Page 78: GUI design with Python – examples from crystallography Bernhard Lohkamp Karolinska Institute Stockholm Sweden

7/5/[email protected]