CSC 1010 Lecture 8 What do we know so far? Class lecture, lab, - - PDF document

csc 1010 lecture 8
SMART_READER_LITE
LIVE PREVIEW

CSC 1010 Lecture 8 What do we know so far? Class lecture, lab, - - PDF document

CSC 1010 Lecture 8 What do we know so far? Class lecture, lab, Rephactor, Quick Checks, R&R, easter eggs Solve problems, computers useful, user vs. programmer Sequence of instructions, algorithm is step by step Python


slide-1
SLIDE 1

CSC 1010 Lecture 8 1

CSC 1010 Programming for All

Lecture 9 Graphical User Interfaces

What do we know so far?

  • Class – lecture, lab, Rephactor, Quick Checks, R&R, easter eggs
  • Solve problems, computers useful, user vs. programmer
  • Sequence of instructions, algorithm is step‐by‐step
  • Python is 3rd most popular language, core principles, always more than one way
  • Syntax, runtime, & logic errors, testing & debugging, hardware vs. software
  • Control flow – step‐by‐step, function call, conditional, loop
  • IDLE shell, editor, install Python, Hello World
  • Intrepreter, compiler, Python Standard Library
  • Variables, assignment, numeric expr., precedence
  • Print function, Strings, concatenation, indexes, in, *
  • Interactive programs, if, if‐else, if‐elif‐else, int, float
  • Boolean expressions: ==, !‐, <, <=, >, >=, not, and, or
  • Input function, comparing strings, programming conventions
  • Variable & function names lowercase, CONSTANTS, indent
  • while, for, range, augmented assignments, palindromes
  • Turtle Graphics, forward, left, right, pensize, pencolor, dot, circle
  • goto, penup, pendown, fillcolor, begin_fill, end_fill, speed
  • Calling & defining functions, import, parameters vs. arguments, return
  • Positional args, default args, variable args, keyword args, local variables
  • String methods, replace, method vs. function, built‐in & external functions
  • Using loops and functions to create graphics, random numbers, design process
  • Lists: indexing, iterating, concatenating, containment, repetition
  • Membership: in & not in, Identity: is & is not, type checking
  • List algorithms, list methods, slicing strings and lists, ex: finding a minimum
  • Dictionaries, change, add, lookup, remove, key exists, iterate, other methods
  • Reading and writing text files, file choosers, dictionary for counting occurrences

2

Introduction to Tkinter

A Graphical User Interface or GUI (or "gooey") is a visual, interactive application with a window, buttons, text fields, check boxes and more. Tkinter ("tee‐kay inter") is a built‐in Python framework for creating GUIs. Tk Themed Widgets is a more recent addition to the framework. There are four steps to creating a GUI application:

  • 1. Import Tkinter
  • 2. Create a main window
  • 3. Add widgets
  • 4. Add event handlers

3

Import Tkinter

Use the import statement to import Tkinter and Tk Themed Widgets:

4

i m por t t ki nt er i m por t t ki nt er . t t k

Tkinter has a number of foundational widgets, such as buttons, labels, text areas, etc. Tk Themed Widgets consist of many of the same widgets along with some more advanced ones including tabbed panes, progress bars, and label frames.

Create a Window

Create a root window using the Tk constructor. Optionally, set a title and initial size.

5

r oot = Tk( ) r oot . t i t l e( ' M y Pol i t e Appl i cat i on' ) r oot . geom et r y( ' 300x100' )

Add Widgets

Add widgets to the root window and "pack" them, organizing and positioning them in the window.

6

