Python Database Programming: Part Six

Python Database ProgrammingIn the previous article, we covered creating and accessing databases with Sqlite. In this article, we continue our look at the Python database APIs.

You must download a separate DB API module for each database you need to access. Modules exist for most major databases with the exception of Microsoft’s SQL Server. You can access SQL Server using an ODBC (Open Database Connectivity) module, though. In fact, the mxODBC module can communicate with most databases using ODBC on Windows or an OBDC bridge on UNIX or Linux.

A complete listing of databases is available at the official Python wiki. You just need to download the modules you need, and follow the instructions that come with the modules to install them. You may need a C compiler and build environment to install some of the database modules. If you do, it will be described in the module’s own documentation. For some databases, such as oracle, you can choose among a number of slightly different modules.

Python Database Programming: Creating a Connection Object 

A Connection object provides the means to communicate from your script to a database program. Note the major assumption here that the database is running in a separate process (or processes). The Python database modules connect to the database. They do not include the database application itself.

Each database module needs to provide a connect function that returns a connection object. The parameters that are passed to connect vary by the module and what is required to communicate with the database.

Parameter Usage
Dsn Data source name. This usually includes the name of your database and the server where it’s running.
Host Host, or network system name, on which the database runs.
Database Name of the database.
User User name for connecting to the database.
Password Password for the given user name.

The above table summarizes some of the more common parameters. For example, here’s a typical connect statement:

conn = dbmodule.connect(dsn='localhost:MYDATABASE', user='chris', password='agenda')

With a Connection object, you can work with transactions, close the connection, and get a cursor.

A cursor is a Python object that enables you to work with the database. In database terms, the cursor is positioned at a particular location within a table or tables in the database, much like the cursor on your screen when you’re editing a document, which is positioned at a pixel location.

To get a cursor, you need to call the cursor method on the connection object:

cursor = conn.cursor()

Once you have a cursor, you can perform operations on the database, such as inserting records.

import os
import sqlite3

conn = sqlite3.connect('my_database')
cursor=conn.cursor()

# Create players
cursor.execute("""
insert into player(idnum,lastname,firstname,age,team,lefthanded,totalwar,earliestfreeagent)
values(100,"d'Arnaud",'Travis',26,18,'No',0.0,2020)""")

cursor.execute("""
insert into player(idnum,lastname,firstname,age,team,lefthanded,totalwar,earliestfreeagent)
values(101,'Duda','Lucas',29,18,'Yes',2.9,2018)""") 

cursor.execute("""
insert into player(idnum,lastname,firstname,age,team,lefthanded,totalwar,earliestfreeagent)
values(102,'Harper','Bryce',22,20,'Yes',9.6,2019)""")

# Create teams
cursor.execute("""
insert into team(teamid,teamname,teamballpark)
values(18,'New York Mets','Citi Field')""")

cursor.execute("""
insert into team(teamid,teamname,teamballpark)
values(20,'Washington Nationals','Nationals Park')""")

conn.commit()
cursor.close()
conn.close()

The first few lines of the script set up the database connection and create a cursor object:

import os
import sqlite3
conn = sqlite3.connect('my_database')
cursor=conn.cursor()

Note how we connect to an Sqlite database. To conect to a different database, replace this with your database-specific module, and modify the call to use the connect function from that database module, as needed.

The next several lines execute a number of SQL statements to insert rows into the two tables set up earlier: player and team. The execute method on the cursor object executes the SQL statement:

cursor.execute("""
insert into player(idnum,lastname,firstname,age,team,lefthanded,totalwar,earliestfreeagent)
values(100,"d'Arnaud",'Travis',26,18,'No',0.0,2020)""")

This example uses a triple-quoted string to cross a number of lines as needed. You will find that SQL commands, especially those embedded within Python scripts, are easier to understand if you can format the commands over a number of lines. This becomes helpful with more complex queries. Also, note that we used double quotes around the last name in this example (“d’Arnaud”) so we could include an apostrophe in the name (if we used single quotes, the apostrophe, identical to a single quote mark, would have denoted the end of the string).

To save your changes to the database, you must commit the transaction:

conn.commit()

Note that this method is called on the connection, not the cursor.

When you are done with the script, close the cursor and then the connection to free up resources. In short scripts like this, it may not seem important, but it helps the database program free its resources, as well as your Python script:

cursor.close()
conn.close()

You now have a small amount of sample data to work with while using other parts of the DB API.

