Tkinter Part II: Buttons, Lambda & Dynamic Content

Similar documents
Mid Unit Review. Of the four learning outcomes for this unit, we have covered the first two. 1.1 LO1 2.1 LO2 LO2

Level 3 Computing Year 2 Lecturer: Phil Smith

Programming Training. This Week: Tkinter for GUI Interfaces. Some examples

Teaching London Computing

Tkinter: Input and Output Bindings. Marquette University

This text is used together with Mark Pilgrims book Dive Into Python 3 for the Arthead course Python Fundamentals.

Application Note: Creating a Python Graphical User Interface. Matthew Roach March 31 st, 2014

ENGR/CS 101 CS Session Lecture 15

CS2021 Week #6. Tkinter structure. GUI Programming using Tkinter

Selected GUI elements:

CIS192 Python Programming

A GUI for DFT and Orthogonal DWT in Tkinter

CAS London CPD Day February 16

CS 112 Project Assignment: Visual Password

CS 112: Intro to Comp Prog

About 1. Chapter 1: Getting started with pyqt5 2. Remarks 2. Examples 2. Installation or Setup 2. Hello World Example 6. Adding an application icon 8

Python for Scientific Computations and Control

# arrange Label in parent widget

Tcl/Tk lecture. What is the Wish Interpreter? CIS 410/510 User Interface Programming

Lecture 3 - Overview. More about functions Operators Very briefly about naming conventions Graphical user interfaces (GUIs)

Graphical User Interfaces

Fundamentals of Programming. Functions Redux. Event-based Programming. File and Web IO. November 4th, 2014

Easy Graphical User Interfaces

Thinking in Tkinter. Thinking in Tkinter. by Stephen Ferg ferg.org) revised:

PTN-202: Advanced Python Programming Course Description. Course Outline

Part 3. Useful Python

CIS192 Python Programming

Lecture 3 - Overview. Object-oriented programming and classes Operators Very briefly about naming conventions Graphical user interfaces (GUIs)

Graphical user interface software

Noughts and Crosses. Step 1: Drawing the grid. Introduction

""" helloio.py illustrate form IO (still procedural) works, but bad design: no main function """ from Tkinter import *

CS 2316 Exam 3 Fall 2011

CS 2316 Exam 3 Spring 2013

When to use the Grid Manager

Python GUIs. $ conda install pyqt

Outline. general information policies for the final exam

CS 2316 Exam 3 Summer 2014

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Chapter 9 GUI Programming Using Tkinter. Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

Graphical User Interfaces

Introducing Motif. Motif User s Guide 1

User Interfaces. getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy

Graphics: Legacy Library

Python. Easiest to Highest. PART 1: Python Element

tkstuff Documentation

[CHAPTER] 1 INTRODUCTION 1

Sliders. If we start this script, we get a window with a vertical and a horizontal slider:

Asynchronous Programming

CSc 110, Autumn 2016 Lecture 7: Graphics. Adapted from slides by Marty Stepp and Stuart Reges

CSE : Python Programming

Getting Started p. 1 Obtaining Tcl/Tk p. 1 Interactive Execution p. 1 Direct Execution p. 4 Reading this Book p. 6 Requirements for Networking

Your (printed!) Name: CS 1803 Exam 2. Grading TA / Section: Monday, Oct 25th, 2010

Graphical User Interfaces

Question Possible Points Earned Points Graded By GUI 22 SQL 24 XML 20 Multiple Choice 14 Total Points 80

Learning outcomes. COMPSCI 101 Principles of Programming. Drawing 2D shapes using Characters. Printing a Row of characters

user's guide Author : Matías Guijarro Date : 18/06/04

Introduction to Programming Using Python Lecture 6. Dr. Zhang COSC 1437 Spring, 2018 March 01, 2018

Programming in Python

Python GUI Programming (Tkinter)

The Racket Graphical Interface Toolkit

Graphical User Interfaces with Perl/Tk. Event Driven Programming. Structure of an Event-Driven Program. An introduction

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang

Graphical User Interface (GUI)

Using tcl to Replay Xt Applications

Please attribute to : Paul Sutton : :

Windows and Events. created originally by Brian Bailey

Graphical User Interface (GUI)

CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random. Adapted from slides by Marty Stepp and Stuart Reges

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

CS 2316 Homework 9b GT Thrift Shop Due: Wednesday, April 20 th, before 11:55 PM Out of 100 points. Premise

AP CS Unit 11: Graphics and Events

Embedded GUI: Widgets Within Editors

Microsoft Office Excel 2007: Basic. Course Overview. Course Length: 1 Day. Course Overview

Python Scripting for Computational Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Threading the Code. Self-Review Questions. Self-review 11.1 What is a thread and what is a process? What is the difference between the two?

CS123. Programming Your Personal Robot. Part 2: Event Driven Behavior

CS 170 Java Programming 1. Week 9: Learning about Loops

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

2. (True/False) All methods in an interface must be declared public.

Python Scripting for Computational Science

FrTime: A Language for Reactive Programs

GUI Output. Adapted from slides by Michelle Strout with some slides from Rick Mercer. CSc 210

