Raspberry Pi 3 Cookbook for Python Programmers
上QQ阅读APP看书,第一时间看更新

Getting ready

You must ensure that this file is placed in the same directory.

Since we are using Tkinter (one of many available add-ons for Python), we need to ensure that it is installed. It should be installed by default on the standard Raspbian image. We can confirm it is installed by importing it from the Python prompt, as follows:

Python3
>>> import tkinter

If it is not installed, an ImportError exception will be raised, in which case you can install it using the following command (use Ctrl + Z to exit the Python prompt):

sudo apt-get install python3-tk

If the module did load, you can use the following command to read more about the module (use Q to quit when you are done reading):

>>>help(tkinter)

You can also get information about all the classes, functions, and methods within the module using the following command:

>>>help(tkinter.Button)

The following dir command will list any valid commands or variables that are in the scope of the module:

>>>dir(tkinter.Button)

You will see that our own modules will have the information about the functions marked by triple quotes; this will show up if we use the help command.

The command line will not be able to display the graphical displays created in this chapter, so you will have to start Raspberry Pi desktop (using the command startx), or if you are using it remotely.

Make sure you have X11 forwarding enabled and an X server running (see Chapter 1, Getting Started with a Raspberry Pi 3 Computer).