External Links:

Database Programming at wiki.python.org

Database Programming at python.about.com

Databases at docs.python-guide.org

Python Database Programming: Part Five

Python database programming

Using Eclipse to create an Sqlite database.

In most cases when you are doing database programming, the database will be up and running. Your website hosting, for example, may have MySQL database access included. Your IT department may have standardized on a particular database, such as Oracle, DB/2, Sybase, or Informix.

Python Database Programming: Sqlite

But if you have no database yet, a good starting database is Sqlite. The main advantages of Sqlite are that it comes installed with Python and it is simple, yet functional. Using Sqlite is as simple as importing a module (sqlite3).

If you are working with another database, such as SQL Server, then the chances are good that a database has already been created. If not, follow the instructions from you database vendor.

import os
import sqlite3
conn = sqlite3.connect('my_database')
cursor=conn.cursor()
# Create tables
cursor.execute("""
create table player
    (idnum integer,
    lastname varchar,
    firstname varchar,
    age integer,
    team integer,
    lefthanded varchar,
    totalwar float(5,1),
    earliestfreeagent integer)
""")
cursor.execute("""
create table team
    (teamid integer,
    teamname varchar,
    teamballpark varchar)
""")
# Create indices
cursor.execute("""create index teamid on team (teamid)""")
cursor.execute("""create index idnum on player (idnum)""")
cursor.execute("""create index teamidfk on player(team)""")
conn.commit()
cursor.close()
conn.close()

This script should produce no output unless it raises an error. It uses the Sqlite API, which is somewhat similar to the DB API. After the import statements, the script creates a database connection with this statement:

conn = sqlite3.connect('my_database')

From there, the script gets a Cursor object. The Cursor object is used to create two tables (the Player and Team tables from the previous two articles) and define indexes on these tables.

The script then calls the commit method on the connection to save all the changes to disk. Sqlite stores all the data in the file my_database, which you should see in your Python directory.

Python’s support for relational databases started with ad hoc solutions, with one solution written to interface with each particular database, such as Oracle. Each database module created its own API, which was highly specific to that database because each database vendor evolved its own API based on its own needs. This can be difficult to support, because moving from one database to another requires that everything be rewritten and retested.

Over the years, Python has come to support a common database known as the DB API. Specific modules enable your Python scripts to communicate with different databases, such as DB/2, PostgreSQL, and so on. All of these modules support the common API, which makes your job a lot easier when you write scripts to access databases.

The DB API provides a minimal standard for working with databases, using Python structures and syntax wherever possible. This API includes the following:

  • Connections, which cover guidelines for how to connect to databases
  • Executing statements and stored procedures to query, update, insert and delete data with cursors
  • Transactions, with support for committing or rolling back a transaction
  • Examining metadata on the database module as well as on database and table structure
  • Defining the types of errors

In the next article, we will begin our look at the DB API.

External Links:

Database Programming at wiki.python.org

Database Programming at python.about.com

Databases at docs.python-guide.org

Python Database Programming: Part Four

Python Database ProgrammingIn the previous article, we introduced the concept of relational databases. In this article, we introduce the Structured Query Language, or SQL.

Python Database Programming: SQL

SQL (pronounced “sequel” or “S-Q-L”) defines a standard language for querying and modifying databases. SQL supports the following basic operations:

Operation Usage
Select Perform a query to search the database for specific data.
Update Modify a row or rows, usually based on a certain condition.
Insert Create new rows in the database.
Delete Remove a row or rows from the database

SQL offers more than these basic operations, but at least initially, these are the operations you will be using in writing Python applications: Query, Update, Insert and Delete (QUID), or Create, Read, Update and Delete (CRUD).

If you are not familiar with SQL, you’re going to want a good SQL book. O’Reilly has a useful book on SQL which can be downloaded here, and if you’re looking for a user-friendly SQL book, one is available here.

SQL is important because when you access databases with the Python DB API, you must first create SQL statements and then execute these statements by having the database evaluate them. Thus you will be using Python statements to execute SQL statements.

The basic SQL syntax for the operations mentioned above are:

SELECT columns FROM tables WHERE condition ORDER by columns ascending_or_descending

UPDATE table SET new values WHERE condition

INSERT INTO table (columns) VALUES (values)

DELETE FROM table WHERE condition

This covers the basic syntax, but there are many more optional parameters and specifiers available. You can use these options with Python’s DB API.

