root.title('This is a test window')
root.geometry('300x100')
After you set these methods, you need to use the mainloop() method, which puts the window into
a loop, waiting for a window widget to trigger an event. As events occur in the window, Python
intercepts them and passes them to your program code. For example, if you click the X at the top-right
corner of the window, Python captures that event and knows to close out the window. (Later on,
you’ll code your own events to add to the window.) Listing 18.1 shows the tkinter window code
to create a simple window.
LISTING 18.1 The script1801.py Code
Click here to view code image
#!/usr/bin/python3
from tkinter import *
root= Tk()
root.title('This is a test window')
root.geometry('300x100')
root.mainloop()
To run the script1801.py code, you need to be in the LXDE graphical desktop on the Raspberry
Pi. Once you’re in the desktop, you can start the script from the command line by opening the
LXTerminal utility and then running the code from the command prompt, like this:
Click here to view code image
pi@raspberrypi ~$ python3 script1801.py
You don’t see anything happen at the command prompt, but you should see a simple window object
appear on your desktop, as shown in Figure 18.1.