Python Crash Course: GUIs with Tkinter



Similar documents
Tkinter 8.5 reference: a GUI for Python

Python programming GUI

PowerPoint 2007 Basics Website:

Scientific Visualization with wxpython and Matplotlib. Scott Pearse CSCI 5448 Spring 2011

KaleidaGraph Quick Start Guide

Designing and Implementing Forms 34

The VB development environment

Introduction to Graphical User Interface (GUI) MATLAB 6.5

Solutions from SAP. SAP Business One 2005 SP01. User Interface. Standards and Guidelines. January 2006

HTML Form Widgets. Review: HTML Forms. Review: CGI Programs

Creating Fill-able Forms using Acrobat 8.0: Part 1

GUI application set up using QT designer. Sana Siddique. Team 5

Excel 2007 Basic knowledge

Formulas, Functions and Charts

INTRODUCTION TO DESKTOP PUBLISHING

Appendix 2.1 Tabular and Graphical Methods Using Excel

Microsoft Office PowerPoint 2013

Microsoft PowerPoint 2011

Python Documentation & Startup

Introduction To Microsoft Office PowerPoint Bob Booth July 2008 AP-PPT5

Creating Web Pages with Microsoft FrontPage

Microsoft Office Excel 2007 Key Features. Office of Enterprise Development and Support Applications Support Group

Writer Guide. Chapter 15 Using Forms in Writer

Chapter 15 Using Forms in Writer

Microsoft Access 2010 Overview of Basics

Introduction to MS WINDOWS XP

DNNCentric Custom Form Creator. User Manual

Using Microsoft Word. Working With Objects

JOOMLA 2.5 MANUAL WEBSITEDESIGN.CO.ZA

Microsoft PowerPoint Tutorial

Introduction to the TI-Nspire CX

Rapid Application Development with GNOME and Python

Project 1 - Business Proposal (PowerPoint)

The key to successful web design is planning. Creating a wireframe can be part of this process.

Excel 2007: Basics Learning Guide

Working with SmartArt

Creating Interactive PDF Forms

Graphing Quadratic Functions

Microsoft Publisher 2010 What s New!

KIVY - A Framework for Natural User Interfaces

Declarative API for Defining Visualizations in Envision

Creating Charts in Microsoft Excel A supplement to Chapter 5 of Quantitative Approaches in Business Studies

Table of Contents. I. Banner Design Studio Overview II. Banner Creation Methods III. User Interface... 8

CONTENTM WEBSITE MANAGEMENT SYSTEM. Getting Started Guide

DataPA OpenAnalytics End User Training

paragraph(s). The bottom mark is for all following lines in that paragraph. The rectangle below the marks moves both marks at the same time.

Chaco: A Plotting Package for Scientists and Engineers. David C. Morrill Enthought, Inc.

GAMBIT Demo Tutorial

Create Charts in Excel

Website Development Komodo Editor and HTML Intro

Ulf Hermann. October 8, 2014 / Qt Developer Days The Qt Company. Using The QML Profiler. Ulf Hermann. Why? What? How To. Examples.

understand how image maps can enhance a design and make a site more interactive know how to create an image map easily with Dreamweaver

Introduction to dobe Acrobat XI Pro

Using Adobe Dreamweaver CS4 (10.0)

Unit 21 - Creating a Button in Macromedia Flash

ImageNow Document Management Created on Friday, October 01, 2010

Microsoft Office PowerPoint Creating a new presentation from a design template. Creating a new presentation from a design template

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

MICROSOFT POWERPOINT STEP BY STEP GUIDE

VISUAL BASIC Controls. Version 1 Trg. Div., NIC HQRs

Microsoft PowerPoint 2010 Computer Jeopardy Tutorial

ACS Version Check Layout Design

Welcome to CorelDRAW, a comprehensive vector-based drawing and graphic-design program for the graphics professional.

Microsoft PowerPoint 2010

Assignment 1 supplement: Using Microsoft Excel

If you know exactly how you want your business forms to look and don t mind

About PivotTable reports

Microsoft Word Quick Reference Guide. Union Institute & University

Smart Connection 9 Element Labels

Microsoft Excel Basics

9 CREATING REPORTS WITH REPORT WIZARD AND REPORT DESIGNER

Introduction to Microsoft Word 2008

Quick Start Guide. Microsoft Access 2013 looks different from previous versions, so we created this guide to help you minimize the learning curve.

Microsoft Power Point 2007 Study Guide PowerPoint The Microsoft Office Button The Quick Access Toolbar The Title Bar

Application Developer Guide

PowerPoint 2007: Animations Contents

Countdown Alarm Clock. GUI Programming with wxhaskell. Widgets used. UI Creation Code. Wei Tan