To insert a new row in the player table from the previous article, you can use an SQL query like this one (even though we are adding data and not getting data, the convention is that all SQL commands or statements are called queries):

insert into player (idnum, lastname, firstname, age, team, left-handed, totalwar, earliestfreeagent) values (103, ‘Murphy’, ‘Daniel’, 30, 18, ‘Yes’, 10.9, 2016)

In this example, the first tuple holds the names of the columns in the order you are using for inserting your data. The second tuple, after the keyword values, holds the data in the same order. Notice how SQL uses single quotes to delimit strings, and no quotes around numbers.

With queries, you can use wildcards such as * to say that you want an operation to be performed using all of the columns in a table. For example, to query all of the rows in the team table, showing all of the columns for each row, you can use a query like this:

select * from team

Note that SQL is not case-sensitive for its keywords, such as SELECT and FROM. But some databases require table and column names to be all uppercase. It is thus common to see people use SELECT and FROM and other operations in all capital letters to make them more easily distinguished from other parts of the query.

This SQL statement omits the names of the columns to read and any conditions that would otherwise narrow down the data that would be returned. Thus the query will return all of the columns (from the *) and all of the rows (because there is no where clause).

You can also perform a join with the select command, to query data from more than one table, but present it all in a single response. For example, to extract the team name with each player, you could perform a query like the following:

select player.firstname, player.lastname, team.name from player, team
where player.team = team.teamid order by lastname desc

In this example, the select statement requests two columns from the player table (the firstname and the lastname, but these are specified as coming from player by the convention of specifying the table name and the column name in the table) and one from the team table (team.name). The order by section of the statement tells the database to order the results by the value in the lastname column, in descending order.

To simplify these queries, you can use aliases for the table names, which make them easier to type and read. For example, to use the alias p with the player table, you can start a query like this:

select p.firstname, p.lastname from player p

In this case, you must place the alias, p, after the table name in the from clause. You can also use the following format with the optional keyword as, which could be easier for you to read:

select p.firstname, p.lastname from player as p

To modify or update a row, use a SQL statement like the following:

update player set age=27 where idnum=100

This example modifies the player with an idnum of 100 by setting that player’s age to 27. As with other queries, numbers do not need to have quotes around them; however, strings would need to be quoted with single quotes.

To delete a row, use an SQL statement like the following:

delete player where idnum=101

This example deletes the player with an idnum of 101, but doesn’t affect anything else in the database.

External Links:

Database Programming at wiki.python.org

Database Programming at python.about.com

Databases at docs.python-guide.org

Python Database Programming: Part Three

Python Database ProgrammingIn the previous article, we showed how to create, access and modify a persistent dictionary in Python using the dbm module. In this article, we will consider using Python to create, access and modify a relational database.

The dbm modules work well when your data needs to be stored as key/value pairs. You can store more complicated data within key/value pairs with some imagination. For example, you can create formatted strings that use a comma or some other character to delimit items in the strings. This, however, can be difficult to maintain, and it can restrict you because now your data is stored in an inflexible manner. In addition, some dbm libraries limit the amount of space you can use for the values – sometimes to a maximum of 1024 bytes.

The upshot of all this is that if your data needs are simple and you only plan to store a small amount of data, you should use a dbm persistent dictionary. If, on the other hand, you require support for transactions and if you require complex data structures or multiple tables of linked data, you should use a relational database. If you use relational databases, you will also find that they provide a far richer and more complex API than the simple dbm modules.

Python Database Programming: Introducing Relational Databases

In a relational database, data is stored in tables that can be viewed as two-dimensional data structures. The columns, or vertical part of the two-dimensional matrix, are all of the same type of data (e.g. strings, numbers, dats, etc.). Each horizontal component of the table is made up of rows, also called records. Each row is made up of columns. Typically, each record holds the information pertaining to one item.

idnum last name first name age Team left-handed total war earliest free agency
100 d’Arnaud Travis 26 18 No 0.0 2020
101 Duda Lucas 29 18 Yes 2.9 2018
102 Harper Bryce 22 20 Yes 9.6 2019

This table holds seven columns about baseball players:

  • idnum: The player’s ID number. Relational databases make extensive use of ID numbers where the database manages the assignment of unique numbers so that each row can be referenced with these numbers to make each row unique, even if they have identical data. We can then refer to the player by the ID number. The ID number alone provides enough information to look up the employee.
  • lastname: Holds the person’s last name.
  • firstname: Holds the player’s first name.
  • age: Holds the player’s age.
  • team: Holds ID of the player’s team.
  • left-handed: Holds whether the player is left-handed.
  • total war: Holds the player’s total WAR (Wins Above Replacement).
  • earliest free agent: Holds the earliest year the player will be eligible for free agency.

