csc 1010 lecture 8
play

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


  1. 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 is 3 rd most popular language, core principles, always more than one way CSC 1010 Programming for All • • 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 • Lecture 9 • 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 Graphical User Interfaces • 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 1 2 Introduction to Tkinter Import Tkinter A Graphical User Interface or GUI (or "gooey") is a visual, Use the import statement to import Tkinter and Tk Themed interactive application with a window, buttons, text fields, Widgets: check boxes and more. i m por t t ki nt er i m por t t ki nt er . t t k Tkinter ("tee ‐ kay inter") is a built ‐ in Python framework for creating GUIs. Tk Themed Widgets is a more recent addition to Tkinter has a number of foundational widgets, such as the framework. buttons, labels, text areas, etc. There are four steps to creating a GUI application: Tk Themed Widgets consist of many of the same widgets 1. Import Tkinter along with some more advanced ones including tabbed panes, progress bars, and label frames. 2. Create a main window 3. Add widgets 4. Add event handlers 3 4 3 4 Create a Window Add Widgets Create a root window using the Tk constructor. Optionally, set Add widgets to the root window and "pack" them, organizing a title and initial size. and positioning them in the window. l abel = t ki nt er . Label ( r oot , t ext =' Hel l o, W or l d! ' ) r oot = Tk( ) l abel . pack( ) 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' ) but t on = t ki nt er . But t on( r oot , t ext =' Say Hel l o' ) but t on. pack( ) 5 6 5 6 1

  2. CSC 1010 Lecture 8 Add Event Handlers Buttons Most widgets can take a command argument, which is the A button is a GUI widget that allows the user to initiate an name of a function to run with the widget is clicked. action by pushing a graphical button using the mouse. def say_hel l o( ) : but t on = But t on( r oot , t ext =' Com put e Tax' ) l abel [ ' t ext ' ] = ' Hel l o ' + l abel [ ' t ext ' ] To specify an event handler for the button press, set the 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) command argument to the name of a function: Here's the GUI after clicking the button twice: 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) 7 8 7 8 Button Appearance Button Appearance A button can be modified with a text font, image, and The first button has a larger, bold font and a gold background. background color, among many options. The second button has an image and text. The third button has just an image with no text. 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… 9 10 9 10 GUI Widgets GUI Widgets A widget is visual element of a Graphical User Interface. It displays information or enables user interaction. A widget is Listbox messagebox sometimes called a control. Button Entry Menu Notebook Frame Canvas MenuButton OptionMenu Label Checkbutton Message PanedWindow LabelFrame Combobox 11 12 11 12 2

  3. CSC 1010 Lecture 8 GUI Widgets Big Widget Demo Progressbar Spinbox Radiobutton Text Scale Tk Scrollbar Toplevel Separator simpledialog Treeview 13 14 13 14 Color Choosers Installing Packages A color chooser is a GUI element that opens a dialog that In addition to the built ‐ in functions and modules in Python, allows the user to select a color. there are many external packages. To be used, they first need to be installed using pip3 in a Terminal or Command Prompt. f r om t ki nt er . col or chooser i m por t askcol or Windows ( r gb, hx) = askcol or ( t i t l e=' Pi ck a f i l l col or ' ) > pi p3 i nst al l ant i gr avi t y Mac askcolor returns two values for the color picked: > sudo pi p3 i nst al l ant i gr avi t y A tuple of red, green, blue Pip stands for "Preferred Installer Package." Once the above values and a hex string. package is installed, it can be imported in your program. 15 16 15 16 Installing Packages Installing PyGame PyGame is a popular package for creating computer games Once installed, a package can be imported. and working with multimedia. It is easy to install. i m por t ant i gr avi t y Windows > pi p3 i nst al l pygam e The antigravity package is Mac a silly one. When imported > sudo pi p3 i nst al l pygam e it opens a comic web site. That's all it does. 17 18 17 18 3

  4. CSC 1010 Lecture 8 PyGame Example: Playing Sound PyGame Example: Playing Sound This example plays an audio file when the button is clicked. This example plays an audio file when the button is clicked. 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 o' ) 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) 19 20 19 20 4

Recommend


More recommend