Lecture 2 Mathcad Basics

If you know exactly how you want your business forms to look and don t mind detail

Building Applications With DUIM

Education Solutions Development, Inc. APECS Navigation: Business Systems Getting Started Reference Guide

Intermediate PowerPoint

KB COPY CENTRE. RM 2300 JCMB The King s Buildings West Mains Road Edinburgh EH9 3JZ. Telephone:

Brock University Content Management System Training Guide

Acrobat X Pro Accessible Forms and Interactive Documents

ADOBE ACROBAT 7.0 CREATING FORMS

Multiprocess System for Virtual Instruments in Python

Introduction to OpenOffice Writer 2.0 Jessica Kubik Information Technology Lab School of Information University of Texas at Austin Fall 2005

0 Introduction to Data Analysis Using an Excel Spreadsheet

Excel 2003 A Beginners Guide

Cyberex BCM-SFCM Configurator/Monitor User's Manual

This document will describe how you can create your own, fully responsive. drag and drop template to use in the creator.

Tutorial for Tracker and Supporting Software By David Chandler

Task Force on Technology / EXCEL

DIA Creating Charts and Diagrams

Introduction to Microsoft Excel 2007/2010

Basic tutorial for Dreamweaver CS5

Creating and Using Links and Bookmarks in PDF Documents

Transcription:

Python Crash Course: GUIs with Tkinter Do you train for passing tests or do you train for creative inquiry? Noam Chomsky Steffen Brinkmann Max-Planck-Institut für Astronomie, Heidelberg IMPRESS, 2016 1 / 40

The ingredients A working Python installation Internet connection Passion for Python If anything of the above is missing, please say so now! 2 / 40

Outline Motivation To GUI or not to GUI Getting started Terminology Hello World Overview Widget Properties and Placement Putting it together More complexity 3 / 40

Motivation To GUI or not to GUI Outline Motivation To GUI or not to GUI Getting started Terminology Hello World Overview Widget Properties and Placement Putting it together More complexity 4 / 40

Motivation To GUI or not to GUI Why use GUIs? Users are used to GUIs Easy interaction Display images, animations etc. Visualise workflow Display complex Data 5 / 40

Motivation To GUI or not to GUI Why not use GUIs? Faster development Faster execution Easier to test Can be included in scripts (pipelines) less dependences 6 / 40

Motivation To GUI or not to GUI Why Tkinter? Tkinter is the standard Python interface to the Tk GUI toolkit. No need to install additional packages Simple enough to get quick results Powerful enough for most applications Alternatives: gi (GTK), pyside (Qt), wx (wxwidgets) 7 / 40

Motivation To GUI or not to GUI Get help http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html http://www.tutorialspoint.com/python/python gui programming.htm 8 / 40

Getting started Terminology Outline Motivation To GUI or not to GUI Getting started Terminology Hello World Overview Widget Properties and Placement Putting it together More complexity 9 / 40

Getting started Terminology Terminology Widget: Visual element of interaction. Can be a container (see below), control (Button, slider,... ), a display (label, canvas,... ) or a combination of both (Text field, spin box) Box, Container: Widget that can contain one or more other widgets. 10 / 40

Getting started Terminology Terminology Event: Events are created by the program itself (destroy, resize,... ) or the user via controls (click button, mouse move,... ). Event handler: Function or method dedicated to react to events. Typically called on event. Event loop: separate execution thread or process which checks for new events. 11 / 40

Getting started Hello World Outline Motivation To GUI or not to GUI Getting started Terminology Hello World Overview Widget Properties and Placement Putting it together More complexity 12 / 40

Getting started Hello World Very simple #!/ bin /env python from future import print_function import sys if sys. version_info >= (3,): import tkinter as tk else : import Tkinter as tk top = tk.tk () top. mainloop () The Tkinter module is called differently in Python 2 and 3 The class Tk represents the Tkinter framework and is necessary for the event loop, exiting cleanly etc. The function mainloop starts the event loop. It stops when Tk().destroy is called. 13 / 40

Getting started Hello World A little less simple class Hello (tk. Frame ): def init (self, master =tk.tk ()): tk. Frame. init (self, master ) self. master = master self. grid () self. createwidgets () def createwidgets ( self ): self. button1 = tk. Button (self, text =" click me", command = self. say_hello ) self. button1.grid (row=10, column =10) self. button_quit = tk. Button (self, text =" QUIT ", fg=" red ", command = self. master. destroy ) self. button_quit.grid (row=20, column =10) def say_hello ( self ): print (" hello there, everyone!") app = Hello () app. mainloop () 14 / 40

Getting started Hello World A little less simple It is convenient to create an application class, derived from tk.frame Widgets can be added to the application window by grid(), pack() or place(). 15 / 40