In this example, the column idnum, the ID number, would be used as the primary key. A primary key is a unique index for a table, where each element has to be unique because the database will use that element as the key to the given row and as a way to refer to the data in that row, in a manner similar to dictionary keys and values in Python. Thus, each player needs to have a unique ID number, and once we have an ID number, we can look up any player. Therefore it makes sense to make idnum the key.

The team column holds the ID of a team – that is, an ID of a row in another table. This ID could be considered a foreign key, because the ID acts as a key into another table.

For example, here is a possible layout of the teams table:

team id name ballpark
18 New York Mets Citi Field
20 Washington Nationals Nationals Park

In these examples, Travis d’Arnaud and Lucas Duda play for team 18, the New York Mets. Bryce Harper plays for team 20, the Washington Nationals.

In a large enterprise, there may be hundreds of tables in the database with thousands (or even millions) of records. In the next article, we will cover how to make SQL queries with Python.

External Links:

Python Database Programming at wiki.python.org

Python Database Programming at python.about.com

Databases at docs.python-guide.org

Python Database Programming: Part Two

Python database programming

Using the Eclipse IDE to access and modify a Python persistent dictionary.

In the previous article, we introduced Python database programming , the concept of persistent dictionaries, and different database modules such as dbm. In this article, we will put it all together and use the dbm module to create, access and modify a persistent dictionary.

All of the dbm modules support an open function to create a new dbm object. Once opened, you can store data in the dictionary, read data, close the dbm object as well as the associated data file/files, remove items and test for the existence of a key in the dictionary.

Python Database Programming: Creating a Persistent Dictionary

To open a dbm persistent dictionary, use the open function on the module you choose. For example, we can use this code to create a persistent dictionary with the dbm module:

import dbm

db = dbm.open('payroll', 'c')

# Add on item
db['Orioles'] = '118'
db['Yankees'] = '211'
db['Blue Jays'] = '120'

print(db['Orioles'])

# Close and save to disk
db.close()

When you run this script, you will see output like the following:

b'118'

This example, which creates a ‘payroll’ dictionary with three entries, uses the recommended dbm module. The open function requires the name of the dictionary to create. The name gets translated into the name of the data file or files that may already be on the disk. The dbm module may create more than one file (usually a file for the data and one for the index of the keys), but it does not always do this. The name of the dictionary is treated as a base file name, including the path. Usually, the underlying dbm library will append a suffix such as .dat for data. You can find the file yourself by looking for the file named payroll, most likely in your current working directory.

There is also an optional flag. The following table lists the available flags:

Flag Usage
C Opens the data file for reading and writing, creating the file if needed.
N Opens the file for reading and writing, but always creates a new empty file. If one already exists, it will be overwritten and its contents lost.
W Opens the file for reading and writing, but if the file doesn’t exist it will not be created.

You can also set another optional parameter, the mode. The mode holds a set of UNIX file permissions.

The above code is simple. First, we use the open method of the dbm module, which returns a new dbm object (db), which we can then use to store and retrieve data.

Once we open a persistent dictionary, we can write values as we normally would with Python dictionaries, as shown in this example:

db['Orioles'] = '118'

Both the key and value must be strings and cannot be other objects, like numbers or Python objects. But if you want to save an object, you can serialize it using the pickle module:

import pickle

data = {
        'Orioles' : ['118', 'Dan Duquette', 'Buck Showalter', 'Camden Yards'],
        'Yankees' : ['211', 'Brian Cashman', 'Joe Girardi', 'Yankee Stadium III'],
        'Blue Jays' : ['120', 'Alex Anthopoulos', 'John Gibbons', 'Rogers Centre']
        }

with open('data.pickle', 'wb') as f:
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
    
with open('data.pickle', 'rb') as f:
    data = pickle.load(f)

Finally, the close method closes the file or files and saves the data to disk.

Python Database Programming: Accessing and Modifying the Persistent Database

With the dbm modules, you can treat the object you get back from the open function as a dictionary object. You can get and set values using code like the following:

db['key'] = 'value'
value = db['key']

Remember that the key and the value must both be text strings.

