-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram_18_Tk_States.py
More file actions
140 lines (110 loc) · 5.38 KB
/
program_18_Tk_States.py
File metadata and controls
140 lines (110 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#----------------------------------------------------------
# These websites are a good reference for Tk
# http://www.tkdocs.com/tutorial/index.html
# http://www.pythonware.com/library/tkinter/introduction/
#----------------------------------------------------------
from tkinter import *
from tkinter import ttk
#----------------------------------------------------------------
# This program will use the database (dictionary) of US States
# from program_12_States.py to allow the user to select a state
# from a Tk Listbox. When a state is selected, it's database
# information is displayed in a Label widget.
#----------------------------------------------------------------
# Create a UnitedStates object from program_12_States
import program_12_States
US = program_12_States.UnitedStates()
# Fill in the database from the file with the US.ReadDataFile() method
US.ReadDataFile( 'US_States.csv' )
# Get a list of the state names from the US_database
# We will show this list in the Listbox
stateNames = list( US.US_database.keys() )
# Since this list is from dictionary keys, the order is random
# sort them alphabetically
stateNames.sort()
# Initialize Tk, must be prior to creation of StringVar()
root = Tk()
root.title('US States Database')
# Tk I/O variables
# valid types are BooleanVar, DoubleVar, IntVar, StringVar
outputMessage = StringVar()
#----------------------------------------------------------------
# When the user selects an item from the Listbox, this callback
# function process the selected state by extracting the stored
# information from the US_database and updating the message
# text in outputMessage. The outputMessage is then displayed
# in the Label widget.
#----------------------------------------------------------------
def ProcessListbox(*args):
# Get the index of the state selected in the ListBox
i_selected = lbox.curselection() # tuple of indices as text
# Just process the first selection (i_selected[0])
# and output all the info for this single selection
if len( i_selected ) > 0:
i = int(i_selected[0]) # have to convert the text to an int
# we assume that the i corresponds to the index in stateNames
# get the data from the database into local variables
dateFounded = US.US_database[ stateNames[i] ].dateFounded
USPS = US.US_database[ stateNames[i] ].USPS
capital = US.US_database[ stateNames[i] ].capital
population = US.US_database[ stateNames[i] ].population
area = US.US_database[ stateNames[i] ].area
biggestCity = US.US_database[ stateNames[i] ].biggestCity
# Create an output message from the data
msg = stateNames[i] + ' was founded ' + dateFounded + '\n'
msg = msg + 'The capital is ' + capital + '\n'
msg = msg + 'The largest city is ' + biggestCity + '\n'
msg = msg + 'The population is ' + population + '\n'
msg = msg + 'The area is ' + area + '\n'
outputMessage.set(msg)
#----------------------------------------------------------------
# Create and grid the outer content frame
mainFrame = ttk.Frame( root, padding = (5, 5, 12, 0) )
mainFrame.grid( column = 0, row = 0, sticky = (N,W,E,S) )
# These grid_*configure functions make the widgets in the
# rows and columns expand as the root window is resized
# if weight = 0, there is no resize, if weight = 1, then
# resize to fill the whole window
root.grid_columnconfigure(0, weight = 1)
root.grid_rowconfigure (0, weight = 1)
# Create the Listbox to hold the state names
lbox = Listbox( mainFrame, height = 10 )
# Insert the state names into the Listbox
# The listvariable = [] option won't work if
# there is whitespace in a name, so insert them manually
for i in range( len(stateNames) ) :
lbox.insert( i, stateNames[i] )
# Create a Label to hold the outputMessage
stateInfo = ttk.Label( mainFrame, textvariable = outputMessage, anchor = E )
# Create a vertical scroll bar for the Listbox
# Tell the scrollbar that it will call the Listbox yview function
# when the user moves the scrollbar
scrollBar = ttk.Scrollbar( mainFrame, orient = VERTICAL, command = lbox.yview )
# Tell the Listbox that it will scroll according to the scrollBar
lbox.configure( yscrollcommand = scrollBar.set )
# Grid all the widgets - This is the layout of the window
# This application has 2 columns and 1 row
# The Listbox and scrollbar are in column 0,
# The outputMessage Label is in column 1
# Both the Listbox and outputMessage Label are in row 0
# padx = 23 prevents overlap with the scrollbar
lbox.grid( column = 0, row = 0, padx = 23, sticky=(N,S,E,W) )
scrollBar.grid( column = 0, row = 0, sticky = (W,N,S) )
stateInfo.grid( column = 1, row = 0, sticky=(W,E) )
# Don't resize the column 0 - the width of the Listbox
mainFrame.grid_columnconfigure(0, weight = 0)
# Resize the column 1 and row 0 as the window is resized
mainFrame.grid_columnconfigure(1, weight = 1)
mainFrame.grid_rowconfigure (0, weight = 1)
# This tells the Listbox to call the functin ProcessListbox()
# when the selection in the listbox changes
lbox.bind('<<ListboxSelect>>', ProcessListbox)
# Colorize alternating lines of the listbox
for i in range( 0, len(stateNames), 2 ):
lbox.itemconfigure( i, background = '#f0f0ff' )
# Set the starting state of the interface by selecting
# the first state in the list and calling ProcessListbox()
lbox.selection_set(0)
ProcessListbox()
# Enter the main event loop
root.mainloop()