Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 80 additions & 54 deletions country.py
Original file line number Diff line number Diff line change
@@ -1,87 +1,111 @@
from tkinter import *
# country.py

import tkinter
from tkinter import ttk
root = Tk()

# Initialize our country "databases":
# - the list of country codes (a subset anyway)
# - parallel list of country names, same order as the country codes
# - a hash table mapping country code to population
countrycodes = ('ar', 'au', 'be', 'br', 'ca', 'cn', 'dk', 'fi', 'fr', 'gr', 'in', 'it', 'jp', 'mx', 'nl', 'no', 'es', 'se', 'ch')
countrynames = ('Argentina', 'Australia', 'Belgium', 'Brazil', 'Canada', 'China', 'Denmark', \
'Finland', 'France', 'Greece', 'India', 'Italy', 'Japan', 'Mexico', 'Netherlands', 'Norway', 'Spain', \
'Sweden', 'Switzerland')
cnames = StringVar(value=countrynames)
populations = {'ar':41000000, 'au':21179211, 'be':10584534, 'br':185971537, \
'ca':33148682, 'cn':1323128240, 'dk':5457415, 'fi':5302000, 'fr':64102140, 'gr':11147000, \
'in':1131043000, 'it':59206382, 'jp':127718000, 'mx':106535000, 'nl':16402414, \
'no':4738085, 'es':45116894, 'se':9174082, 'ch':7508700}

root = tkinter.Tk()

"""
Initialize our country "databases":
- the list of country codes (a subset anyway);
- parallel list of country names, same order as the country codes;
- a hash table mapping country code to population.
"""
countrycodes = ('ar', 'au', 'be', 'br', 'ca', 'cn', 'dk', 'fi', 'fr', 'gr',
'in', 'it', 'jp', 'mx', 'nl', 'no', 'es', 'se', 'ch')
countrynames = ('Argentina', 'Australia', 'Belgium', 'Brazil', 'Canada',
'China', 'Denmark', 'Finland', 'France', 'Greece', 'India',
'Italy', 'Japan', 'Mexico', 'Netherlands', 'Norway', 'Spain',
'Sweden', 'Switzerland')
cnames = tkinter.StringVar(value=countrynames)
populations = {'ar': 41000000, 'au': 21179211, 'be': 10584534,
'br': 185971537, 'ca': 33148682, 'cn': 1323128240,
'dk': 5457415, 'fi': 5302000, 'fr': 64102140, 'gr': 11147000,
'in': 1131043000, 'it': 59206382, 'jp': 127718000,
'mx': 106535000, 'nl': 16402414, 'no': 4738085, 'es': 45116894,
'se': 9174082, 'ch': 7508700}

# Names of the gifts we can send
gifts = { 'card':'Greeting card', 'flowers':'Flowers', 'nastygram':'Nastygram'}
gifts = {'card': 'Greeting card',
'flowers': 'Flowers',
'nastygram': 'Nastygram'}

# State variables
gift = StringVar()
sentmsg = StringVar()
statusmsg = StringVar()

# Called when the selection in the listbox changes; figure out
# which country is currently selected, and then lookup its country
# code, and from that, its population. Update the status message
# with the new population. As well, clear the message about the
# gift being sent, so it doesn't stick around after we start doing
# other things.
gift = tkinter.StringVar()
sentmsg = tkinter.StringVar()
statusmsg = tkinter.StringVar()

"""
Called when the selection in the listbox changes; figure out
which country is currently selected, and then lookup its country
code, and from that, its population. Update the status message
with the new population. As well, clear the message about the
gift being sent, so it doesn't stick around after we start doing
other things.
"""


def showPopulation(*args):
idxs = lbox.curselection()
if len(idxs)==1:
if len(idxs) == 1:
idx = int(idxs[0])
code = countrycodes[idx]
name = countrynames[idx]
popn = populations[code]
statusmsg.set("The population of %s (%s) is %d" % (name, code, popn))
sentmsg.set('')

# Called when the user double clicks an item in the listbox, presses
# the "Send Gift" button, or presses the Return key. In case the selected
# item is scrolled out of view, make sure it is visible.
#
# Figure out which country is selected, which gift is selected with the
# radiobuttons, "send the gift", and provide feedback that it was sent.

"""
Called when the user double clicks an item in the listbox, presses
the "Send Gift" button, or presses the Return key. In case the selected
item is scrolled out of view, make sure it is visible.

Figure out which country is selected, which gift is selected with the
radiobuttons, "send the gift", and provide feedback that it was sent.
"""