Overview Outline Motivation To GUI or not to GUI Getting started Terminology Hello World Overview Widget Properties and Placement Putting it together More complexity 16 / 40

Overview Overview Frame and Buttons Frame The Frame widget is used as a container widget to organize other widgets. Button The Button widget is used to display buttons in your application. Checkbutton The Checkbutton widget is used to display a number of options as checkboxes. The user can select multiple options at a time. Radiobutton The Radiobutton widget is used to display a number of options as radio buttons. The user can select only one option at a time. http://www.tutorialspoint.com/python/python gui programming.htm 17 / 40

Overview Overview Text and Displays Label The Label widget is used to provide a single-line caption for other widgets. It can also contain images. Message The Message widget is used to display multiline text fields. Entry The Entry widget is used to display a single-line text field for accepting values from a user. Spinbox The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values. Text The Text widget is used to display text in multiple lines. Canvas The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your application. http://www.tutorialspoint.com/python/python gui programming.htm 18 / 40

Overview Overview Menus and more choices and controls Menubutton The Menubutton widget is used to display menus in your application. Menu The Menu widget is used to provide various commands to a user.these commands can be contained inside Menubutton or set as the main menu. Listbox The Listbox widget is used to provide a list of options to a user. Scale The Scale widget is used to provide a slider widget. Scrollbar The Scrollbar widget is used to add scrolling http://www.tutorialspoint.com/python/python gui programming.htm 19 / 40

Overview Overview Containers Toplevel The Toplevel widget is used to provide a separate window container. PanedWindow A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically. LabelFrame A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts. http://www.tutorialspoint.com/python/python gui programming.htm 20 / 40

Overview Overview Dialogues tkmessagebox This module is used to display message boxes in your applications. tkfiledialog This module provides two different pop-up windows you can use to give the user the ability to find existing files or create new files. tkcolorchooser Module featuring a dialogue to select a colour. http://www.tutorialspoint.com/python/python gui programming.htm 21 / 40

Widget Properties and Placement Outline Motivation To GUI or not to GUI Getting started Terminology Hello World Overview Widget Properties and Placement Putting it together More complexity 22 / 40

Widget Properties and Placement Setting Widget Properties During Initialisation button = tk. Button ( parent, text = button label, command = self. on_button ) The only positional argument parent specifies in whose grid the widget will be displayed. Options are set as keyword arguments: option=value 23 / 40

Widget Properties and Placement Setting Widget Properties Using config() button = tk. Button ( parent ) button. config ( text = button label ) button. config ( command = self. on_button ) parent still has to be set when constructing the widget. Options are set as keyword arguments in method config: config(option=value) calling config() without arguments returns a dictionary of all the widget s current options. The method cget(option) returns the value of a single option. 24 / 40

Widget Properties and Placement Setting Widget Properties Using Widget as Dictionary button = tk. Button ( parent ) button [ text ] = button label button [ command ] = self. on_button parent still has to be set when constructing the widget. Options are set as values using the string option as key. 25 / 40

Widget Properties and Placement Postioning Widgets Overview grid(): Arrange widget on e rectangular grid, i.e. in rows and columns. Recommended. pack(): Arrange widgets by stacking them vertically or horizontally. Very limited. place() Place widgets at specific coordinates. Most (too much) freedom. 26 / 40

Widget Properties and Placement Postioning Widgets class Hello (tk. Frame ): def init (self, master =tk.tk ()): tk. Frame. init (self, master ) self. master = master self. grid () self. createwidgets () def createwidgets ( self ): self. button1 = tk. Button (self, text =" click me", command = self. say_hello ) self. button1.grid (row=10, column =10) self. button_quit = tk. Button (self, text =" QUIT ", fg=" red ", command = self. master. destroy ) self. button_quit.grid (row=20, column =10) def say_hello ( self ): print (" hello there, everyone!") app = Hello () app. mainloop () 27 / 40

Widget Properties and Placement Postioning Widgets grid() grid() places the widget in tk s geometry manager. Use grid in the top level widget (e.g. Frame or other container) to initialise the grid. Call grid for every widget with the keywords row and column. Cover more rows and/or columns: columnspan, rowspan Use sticky argument to align (e.g. tk.e, tk.nw) or stretch (e.g. tk.n+tk.s) the widget. 28 / 40

Widget Properties and Placement Exercise Conversion tool Write a program that converts meter to parsec and vice versa: Hint: You will need tk.entry and tk.stringvar. 29 / 40

Putting it together More complexity Outline Motivation To GUI or not to GUI Getting started Terminology Hello World Overview Widget Properties and Placement Putting it together More complexity 30 / 40