You can delete a value in the dictionary using del:

del db['key']

As with a normal dictionary, the keys method returns a list of all the keys:

for key in db.keys():
	# do something else

The keys method may take a long time to execute if there are a huge number of keys in the file. Also, this method may require a lot of memory to store the potentially large list that it would create with a large file.

Here’s a script we can use to access the persistent dictionary we created with the first script:

import dbm

# Open existing file
db = dbm.open('payroll', 'w')

# Add another item
db['Rays'] = '67'

# Verify the previous item remains
if db['Blue Jays'] != None:
    print('Found Blue Jays')
else:
    print('Error: Missing item')
    
# Iterate over the keys...may be slow
# May use a lot of memory
for key in db.keys():
    print('Key = ', key, ' value = ', db[key])
    
del db['Rays']
print('After deleting Rays, we have:')

for key in db.keys():
    print('Key = ', key, ' value = ', db[key])
    
# Close and save to disk
db.close()

When you run this script, you should see output similar to the following:

Found Blue Jays
Key =  b'Rays'  value =  b'67'
Key =  b'Orioles'  value =  b'118'
Key =  b'Yankees'  value =  b'211'
Key =  b'Blue Jays'  value =  b'120'

After deleting Rays, we have:

Key =  b'Orioles'  value =  b'118'
Key =  b'Yankees'  value =  b'211'
Key =  b'Blue Jays'  value =  b'120'

This script works with a small database of major league baseball teams and their payrolls (in millions of dollars). You need to run the first script in this article first. That example creates the dbm file and stores data in the file. This script then opens the preexisting dbm file.

The script opens the persistent dictionary payroll in read/write mode. The call to the open function will generate an error if the necessary data file or files do not exist on disk in the current directory.

From the previous example, there should be three values in the dictionary (the new script tests to see if one of them exists). This example adds the Tampa Bay Rays, with a payroll of $67 million, as another key.

The script verifies that the ‘Blue Jays’ key exists in the dictionary, using the following code:

if db['Blue Jays'] != None:
    print('Found Blue Jays')
else:
    print('Error: Missing item')

Next, the script prints out all of the keys and values in the dictionary:

for key in db.keys():
    print('Key = ', key, ' value = ', db[key])

Note that there should now be four entries.

After printing out all the entries, the script removes one using del:

del db['Rays']

The script then prints out all the keys and values again, which should result in three entries, as show in the output. Finally, the close method closes the dictionary, which involves saving all the changes to disk, so the next time the file is opened, it will be in the state we left it.

As you can see from these examples, the API for working with persistent dictionaries is very simple because it works with files and like dictionaries.

External Links:

Python Database Programming at wiki.python.org

Python Database Programming at python.about.com

Databases at docs.python-guide.org

Python Database Programming: Part One

Python databaseMost large enterprise-level systems use databases for storing data. In order for Python to be capable of handling these types of enterprise applications, the language must be able to access databases.

For Python database programming, Python provides a database Application Programming Interface (API) that enables you to access most databases regardless of the databases’ native API. Although minor differences exist between different implementations of databases, for the most part you can access databases such as Oracle or MySQL from your Python scripts without worrying too much about the details of the specific databases. There are two main database systems supported by Python: dbm persistent dictionaries and relational databases with the DB API. Moreover, you can use add-ons such as MySQL-python to make direct database queries from within your Python scripts.

Python Database Programming: Persistent Dictionaries

A persistent dictionary, as the name suggests, is a Python dictionary that can be saved to disk. You store name/value pairs in the dictionary, which is saved. Thus, if you save data to a dictionary that’s backed by a dbm, the next time you start your program, you can read the value stored under a given key again, once you’ve loaded the dbm file. The dictionaries work like normal Python dictionaries; you might recall that the syntax of a statement creating a dictionary looks something like this:

payroll = { ‘Orioles’: 118, ‘Yankees’: 211, ‘Blue Jays’: 120 }

With a persistent dictionary, the main difference is that the data is written to and read from disk. An additional difference is that the keys and the values must both be strings; therefore our above example would have to be rewritten:

payroll = { ‘Orioles’: ‘118’, ‘Yankees’: ‘211’, ‘Blue Jays’: ‘120’ }

Python Database Programming: Modules

Python supports a number of dbm modules for Python database programming. Each dbm module supports similar interface and uses a particular C library to store the data to disk. The difference is in the underlying binary format of the data files on disk.