def sendGift(*args):
idxs = lbox.curselection()
if len(idxs)==1:
if len(idxs) == 1:
idx = int(idxs[0])
lbox.see(idx)
name = countrynames[idx]
# Gift sending left as an exercise to the reader
sentmsg.set("Sent %s to leader of %s" % (gifts[gift.get()], name))


# Create and grid the outer content frame
c = ttk.Frame(root, padding=(5, 5, 12, 0))
c.grid(column=0, row=0, sticky=(N,W,E,S))
c.grid(column=0, row=0, sticky=(tkinter.N, tkinter.W, tkinter.E, tkinter.S))
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0,weight=1)
root.grid_rowconfigure(0, weight=1)

# Create the different widgets; note the variables that many
# of them are bound to, as well as the button callback.
# We're using the StringVar() 'cnames', constructed from 'countrynames'
lbox = Listbox(c, listvariable=cnames, height=5)
lbox: tkinter.Listbox = tkinter.Listbox(c, listvariable=cnames, height=5)
lbl = ttk.Label(c, text="Send to country's leader:")
g1 = ttk.Radiobutton(c, text=gifts['card'], variable=gift, value='card')
g2 = ttk.Radiobutton(c, text=gifts['flowers'], variable=gift, value='flowers')
g3 = ttk.Radiobutton(c, text=gifts['nastygram'], variable=gift, value='nastygram')
g1 = ttk.Radiobutton(c, text=gifts['card'],
variable=gift, value='card')
g2 = ttk.Radiobutton(c, text=gifts['flowers'],
variable=gift, value='flowers')
g3 = ttk.Radiobutton(c, text=gifts['nastygram'],
variable=gift, value='nastygram')
send = ttk.Button(c, text='Send Gift', command=sendGift, default='active')
sentlbl = ttk.Label(c, textvariable=sentmsg, anchor='center')
status = ttk.Label(c, textvariable=statusmsg, anchor=W)
status = ttk.Label(c, textvariable=statusmsg, anchor=tkinter.W)

# Grid all the widgets
lbox.grid(column=0, row=0, rowspan=6, sticky=(N,S,E,W))
lbox.grid(column=0, row=0, rowspan=6, sticky=(tkinter.N, tkinter.S, tkinter.E, tkinter.W))
lbl.grid(column=1, row=0, padx=10, pady=5)
g1.grid(column=1, row=1, sticky=W, padx=20)
g2.grid(column=1, row=2, sticky=W, padx=20)
g3.grid(column=1, row=3, sticky=W, padx=20)
send.grid(column=2, row=4, sticky=E)
sentlbl.grid(column=1, row=5, columnspan=2, sticky=N, pady=5, padx=5)
status.grid(column=0, row=6, columnspan=2, sticky=(W,E))
g1.grid(column=1, row=1, sticky=tkinter.W, padx=20)
g2.grid(column=1, row=2, sticky=tkinter.W, padx=20)
g3.grid(column=1, row=3, sticky=tkinter.W, padx=20)
send.grid(column=2, row=4, sticky=tkinter.E)
sentlbl.grid(column=1, row=5, columnspan=2, sticky=tkinter.N, pady=5, padx=5)
status.grid(column=0, row=6, columnspan=2, sticky=(tkinter.W, tkinter.E))
c.grid_columnconfigure(0, weight=1)
c.grid_rowconfigure(5, weight=1)

Expand All @@ -92,13 +116,15 @@ def sendGift(*args):
root.bind('<Return>', sendGift)

# Colorize alternating lines of the listbox
for i in range(0,len(countrynames),2):
for i in range(0, len(countrynames), 2):
lbox.itemconfigure(i, background='#f0f0ff')

# Set the starting state of the interface, including selecting the
# default gift to send, and clearing the messages. Select the first
# country in the list; because the &lt;&lt;ListboxSelect&gt;&gt; event is only
# fired when users makes a change, we explicitly call showPopulation.
"""
Set the starting state of the interface, including selecting the
default gift to send, and clearing the messages. Select the first
country in the list; because the &lt;&lt;ListboxSelect&gt;&gt; event is only
fired when users makes a change, we explicitly call showPopulation.
"""
gift.set('card')
sentmsg.set('')
statusmsg.set('')
Expand Down