Putting it together More complexity Control Variables Control variables can be tk.stringvar tk.intvar tk.doublevar Get and set values with get() and set(). Use them to store the Data independently from the visual representation. I.e. Keep model and view separated. 31 / 40

Putting it together More complexity Matplotlib in tkinter For simple plot, tk.canvas may suffice For displaying Matplotlib plots in tkinter programs, use FigureCanvasTkAgg: import matplotlib matplotlib. use ( TkAgg ) from matplotlib. backends. backend_tkagg import FigureCanvasTkAgg Use FigureCanvasTkAgg as Canvas 32 / 40

Putting it together More complexity Matplotlib in tkinter 33 / 40

Putting it together More complexity Matplotlib in tkinter from numpy import * import matplotlib matplotlib. use ( TkAgg ) from matplotlib. backends. backend_tkagg import FigureCanvasTkAgg from matplotlib. figure import Figure class Fitting (tk. Frame ): def init (self, master =tk.tk ()): tk. Frame. init (self, master ) self. master = master self. grid () self. createvariables () self. createwidgets ()... def createvariables ( self ): self. e_input_text = tk. StringVar () self. e_input_text.set ( sin (x) ) 34 / 40

Putting it together More complexity Matplotlib in tkinter def createwidgets ( self ): # matplotlib FigureCanvasTkAgg f = Figure ( figsize=(5, 4), dpi =100) self.sp = f. add_subplot (111) self.x = linspace ( -10,10,200) x = self.x y = eval (self. e_input_text.get ()) self.sp. plot (x, y) self. canvas = FigureCanvasTkAgg (f, master = self ) self. canvas.show () self. canvas. get_tk_widget (). grid (row = 2, column=10, columnspan =20, sticky =tk. NSEW ) # entry self. e_input = tk. Entry (self, textvariable=self. e_input_text ) self. e_input.grid (row =5, column =10, columnspan =20, sticky =tk.ew) # buttons self. b_plot = tk. Button (self, text="plot ", command=self. on_plot ) self. b_plot.grid (row =10, column =10, sticky =tk.ew) self. b_quit = tk. Button (self, text="quit ", command=self. master. destroy ) self. b_quit.grid (row =10, column =20, sticky =tk.ew) def on_plot ( self ): x = self.x y = eval (self. e_input_text.get ()) self.sp. plot (x, y) self. canvas.show () app = Fitting () app. mainloop () 35 / 40

Putting it together More complexity Loading files conveniently Let the user choose the file to load using the tkfiledialog if sys. version_info >= (3,): from tkinter. filedialog import askopenfilename else : from tkfiledialog import askopenfilename filename_with_path = askopenfilename () data = np. loadtxt ( filename_with_path, unpack=true ) To let the user choose a filename for saving data (i.e. creating a file) use asksaveasfilename() 36 / 40

Putting it together More complexity Menus in tkinter Create menus with tk.menu Add items to the menu using add command() Add a submenu using add cascade(). Add a separator using add separator(). Finally set the menu as the programs main menu with master.config(menu=themenu) 37 / 40

Putting it together More complexity Menus in tkinter class JustMenu (tk. Frame ): def init (self, master =tk.tk ()): tk. Frame. init (self, master ) self. master = master self. config ( width =300) self. config ( height =200) self. grid () self. createmenu () def createmenu ( self ): menubar = tk.menu (self. master ) filemenu = tk. Menu ( menubar, tearoff =1) filemenu. add_command ( label="new ", command=self. donothing, underline =0) filemenu. add_command ( label ="Open ", command = self. donothing ) filemenu. add_command ( label ="Save ", command = self. donothing ) filemenu. add_command ( label ="Save as... ", command = self. donothing ) filemenu. add_command ( label =" Close ", command = self. donothing ) filemenu. add_separator () filemenu. add_command ( label ="Exit ", command = self. master.quit ) menubar. add_cascade ( label="file ", menu=filemenu, underline =0) self. master. config (menu = menubar ) def donothing ( self ): print ( do nothing ) app = JustMenu () app. mainloop () 38 / 40

Putting it together More complexity Exercise Download the data file: www.mpia.da/ brinkmann/pythoncrash/fitting.dat Write a program that implements a button to load and display the data in a plot. Extend this program to feature three slide controls (tk.scale), which control a, b and c in this function def model (x,a,b,c): return a*x **2 + b*x + c Plot the parabola in the same axes as the data. It should update whenever the slide controls are changed. 39 / 40

Appendix For Further Reading For Further Reading I J. E. Grayson Python and Tkinter Programming. Manning Publications, 2000. B. Chaudhary Tkinter GUI Application Development. Packt Publishing, 2013. 40 / 40