DBM, short for database manager, acts as a generic name for a number of C language libraries originally created on UNIX systems. The names of these libraries (e.g. dbm, gdbm, etc.) correspond closely to the available modules that provide the needed functionality within Python.

Python supports a number of dbm modules, each of which supports a similar interface and uses a particular C library to store the data. The underlying binary format of each module is different. As a result, each dbm module creates incompatible files. If you create a dbm persistent dictionary with one dbm module, you must use the same module to read the data. None of the other modules will work with a data file created by another module.

Module Description
dbm Chooses the best dbm module
dbm.dumb Uses a simple, but portable, implementation of the dbm library
dbm.gnu Uses the GNU dbm library

Originally, this library was only available with the commercial versions of UNIX. This led to the creation of alternative libraries: e.g. the Berkeley UNIX library and GNU’s gdbm.

With all the incompatible file formations, all these libraries can be an issue. But by using the dbm module, you can sidestep this issue. The dbm module will choose the best implementation available on your system when creating a new persistent dictionary. When it reads a file, the dbm module uses the whichdb function to make an informed guess as to which library created the database. It is usually good practice to use the dbm module, unless you need to use a specific feature of one of the dbm libraries.

In the next article on Python database programming, we’ll start to cover the nuts and bolts of programming using the dbm module in Python.

External Links:

Python Database Programming at wiki.python.org

Python Database Programming at python.about.com

Databases at docs.python-guide.org

Tkinter GUI Framework: Part Three

Tkinter GUI

Creating dialog boxes in Tkinter using the Pydev module with Eclipse.

Form and function are key to creating a good GUI. In this article, we’ll go one step further and control the actual appearance of widgets.

Up to this point, we have used the default look for our widgets. This is somewhat drab, so to create programs that are visually appealing, we want to tweak the look of our widgets. For example, we can change the font and background/foreground colors.

import tkinter
from tkinter import *
root = Tk()
labelfont = ('times', 36, 'italic')

widget = Label(root, text='This is a test.')
widget.config(bg='black', fg='blue')

widget.pack(expand=YES, fill=BOTH)
root.mainloop()

Although all the program does is display a window with some text in it, the design does draw the user’s attention.

Radio Buttons and Checkboxes

In addition to windows, frame, labels and buttons, sometimes you want to use radio buttons and checkboxes to give your users more choices. Aside from appearance, radio buttons and checkboxes differ in one significant way: radio buttons offer users a list of options, but allow them to select only one; checkboxes offer users many options and lets them choose as many as they want.

For example, here’s some code that implements radio buttons:

import tkinter 
from tkinter import *
state = ''
buttons = []

def choose(i): 
	global state
	state = i
	for btn in buttons: 
		btn.deselect()
	buttons[i].select()

