Chapter 12 GUI Programming STARTING OUT WITH Python

  • Slides: 34
Download presentation
Chapter 12 GUI Programming STARTING OUT WITH Python First Edition by Tony Gaddis Copyright

Chapter 12 GUI Programming STARTING OUT WITH Python First Edition by Tony Gaddis Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

12. 1 Graphical User Interfaces Concept: A graphical user interface allows the user to

12. 1 Graphical User Interfaces Concept: A graphical user interface allows the user to interact with the operating system and other programs using graphical elements such as icons, buttons, and dialog boxes. 1 -2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -2

12. 1 Graphical User Interfaces A computer’s user interface is the part of the

12. 1 Graphical User Interfaces A computer’s user interface is the part of the computer that the user interacts with User interface consists of • Hardware devices • Commands from the user the operating system accepts Command line interface displays a prompt and the user types a command which is then executed Figure 12 -1 A command line interface 1 -3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -3

12. 1 Graphical User Interfaces A graphical user interface (GUI) allows the user to

12. 1 Graphical User Interfaces A graphical user interface (GUI) allows the user to interact with the operating system and other programs through graphical elements (icons, buttons, slider bars, etc. ) on the screen. • GUIs popularized the use of the mouse. • GUIs allow the user to point at graphical elements and click the mouse button to activate them. 1 -4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -4

12. 1 Graphical User Interfaces Interaction with a GUI is done through dialog boxes

12. 1 Graphical User Interfaces Interaction with a GUI is done through dialog boxes – small windows that display information and allow the user to perform actions Figure 12 -2 A dialog box 1 -5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -5

12. 1 Graphical User Interfaces GUI Programs Are Event-Driven User determines the order in

12. 1 Graphical User Interfaces GUI Programs Are Event-Driven User determines the order in which things happen GUI programs respond to the actions of the user, thus they are event driven. Figure 12 -3 A GUI program 1 -6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -6

12. 2 Using the Tkinter Module Concept: In Python you can use the Tkinter

12. 2 Using the Tkinter Module Concept: In Python you can use the Tkinter module to create simple GUI programs. 1 -7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -7

12. 2 Using the Tkinter Module • Python does not have GUI programming features

12. 2 Using the Tkinter Module • Python does not have GUI programming features built into the language. • The Tkinter module allows the creation of simple GUI programs. • Tkinter does not always run reliably under IDLE • Use IDLE’s editor to write GUI programs, but for best results run the program from the OS command line. 1 -8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -8

12. 2 Using the Tkinter Module Program 12 -1 (empty_window 1. py) Figure 12

12. 2 Using the Tkinter Module Program 12 -1 (empty_window 1. py) Figure 12 -4 Window displayed by Program 12 -1 1 -9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -9

12. 3 Display Text with Label Widgets Concept: You use the Label widget to

12. 3 Display Text with Label Widgets Concept: You use the Label widget to display text in a window. 1 -10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -10

12. 3 Display Text with Label Widgets Program 12 -3 (hello_world. py) Figure 12

12. 3 Display Text with Label Widgets Program 12 -3 (hello_world. py) Figure 12 -5 Window displayed by Program 12 -3 1 -11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -11

12. 3 Display Text with Label Widgets Program 12 -5 (hello_world 3. py) Figure

12. 3 Display Text with Label Widgets Program 12 -5 (hello_world 3. py) Figure 12 -7 Window displayed by Program 12 -5 1 -12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -12

12. 4 Organizing Widgets with Frames Concept: A Frame is a container that can

12. 4 Organizing Widgets with Frames Concept: A Frame is a container that can hold other widgets. You can use Frames to organize the widgets in a window. 1 -13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -13

12. 4 Organizing Widgets with Frames • A Frame is a container. • It

12. 4 Organizing Widgets with Frames • A Frame is a container. • It is a widget that can hold other widgets. • Frames are useful for organizing and arranging groups of widgets in a window. 1 -14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -14

12. 4 Organizing Widgets with Frames Program 12 -6 (frame_demo. py) Figure 12 -8

12. 4 Organizing Widgets with Frames Program 12 -6 (frame_demo. py) Figure 12 -8 Window displayed by Program 12 -6 1 -15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -15

12. 4 Organizing Widgets with Frames Figure 12 -8 Window displayed by Program 12

12. 4 Organizing Widgets with Frames Figure 12 -8 Window displayed by Program 12 -6 Figure 12 -9 Arrangement of widgets 1 -16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -16

12. 5 Button Widgets and Info Dialog Boxes Concept: Use the Button widget to

