Tkinter GUI Programming by Example
eBook - ePub

Tkinter GUI Programming by Example

  1. 340 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Tkinter GUI Programming by Example

Book details
Book preview
Table of contents
Citations

About This Book

Leverage the power of Python and its de facto GUI framework to build highly interactive interfacesAbout This Book• The fundamentals of Python and GUI programming with Tkinter.• Create multiple cross-platform projects by integrating a host of third-party libraries and tools.• Build beautiful and highly-interactive user interfaces that target multiple devices.Who This Book Is ForThis book is for beginners to GUI programming who haven't used Tkinter yet and are eager to start building great-looking and user-friendly GUIs. Prior knowledge of Python programming is expected.What You Will Learn• Create a scrollable frame via theCanvas widget• Use the pack geometry manager andFrame widget to control layout• Learn to choose a data structurefor a game• Group Tkinter widgets, such asbuttons, canvases, and labels• Create a highly customizablePython editor• Design and lay out a chat windowIn DetailTkinter is a modular, cross-platform application development toolkit for Python. When developing GUI-rich applications, the most important choices are which programming language(s) and which GUI framework to use. Python and Tkinter prove to be a great combination. This book will get you familiar with Tkinter by having you create fun and interactive projects. These projects have varying degrees of complexity. We'll start with a simple project, where you'll learn the fundamentals of GUI programming and the basics of working with a Tkinter application. After getting the basics right, we'll move on to creating a project of slightly increased complexity, such as a highly customizable Python editor. In the next project, we'll crank up the complexity level to create an instant messaging app. Toward the end, we'll discuss various ways of packaging our applications so that they can be shared and installed on other machines without the user having to learn how to install and run Python programs.Style and approachStep by Step guide with real world examples

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Tkinter GUI Programming by Example by David Love in PDF and/or ePUB format, as well as other popular books in Informatica & Programmazione. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788622578
Edition
1

The Finishing Touches – Sound and Animation

With our game now wearing its flashy graphical outfit, it's time to step it up one more notch on the scale of professionalism with some sound effects. These will help to engage the user by tapping into another of their senses.
To go along with the sound, we should also make some of the movement more realistic so that it better appears that our game is generating these noises.
Luckily, Tkinter's Canvas widget is well equipped to handle displaying animations and comes with some great built-in tools to make animating a breeze.
We can also take advantage of a popular Python game development library called pygame to make playing sounds incredibly simple.
By the end of this chapter, we will have covered the following:
  • Making graphics move on a canvas
  • Keeping control of the GUI while animations are playing
  • Integrating the pygame library with Tkinter and playing sounds
  • Expanding and re-structuring a larger application using Python's module system
With such a vast expansion of our application's features, it makes sense to try and abstract some of it into easily reusable chunks. We have already seen that defining classes can help to achieve this, but there is also one further step we can take – using Python's module system. Let's have a look into how this works before we begin refactoring our game.

Python's module system

Python's module system is something we have been using throughout the book. It is what lies behind the import statements.
All that we need to do in order to create a module is make a Python file. It really is that simple. Let's take a small example. Create a new folder to hold our example and add a short simple file:
# mymod.py
myvariable = 15

def do_a_thing():
print('mymod is doing something')

def do_another_thing():
print('mymod is doing something else, and myvariable is', myvariable)
This may look the same as any normal Python file, but we can treat this as a reusable module if we want to. To demonstrate, open up a terminal window, change into the directory you have just created with this file in, and then run the Python REPL:
>>> import mymod
>>> mymod.do_a
mymod.do_a_thing( mymod.do_another_thing(
>>> mymod.do_a_thing()
mymod is doing something
>>> mymod.do_another_thing()
mymod is doing something else, and myvariable is 15
Since we are in the directory in which the mymod.py file is stored, we are able to import it in the same way as anything from the standard library. This is due to how importing works.
When you add import mymod to a Python file, you are telling it to look for a file or package with the name mymod. Python won't scan the whole computer though, only the places defined in your Python install's default path, as well as in a special os variable called PYTHONPATH. These variables simply tell the Python interpreter which folders to check for the imported files or packages.
To check out our path and PYTHONPATH, we can go back to our REPL and do this:
>>> import sys
>>> import os
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/lib64/python3.6/site-packages', '/usr/lib64/python3.6/_import_failed', '/usr/lib/python3.6/site-packages']
>>> os.environ['PYTHONPATH']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'PYTHONPATH'
From the previous code, we can note that my machine does not have a PYTHONPATH environment variable configured, so all modules will be located from the default path found in sys.path.
Speaking of which, the locations listed in sys.path are all displayed, the first of which is an empty string, signaling that the first place that will be searched is the current directory that the file is being executed from. This is why the interpreter was able to find my mymod.py file even though we had just created it in a normal folder.
Once a module has been imported, its classes, functions, and variables are all accessible to the importer (unless specifically protected). Depending on the type of import performed, they will be accessed differently.
To better demonstrate this point, we'll need to change myvariable to a list:
myvariable = ['important', 'list']
Now let's have a look at importing mymod the regular way:
>>> import mymod
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'mymod']
>>> dir(mymod)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'do_a_thing', 'do_another_thing', 'myvariable']
>>> mymod.myvariable
['important', 'list']
>>> mymod.do_another_thing()
mymod is doing something else, and myvariable is ['important', 'list']
>>> myvariable = ['silly', 'list']
>>> mymod.myvariable
['important', 'list']
>>> mymod.do_another_thing()
mymod is doing something else, and myvariable is ['important', 'list']
When importing with a plain import mymod statement, we are able to inspect the module via Python's dir function. This lists all of the available attributes, methods, and variables within the module.
We also use dir to inspect the global namespace to see that we do not have direct access to anything inside mymod. To use its features, we must prepend mymod. to them.
You will notice that the variable myvariable is accessed as mymod.myvariable, meaning we are free to define another myvariable without affecting the one used by our mymod module. Even when we redefine myvariable to ['silly', 'list'], our mymod module will still have access to the original value of ['important', 'list'].
Let's try the other import type using from and *:
>>> from mymod import *
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'do_a_thing', 'do_another_thing', 'myvariable']
>>> myvariable
['important', 'list']
>>> myvariable.append('junk')
>>> do_another_thing()
mymod is doing something else, and myvariable is ['important', 'list', 'junk']
>>> myvariable.remove('important')
>>> do_anot...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. Meet Tkinter
  7. Back to the Command Line – Basic Blackjack
  8. Jack is Back in Style – the Blackjack GUI
  9. The Finishing Touches – Sound and Animation
  10. Creating a Highly Customizable Python Editor
  11. Color Me Impressed! – Adding Syntax Highlighting
  12. Not Just for Restaurants – All About Menus
  13. Talk Python to Me – a Chat Application
  14. Connecting – Getting Our Chat Client Online
  15. Making Friends – Finishing Our Chat Application
  16. Wrapping Up – Packaging Our Applications to Share
  17. Other Books You May Enjoy