Comparing SQLfast code and standard application code. Three examples : data extraction, GUI and file download

Parsing Scheme (+ (* 2 3) 1) * 1

CS 101 Computer Science I Fall Instructor Muller. stddraw API. (DRAFT of 1/15/2013)

Name: Partner: Python Activity 9: Looping Structures: FOR Loops

UI Course HTML: (Html, CSS, JavaScript, JQuery, Bootstrap, AngularJS) Introduction. The World Wide Web (WWW) and history of HTML

Object-Oriented Programming in C# (VS 2015)

Algebra Homework Application Nick Hobbs

Basking in the. by Micah Martin 8th Light, Inc. Copyright 2009 Micah Martin

Object-Oriented Programming in C# (VS 2012)

About this tutorial. The Lianja App Development process

Lay-out formatting Cinema 4d Dialog (R13) For this tutorial I used

MA Petite Application

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Python. Easiest to Highest. PART 1: Python Element

Java Foundations. 9-1 Introduction to JavaFX. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Transcription:

Tkinter Part II: Buttons, Lambda & Dynamic Content July 8, 2015 Brian A. Malloy Slide 1 of 11

1. We further investigate Labels and Buttons and hook Python actions to these widgets. We present lambda functions, useful in GUIs We show how to bind keystrokes and mouse events to Python code In Part III we examine Frames to help with layout management, and show some additional Tkinter widgets. Slide 2 of 11

2. 1 import Tkinter as tk 2 counter = 0 3 def counterlabel(label): 4 def count(): 5 global counter 6 counter += 1 7 label.config(text=str(counter)) 8 label.after(1000, count) 9 count() 10 11 root = tk.tk() 12 root.title("second Timer") 13 label = tk.label(root, fg="green") 14 label.pack() 15 counterlabel(label) 16 button = tk.button(root, text= Stop, \ width=25, command=root.destroy) 17 button.pack() 18 root.mainloop() Slide 3 of 11

2.1. Explained Line #12 sets the title of the window Line #13 instantiates a Label Line #16 instantiates a Button Line #15 calls function counterlabel which: 1. calls count on line #9 2. count updates global counter (line #6) 3. count schedules the next call to count using a method in Label, after, which takes 2 parameters: (1) the delay, and (2) the function to call after the delay. (line #8) Slide 4 of 11

2.2. Connecting Widgets with Code On line #7 the text in the Label widget is updated every 1000 milliseconds, which is every second. On line #16, a Button is instantiated, including the command to be executed when the button is pushed. That command is root.destroy destroy terminates the mainloop and deletes all widgets As an exercise, convert the Timer program so that it uses a class. Slide 5 of 11

3. 1 import Tkinter as tk 2 class App: 3 def init (self, master): 4 self.button = tk.button(master, 5 text="quit", fg="red", 6 command=master.quit) 7 self.button.pack(side=tk.left) 8 self.slogan = tk.button(master, text="hello", 9 command=self.writeslogan) 10 self.slogan.pack(side=tk.left) 11 def writeslogan(self): 12 print "Tkinter is easy to use!" 13 14 root = tk.tk() 15 app = App(root) 16 root.mainloop() Slide 6 of 11

3.1. Buttton Class Explained In this example we package the GUI in a class: App We pass root to the constructor Line #6 connects the code master.quit to the button, which stops the tk interpreter Line #9 connects the code in writeslogan to the slogan button. Slide 7 of 11

4. The lambda operator is a way to create small, anonymous functions. They are dixie cup functions that you only use once where they are created. Syntax: lambda argument list: expression Example: >>> f = lambda x, y : x + y >>> f(1,1) 2 Slide 8 of 11

5. 1 import Tkinter as tk 2 GEOMETRY = 300x300+200+60 3 def callback(event): 4 print event.x, event.y 5 6 root = tk.tk() 7 root.geometry(geometry) 8 root.title("my First Button") 9 root.bind( <Escape>, (lambda event: root.quit())) 10 myfont = ( symbol, 12) 11 12 label = tk.label(root, font = myfont, text="hello") 13 label.config(relief=tk.raised) 14 label.config(width=25, height=5, bg="#00ff00", bd=5) 15 label.pack(side=tk.top, padx=5, pady=5) 16 label.bind("<button>", callback) 17 root.mainloop() Slide 9 of 11

5.1. What Does The figure shows the widgets on the right, and the output of the program on the left. The output representes the (x, y) coordinates of the mouse when the left mouse button is clicked. The program terminates when the escape key is pressed in the green Label. Slide 10 of 11

5.2. Key Bind Explained Lines #2 & #7 set size & position of the tk window; size is 300x300, position is (200, 60) Line #9 binds the escape key to a lambda function that terminates the tk program. Line #10 sets the font Line #13 sets the relief of the Label widget. Line #14 uses hex to set the background color to green, and sets the border (bd) to be 5 pixels. Line #16 binds the left mouse button to the function on line #3, callback; we pass the mouse event to the callback function so it can print the mouse coordinates at the time of the left button click. Slide 11 of 11