12. 5 Button Widgets and Info Dialog Boxes Concept: Use the Button widget to create a standard button in a window. When the user clicks a button, a specified function or method is called. An info dialog box is a simple window that displays a message to the user and has an OK button that dismisses the dialog box. You can use the tk. Message. Box module’s showinfo function to display an info dialog box. 1 -17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -17

12. 5 Button Widgets and Info Dialog Boxes • A Button is a widget

12. 5 Button Widgets and Info Dialog Boxes • A Button is a widget that the user can click to cause an action to take place • A callback function (event handler) is a function or method that executes when the user clicks the button 1 -18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -18

12. 5 Button Widgets and Info Dialog Boxes Info Dialog box tk. Message. Box.

12. 5 Button Widgets and Info Dialog Boxes Info Dialog box tk. Message. Box. showinfo(title, message) Program 12 -7 (button_demo. py) 1 -19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -19

12. 5 Button Widgets and Info Dialog Boxes Creating Quit Button Program 12 -8

12. 5 Button Widgets and Info Dialog Boxes Creating Quit Button Program 12 -8 (quit_button. py) 1 -20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -20

12. 6 Getting Input with the Entry Widget Concept: An Entry widget is a

12. 6 Getting Input with the Entry Widget Concept: An Entry widget is a rectangular area into which the user can type input. Use the Entry widget’s get method to retrieve the data that has been typed into the widget. 1 -21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -21

12. 6 Getting Input with the Entry Widget • An Entry widget is a

12. 6 Getting Input with the Entry Widget • An Entry widget is a rectangular area into which the user can type text. • These widgets are used to gather input in a GUI program. • The get method returns a string. 1 -22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -22

12. 6 Getting Input with the Entry Widget Program 12 -9 (kilo_converter. py) 1

12. 6 Getting Input with the Entry Widget Program 12 -9 (kilo_converter. py) 1 -23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -23

12. 6 Getting Input with the Entry Widget Program 12 -9 (cont. ) (kilo_converter.

12. 6 Getting Input with the Entry Widget Program 12 -9 (cont. ) (kilo_converter. py) 1 -24 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -24

12. 6 Getting Input with the Entry Widget Figure 12 -15 The info dialog

12. 6 Getting Input with the Entry Widget Figure 12 -15 The info dialog box 1 -25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -25

12. 7 Using Labels as Output Fields Concept: When a String. Var object is

12. 7 Using Labels as Output Fields Concept: When a String. Var object is associated with a Label widget, the Label widget displays any data that is stored in the String. Var object. 1 -26 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -26

12. 7 Using Labels as Output Fields Program 12 -10 (kilo_converter 2. py) Figure

12. 7 Using Labels as Output Fields Program 12 -10 (kilo_converter 2. py) Figure 12 -18 Layout of the kilo_converter 2 program’s main window 1 -27 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -27

12. 8 Radio Buttons and Check BUttons Concept: Radio buttons normally appear in groups

12. 8 Radio Buttons and Check BUttons Concept: Radio buttons normally appear in groups of two or more and allow the user to select one of several possible options. Check buttons, which may appear alone or in groups, allow the user to make yes/no or on/off selections. 1 -28 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -28

12. 8 Radio Buttons and Check Buttons Radio buttons are used when the user

12. 8 Radio Buttons and Check Buttons Radio buttons are used when the user needs to select one choice from several options Figure 12 -22 A group of radio buttons 1 -29 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -29

12. 8 Radio Buttons and Check Buttons Program 12 -12 (radiobutton_demo. py) 1 -30

12. 8 Radio Buttons and Check Buttons Program 12 -12 (radiobutton_demo. py) 1 -30 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -30

12. 8 Radio Buttons and Check Buttons Check buttons are used when the user

12. 8 Radio Buttons and Check Buttons Check buttons are used when the user can make multiple choices. Figure 12 -24 A group of check buttons 1 -31 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -31

12. 8 Radio Buttons and Check Buttons Program 12 -13 (checkbutton_demo. py) 1 -32

12. 8 Radio Buttons and Check Buttons Program 12 -13 (checkbutton_demo. py) 1 -32 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -32

12. 8 Radio Buttons and Check Buttons Program 12 -13 (cont. ) (checkbutton_demo. py)

12. 8 Radio Buttons and Check Buttons Program 12 -13 (cont. ) (checkbutton_demo. py) 1 -33 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 -33

Chapter 12 GUI Programming QUESTIONS ? Copyright © 2009 Pearson Education, Inc. Publishing as

Chapter 12 GUI Programming QUESTIONS ? Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley