You are previewing Python GUI Programming Cookbook.
O'Reilly logo
Python GUI Programming Cookbook

Book Description

Over 80 object-oriented recipes to help you create mind-blowing GUIs in Python

About This Book

  • Use object-oriented programming to develop amazing GUIs in Python

  • Create a working GUI project as a central resource for developing your Python GUIs

  • Packed with easy-to-follow recipes to help you develop code using the latest released version of Python

  • Who This Book Is For

    If you are a Python programmer with intermediate level knowledge of GUI programming and want to learn how to create beautiful, effective, and responsive GUIs using the freely available Python GUI frameworks, this book is for you.

    What You Will Learn

  • Create amazing GUIs with Python’s built-in Tkinter module

  • Customize the GUIs by using layout managers to arrange the GUI widgets

  • Advance to an object-oriented programming style using Python

  • Develop beautiful charts using the free Matplotlib Python module

  • Use threading in a networked environment to make the GUIs responsive

  • Discover ways to connect the GUIs to a database

  • Understand how unit tests can be created and internationalize the GUI

  • Extend the GUIs with free Python frameworks using best practices

  • In Detail

    Python is a multi-domain, interpreted programming language. It is a widely used general-purpose, high-level programming language. It is often used as a scripting language because of its forgiving syntax and compatibility with a wide variety of different eco-systems. Its flexible syntax enables developers to write short scripts while at the same time, they can use object-oriented concepts to develop very large projects.

    Python GUI Programming Cookbook follows a task-based approach to help you create beautiful and very effective GUIs with the least amount of code necessary. This book uses the simplest programming style, using the fewest lines of code to create a GUI in Python, and then advances to using object-oriented programming in later chapters. If you are new to object-oriented programming (OOP), this book will teach you how to take advantage of the OOP coding style in the context of creating GUIs written in Python.

    Throughout the book, you will develop an entire GUI application, building recipe upon recipe, connecting the GUI to a database. In the later chapters, you will explore additional Python GUI frameworks, using best practices. You will also learn how to use threading to ensure your GUI doesn’t go unresponsive.

    By the end of the book, you will be an expert in Python GUI programming to develop a common set of GUI applications.

    Style and approach

    Every recipe in this programming cookbook solves a problem you might encounter in your programming career. At the same time, most of the recipes build on each other to create an entire, real-life GUI application.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Python GUI Programming Cookbook
      1. Table of Contents
      2. Python GUI Programming Cookbook
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Creating the GUI Form and Adding Widgets
        1. Introduction
        2. Creating our first Python GUI
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Preventing the GUI from being resized
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Adding a label to the GUI form
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Creating buttons and changing their text property
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Text box widgets
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Setting the focus to a widget and disabling widgets
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Combo box widgets
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Creating a check button with different initial states
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Using radio button widgets
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Using scrolled text widgets
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Adding several widgets in a loop
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      9. 2. Layout Management
        1. Introduction
        2. Arranging several labels within a label frame widget
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Using padding to add space around widgets
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. How widgets dynamically expand the GUI
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Aligning the GUI widgets by embedding frames within frames
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Creating menu bars
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        7. Creating tabbed widgets
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Using the grid layout manager
          1. Getting ready…
          2. How to do it...
          3. How it works...
      10. 3. Look and Feel Customization
        1. Introduction
        2. Creating message boxes – information, warning, and error
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. How to create independent message boxes
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. How to create the title of a tkinter window form
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Changing the icon of the main root window
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Using a spin box control
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Relief, sunken, and raised appearance of widgets
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Creating tooltips using Python
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. How to use the canvas widget
          1. Getting ready
          2. How to do it...
          3. How it works...
      11. 4. Data and Classes
        1. Introduction
        2. How to use StringVar()
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. How to get data from a widget
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Using module-level global variables
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. How coding in classes can improve the GUI
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Writing callback functions
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Creating reusable GUI components
          1. Getting ready
          2. How to do it...
          3. How it works...
      12. 5. Matplotlib Charts
        1. Introduction
        2. Creating beautiful charts using Matplotlib
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Matplotlib – downloading modules using pip
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Matplotlib – downloading modules with whl extensions
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Creating our first chart
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Placing labels on charts
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. How to give the chart a legend
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Scaling charts
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Adjusting the scale of charts dynamically
          1. Getting ready
          2. How to do it...
          3. How it works...
      13. 6. Threads and Networking
        1. Introduction
        2. How to create multiple threads
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Starting a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Stopping a thread
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. How to use queues
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Passing queues among different modules
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Using dialog widgets to copy files to your network
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Using TCP/IP to communicate via networks
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Using URLOpen to read data from websites
          1. Getting ready
          2. How to do it...
          3. How it works...
      14. 7. Storing Data in Our MySQL Database via Our GUI
        1. Introduction
        2. Connecting to a MySQL database from Python
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Configuring the MySQL connection
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Designing the Python GUI database
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Using the SQL INSERT command
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Using the SQL UPDATE command
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Using the SQL DELETE command
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Storing and retrieving data from our MySQL database
          1. Getting ready
          2. How to do it…
          3. How it works…
      15. 8. Internationalization and Testing
        1. Introduction
        2. Displaying widget text in different languages
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Changing the entire GUI language all at once
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Localizing the GUI
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Preparing the GUI for internationalization
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. How to design a GUI in an agile fashion
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Do we need to test the GUI code?
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Setting debug watches
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Configuring different debug output levels
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Creating self-testing code using Python's __main__ section
          1. Getting ready
          2. How to do it...
          3. How it works...
        11. Creating robust GUIs using unit tests
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. How to write unit tests using the Eclipse PyDev IDE
          1. Getting ready
          2. How to do it...
          3. How it works...
      16. 9. Extending Our GUI with the wxPython Library
        1. Introduction
        2. How to install the wxPython library
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. How to create our GUI in wxPython
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Quickly adding controls using wxPython
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Trying to embed a main wxPython app in a main tkinter app
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Trying to embed our tkinter GUI code into wxPython
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. How to use Python to control two different GUI frameworks
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. How to communicate between the two connected GUIs
          1. Getting ready
          2. How to do it...
          3. How it works...
      17. 10. Creating Amazing 3D GUIs with PyOpenGL and PyGLet
        1. Introduction
        2. PyOpenGL transforms our GUI
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Our GUI in 3D!
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Using bitmaps to make our GUI pretty
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. PyGLet transforms our GUI more easily than PyOpenGL
          1. How to do it...
          2. How it works...
        6. Our GUI in amazing colors
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Creating a slideshow using tkinter
          1. Getting ready
          2. How to do it...
          3. How it works...
      18. 11. Best Practices
        1. Introduction
        2. Avoiding spaghetti code
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Using __init__ to connect modules
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Mixing fall-down and OOP coding
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Using a code naming convention
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. When not to use OOP
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. How to use design patterns successfully
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Avoiding complexity
          1. Getting ready
          2. How to do it...
          3. How it works...
      19. Index