Graphics Programming with Python. Many choices Python offers us several library choices: Tkinter ...

12
Graphics Programming with Python

Transcript of Graphics Programming with Python. Many choices Python offers us several library choices: Tkinter ...

Page 1: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Graphics Programming with Python

Page 2: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Many choices Python offers us several library choices:

Tkinter WxPython PyQt PyGTK Jython And others...

Page 3: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Tkinter Tkinter is the 'standard' library for

programming Python GUI's because it is: Accessible (lightweight and easy-to-use) Portable (runs cross-platform) Available (standard module in the

Python library) Well-documented

Python's interface to tk, GUI library for Tcl

Page 4: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Code Example from Tkinter import Label widget = Label(None, text='Hello World!') widget.pack() widget.mainloop()

Page 5: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Hello World! Create new label, placed in highest level

window Default arrangement (top side) mainloop() shows window and starts

event handling

Page 6: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Packing pack() method invokes geometry

manager which controls layout 'widgets' are arranged within containers

(window, frame, etc.) Containers within containers →

hierarchical GUI display Grid geometry as alternative

Page 7: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Code Example from Tkinter import * Label(text='Hello

World!').pack(expand=YES, fill=BOTH) mainloop()

Page 8: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Resizing Windows can be resized by default Expand causes all available space within

a container to be allocated to this widget As consequence, centers widget if alone Fill makes the widget physically stretch to

fill this space (BOTH means both horizontally(X) and vertically (Y))

Page 9: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Code Example from Tkinter import * widget = Label() widget['text'] = 'Hello World!' widget.pack(side=TOP) mainloop()

Page 10: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Code Example from Tkinter import * root = tk() widget = Label(root) widget.config(text='Hello World!') widget.pack(side=TOP, expand=YES) root.title('MyWindow') root.mainloop()

Page 11: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Code Example import sys from Tkinter import * widget = Button(None, text='Goodbye!',

command=sys.exit) widget.pack(side=RIGHT) widget.mainloop()

Page 12: Graphics Programming with Python. Many choices Python offers us several library choices:  Tkinter  WxPython  PyQt  PyGTK  Jython  And others...

Binding Events Def haha():

print 'Hahahahaha!' widget = Button(None, text='HAHA') widget.bind('<Button-1>', haha) Now, clicking this button(left) will cause

your computer to laugh at you