l abel = t ki nt er . Label ( r oot , t ext =' Hel l o, W

  • r l d! ' )

l abel . pack( ) but t on = t ki nt er . But t on( r oot , t ext =' Say Hel l o' ) but t on. pack( )

1 2 3 4 5 6

slide-2
SLIDE 2

CSC 1010 Lecture 8 2

Add Event Handlers

Most widgets can take a command argument, which is the name of a function to run with the widget is clicked.

7

def say_hel l o( ) : l abel [ ' t ext ' ] = ' Hel l o ' + l abel [ ' t ext ' ] but t on = t ki nt er . But t on( r oot , t ext =' Say Hel l o' , com m and=say_hel l o)

Here's the GUI after clicking the button twice:

Buttons

A button is a GUI widget that allows the user to initiate an action by pushing a graphical button using the mouse.

8

but t on = But t on( r oot , t ext =' Com put e Tax' )

To specify an event handler for the button press, set the command argument to the name of a function:

def pr essed( ) : pr i nt ( ' Cl i ck! ' ) but t on = But t on( r oot , t ext =' Com put e Tax' , com m and=pr essed)

Button Appearance

A button can be modified with a text font, image, and background color, among many options.

9

b1 = But t on( f r am e, t ext =' Updat e M y O r der ' , f ont =( ' Hel vet i ca' , 16, ' bol d' ) , backgr ound=' gol d' , act i vebackgr ound=' or ange' , padx=20, pady=10) phot o = Phot oI m age( f i l e=' i ncom e_pr oj ect i on. png' ) b2 = But t on( f r am e, t ext =' I ncom e Pr oj ect i on' , i m age=phot o, com pound=LEFT, r el i ef =G RO O VE, padx=5, pady=5) b3 = But t on( f r am e, i m age=phot o, bor der wi dt h=10, r el i ef =RI DG E)

The modified buttons look like this…

Button Appearance

The first button has a larger, bold font and a gold background. The second button has an image and text. The third button has just an image with no text.

10

GUI Widgets

A widget is visual element of a Graphical User Interface. It displays information or enables user interaction. A widget is sometimes called a control.

11

Button Canvas

Checkbutton Combobox

Entry Frame

Label LabelFrame

GUI Widgets

12

Menu MenuButton Message messagebox OptionMenu PanedWindow Listbox Notebook

7 8 9 10 11 12

slide-3
SLIDE 3

CSC 1010 Lecture 8 3

GUI Widgets

13

Scale Scrollbar Separator Spinbox Toplevel Treeview Progressbar Radiobutton Text simpledialog Tk

Big Widget Demo

14

Color Choosers

A color chooser is a GUI element that opens a dialog that allows the user to select a color.

15

f r om t ki nt er . col or chooser i m por t askcol or ( r gb, hx) = askcol or ( t i t l e=' Pi ck a f i l l col or ' )

askcolor returns two values for the color picked: A tuple of red, green, blue values and a hex string.

Installing Packages

In addition to the built‐in functions and modules in Python, there are many external packages. To be used, they first need to be installed using pip3 in a Terminal or Command Prompt.

16

> pi p3 i nst al l ant i gr avi t y

Pip stands for "Preferred Installer Package." Once the above package is installed, it can be imported in your program. Windows

> sudo pi p3 i nst al l ant i gr avi t y

Mac

Installing Packages

17

Once installed, a package can be imported.

i m por t ant i gr avi t y

The antigravity package is a silly one. When imported it opens a comic web site. That's all it does.

Installing PyGame

PyGame is a popular package for creating computer games and working with multimedia. It is easy to install.

18

> pi p3 i nst al l pygam e

Windows

> sudo pi p3 i nst al l pygam e

Mac

13 14 15 16 17 18

slide-4
SLIDE 4

CSC 1010 Lecture 8 4

PyGame Example: Playing Sound

This example plays an audio file when the button is clicked.

19

f r om t ki nt er i m por t * i m por t pygam e # Handl es t he but t on bei ng pr essed by pl ayi ng t he sound f i l e. def pl ay( ) : pygam

  • e. m

i xer . m usi c. l oad( ' m acar t hur . wav' ) pygam

  • e. m

i xer . m usi c. pl ay( ) # Cr eat es t he r oot wi ndow r oot = Tk( ) r oot . t i t l e( ' Pl ay Sound Dem

  • ' )

r oot . conf i gur e( wi dt h=280, hei ght =100, backgr ound=' pur pl e' ) # I ni t i al i ze pygam e bef or e you use i t pygam

  • e. i ni t ( )

# Adds t he But t on t o t he r oot wi ndow but t on = But t on( r oot , t ext =' Pl ay' , f ont =( ' Hel vet i ca' , 18) , com m and=pl ay) but t on. pl ace( r el x=0. 5, r el y=0. 5, anchor =CENTER)

PyGame Example: Playing Sound

This example plays an audio file when the button is clicked.

20

19 20