root = Tk()
for i in range(4):
	radio = Radiobutton(root, text=str(i), value=str(i), command=(lambda i=i: choose(i) )
	radio.pack(side=BOTTOM) 
	buttons.append(radio)

root.mainloop()
print("You chose the following number: ",state)

This program creates a series of buttons ranging from 0-3 (four in total) with the number 1, 2 and 3 highlighted by default. The user can then choose any of the buttons. Once a button is chosen, any other button’s state becomes False, meaning that the button is no longer selected. When the users close out of the program, they are given a statement showing which number they chose.

For a similar effect with checkboxes, try this code:

from tkinter import *
states = []
def check(i):
	states[i] = not states[i]

root = Tk()

for i in range(4):
	test = Checkbutton(root, text=str(i), command=(lambda i=i: check(i)) )
	test.pack(side=TOP)
	states.append(0)
root.mainloop()
print(states)

The last line in the program is print(states), which prints out the states list – the values of the check boxes. This, if you check off boxes 1 and 4, your result will be:

[True, 0, 0, True]

Dialog Boxes

Sometimes you will want to give the user a piece of additional information. You have probably seen dialog boxes in programs. They pop up whenever there is an error, or a program wants to confirm something. Tkinter offers two types of dialog boxes: modal and nonmodal. Modal dialog boxes wait for some action from the user before going away, andpause the progress of the program. Nonmodal dialog boxes do not interrupt the flow of the program.
Creating a dialog box in Tkinter is almost a trivial process. For example, we can create a simple function to generate a dialog box like so:

def dialog():
	win = Toplevel()
	Label(win, text='This is a dialog box').pack()

We need some means of invoking the dialog box from the main window, so the main program will have this code:

root = Tk()
Button(root, text='Click This', command=dialog).pack()
root.mainloop()

You will also need the usual headers at the beginning:

import sys
from tkinter import *

This will create a main window, and when you click on the button in the main window, it will launch a simple, nonmodal dialog box that has some text in it. This is OK, but we really want a dialog box that actually does something, so let’s rewrite the dialog function:

def dialog():
	win = Toplevel()
	Label(win, text='This is a dialog box').pack()
	Button(win, text='Click this button', command=win.destroy).pack()
	if makemodal:
		win.focus_set()
		win.grab_set()
		win.wait_window()

You will need to add this line before the dialog function:

makemodal = (len(sys.argv) > 1)

When the button in the main window is clicked, the program will launch a modal dialog box and that box will take the focus. When you click on the button in the dialog box, it will close itself, and the original window will take back focus.

External Links

How to Install Tkinter at unpythonic.net

Tkinter wiki at python.org

Tkinter GUI Framework: Part Two

Tkinter GUIIn the previous article, we introduced Tkinter and provided some simple examples. In this article, we will introduce the concept of layouts.

GUI Layouts: Widget Hierarchy and Packing Order

When creating a GUI, it is important to consider the hierarchy of the widgets inside the GUI. This hierarchy is commonly referred to as parent-child. Let’s consider a simple example:

from tkinter import *

def fcall():
	print('This is a function call.')

win = Frame()
win.pack()
Label(win, text='Click Function to make a function call or Quit to Exit').pack(side=TOP)
Button(win, text='Add', command=result).pack(side=LEFT)
Button(win, text="Quit', command=win.quit).pack(side=RIGHT)

The first widget here is the top-level window, which acts as a parent. Next, we have a widget called win, which has a child of its own: a frame widget. The win widget is a child of the top-level window.

Next, we have a label and two buttons, all of which are children of the frame widget. A frame widget is a widget whose purpose is to hold other widgets, and thus allow the programmer the flexibility to create a layout determining where on the window each widget should appear. GUI programming involves working with many different frame widgets, each occupying a specific spot on the top-level window, with each frame having its own set of widgets. These widgets that belong to each frame will, as children of their own respective frame, be limited to the space provided them by their parent frame.

For example, if you have two frames of the same size, each taking up half the window, and assign a button widget to each frame, the button assigned to the left frame will only be able to be placed within the left-hand side of the window. Likewise, the button assigned to the frame on the right side will be constrained to that section. If you were to pack the button in the left frame to the right, it would appear to the user to be in the center of the top-level window.

Another important aspect of layout is known as packing order. When you create each widget, and pack them, they are given all of the space for their region. For example, if you pack a button on the LEFT, it will occupy all of the left-hand space. When you create a second widget and pack it to the left as well, the initial button is shrunk, but still holds the left-most space. This process continues, with each button shrunking to provide room for the other widgets. However, the buttons never move from their original space. The first button packed to the left will always be the leftmost. Likewise, the second button packed to the left will always be the second closest to the left.

For example, we could rearrange our code from earlier to demonstrate how this works:

from tkinter import *

def fcall():
	print('This is a function call.')

win = Frame()
win.pack()
Button(win, text='Add', command=result).pack(side=LEFT)
Label(win, text='Click Function to make a function call or Quit to Exit').pack(side=TOP)
Button(win, text="Quit', command=win.quit).pack(side=RIGHT)

This shows how the program looked before we modified the code:

Tkinter GUI

And this is how it looks with the modified code:

Tkinter GUI

External Links

How to Install Tkinter at unpythonic.net

Tkinter wiki at python.org

Tkinter GUI Framework: Part One

Tkinter

Tkinter in action with the PyDev module in Eclipse under Windows 8.1.

There is wide support for writing GUIs with Python with many different toolkits. These toolkits, binary modules for Python that interface with native code written in C/C++, all have different APIs and different feature sets. Only one comes with Python by default: the TK GUI toolkit.

Other options are wxPython, PyQT, and pyGTK. For this series of articles, we will be using Tkinter, which is Python’s standard GUI package and comes installed with Python. It is perhaps the most used GUI programming kit and is portable across virtually every platform.

Installing Tkinter

If you installed Python under Windows, the Python Windows installer includes Tcl/Tk as well as Tkinter. This is essentially a one-click install of everything needed. Linux and the BSD platforms require a separate installation of Tcl/Tk. This is usually available as a binary package. Under Ubuntu/Debian/Mint, you can use the following command:

sudo apt-get install python python-tk idle python-pmw python-imaging

This installs Python, Tkinter (which is not really needed since it automatically gets installed with Python), IDLE, Python MegaWidgets and PIL. In Fedora Core, it’s:

yum install tinker

Getting Started with Tkinter

The first thing to understand is that most GUI frameworks, including Tkinter, are based on a widget model. A widget is a component of a GUI. Buttons, labels and text boxes are all widgets. Most widgets have graphical representations on screen, but some widgets (such as tables and boxes) exist only to contain other widgets and arrange them on the screen. A GUI is constructed out of an arrangement of widgets.

In this script, we create a GUI of a simple window and a label:

import tkinter
from tkinter import *
widget = Label(None, text='This is my first GUI')
widget.pack()
widget.mainloop()

The first thing the script does is import the Tkinter module. Next, we could either import Label from Tkinter, for simply import all (*) from Tkinter (we did the latter). After that, we create an object for each widget (in this case, Label). The Label is then arranged in the parent window. Finally, the widget is displayed.

This is acceptable as a first GUI script, but it would be nice if the label would re-center itself when the dialog box that contains it is resized (if you try to resize it, you will see that this is not the case). However, we can fix this with a small code change:

import tkinter
from tkinter import *
Label(text='This is my first GUI').pack(expand=YES, fill=BOTH)
mainloop()

When you run the program, try resizing the window. You will see the “This is my first GUI” text label stay centered no matter what the window looks like.

In these initial examples, we created the widgets and configured them at the same time. However, we may want to wait to configure them after they are created:

import tkinter
from tkinter import *
root = Tk()

widget = Label(root)
widget.config(text='This is my first GUI')
widget.pack(side=TOP, expand=YES, fill=BOTH)
root.mainloop()

In this example, we called upon the configure method to achieve the same result as in the previous example. If we wanted to, we could change the appearance of the widget later in the program.

External Links

How to Install Tkinter at unpythonic.net

Tkinter wiki at python.org

Python Iterators: Part Seven

Python iteratorsThe map, zip and filter built-in functions also became iterators in Python 3.0, as did range. This conserves space, as the result list does not have to be produced all at once in memory. All three process iterables and return iterable results in Python 3.0. Unlike range, however, they are their own iterators, and once you step through the results, they are exhausted and you cannot have multiple iterators on their results.

The basic form of the map function is:

map(function, iterable, ...)

The built-in map function returns an iterator that applies a function to every item of an iterable, yielding the resulting iterator. If additional arguments are passed, the function specified as the first argument must take that many arguments and apply them to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For example:

>>> mylist = map(abs, (1, 2, -3))
>>> list(mylist)
[1, 2, 3]
>>> mylist

 <map object at 0x0000000004069D30>

>>> next(mylist)
1
>>> next(mylist)
2
>>> next(mylist)
3
>>> next(mylist)
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in 
    next(mylist)
StopIteration

>>> for x in mylist:
	print(x)

In our for loop, the map iterator is empty, so no output is produced. To do that, we need to create a new iterator:

>>> mylist = map(abs, (-4, 5, -6))
>>> for x in mylist:
	print(x)
	
4
5
6
>>> list(map(abs, (-4, 5, -6)))
[4, 5, 6]

The zip built-in function, introduced in the previous article, returns iterators that work the same way:

>>> myzip = zip((1, 2, 3), (4, 5, 6))
>>> myzip


>>> list(myzip)
[(1, 4), (2, 5), (3, 6)]
>>> for pair in myzip:
	print(pair)  	# Exhausted after one pass

>>> myzip = zip((1, 2, 3), (4, 5, 6))
>>> for pair in myzip:
	print(pair)

(1, 4)
(2, 5)
(3, 6)

>>> myzip = zip((1, 2, 3), (4, 5, 6))
>>> next(myzip)
(1, 4)
>>> next(myzip)
(2, 5)

The filter built-in function also behaves in a similar way. It returns items in an iterable for which a passed-in function returns True:

>>> filter(bool, ['not null', '', 'abc', 1, 0, -1])

['not null', 'abc', 1, -1]

Like map and zip, filter both accepts an iterable to process and returns an iterable to generate results in Python 3.0.

External Links:

The map built-in function at docs.python.org
Python Iterators at Python Wiki
Python Iterator tutorial at bogotobogo.com