Skip to content

Commit 21a5712

Browse files
committed
Solved all chapter 9 problems
1 parent 53b6810 commit 21a5712

36 files changed

+1457
-0
lines changed

Chapter 9/Exercise9_10.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from tkinter import Tk, PhotoImage, Label, RIGHT, LEFT
2+
3+
root = Tk()
4+
picture = PhotoImage(file='Rotating_earth_(large).gif')
5+
imagem = Label(master=root,image=picture)
6+
imagem.pack(side=RIGHT)
7+
text = Label(text='Terra \n 23487283742830947 anos')
8+
text.pack(side=LEFT)
9+
10+
root.mainloop()

Chapter 9/Exercise9_11.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from tkinter import Tk, Button, LEFT, RIGHT
2+
from time import strftime, localtime, gmtime
3+
from tkinter.messagebox import showinfo
4+
5+
def greenwich():
6+
'prints Greenwich day and time info'
7+
time = strftime('Day: %d %b %Y\nTime: %H:%M:%S %p\n',
8+
gmtime())
9+
showinfo(message='Greenwich time\n' + time)
10+
11+
def local():
12+
'prints local day and time info'
13+
time = strftime('Day: %d %b %Y\nTime: %H:%M:%S %p\n',
14+
localtime())
15+
showinfo(message='Local time\n' + time)
16+
17+
root = Tk()
18+
19+
# Local time button
20+
buttonl = Button(root,
21+
text='Local time',
22+
command=local)
23+
24+
# Greenwich mean time button
25+
buttong = Button(root,
26+
text='Greenwich time',
27+
command=greenwich)
28+
29+
buttonl.pack(side = LEFT)
30+
buttong.pack(side = RIGHT)
31+
root.mainloop()

Chapter 9/Exercise9_12.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from tkinter import Tk, Label, RAISED, Button
2+
root = Tk()
3+
labels = [['1', '2', '3'], # phone dial label texts
4+
['4', '5', '6'], # organized in a grid
5+
['7', '8', '9'],
6+
['*', '0', '#']]
7+
8+
def imprint(text):
9+
print(text)
10+
for r in range(4): # for every row r = 0, 1, 2, 3
11+
for c in range(3): # for every row c = 0, 1, 2
12+
# create label for row r and column c
13+
button = Button(root,
14+
relief=RAISED, # raised border
15+
padx=10, # make label wide
16+
text=labels[r][c], # label text
17+
command=lambda text=labels[r][c]:imprint(text))
18+
19+
# place label in row r and column c
20+
button.grid(row=r, column=c)
21+
22+
root.mainloop()

Chapter 9/Exercise9_13.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from tkinter import Tk, Canvas, Frame, Button, SUNKEN, LEFT, RIGHT
2+
3+
4+
5+
# event handlers
6+
def up(Event=0):
7+
'move pen up 10 pixels'
8+
global y, canvas # y is modified
9+
canvas.create_line(x, y, x, y-10)
10+
y -= 10
11+
12+
def down(Event=0):
13+
'move pen down 10 pixels'
14+
global y, canvas # y is modified
15+
canvas.create_line(x, y, x, y+10)
16+
y += 10
17+
18+
def left(Event=0):
19+
'move pen left 10 pixels'
20+
global x, canvas # x is modified
21+
canvas.create_line(x, y, x-10, y)
22+
x -= 10
23+
24+
def right(Event=0):
25+
'move pen right 10 pixels'
26+
global x, canvas # x is modified
27+
canvas.create_line(x, y, x+10, y)
28+
x += 10
29+
30+
31+
32+
root = Tk()
33+
34+
35+
# canvas with border of size 100 x 150
36+
canvas = Canvas(root, height=100, width=150,
37+
relief=SUNKEN, borderwidth=3)
38+
canvas.pack(side=LEFT)
39+
40+
# frame to hold the 4 buttons
41+
box = Frame(root)
42+
box.pack(side=RIGHT)
43+
44+
# the 4 button widgets have Frame widget box as their master
45+
button = Button(box, text='up', command=up)
46+
button.grid(row=0, column=0, columnspan=2)
47+
button = Button(box, text='left',command=left)
48+
button.grid(row=1, column=0)
49+
button = Button(box, text='right', command=right)
50+
button.grid(row=1, column=1)
51+
button = Button(box, text='down', command=down)
52+
button.grid(row=2, column=0, columnspan=2)
53+
54+
x, y = 50, 75 # pen position, initially in the middle
55+
56+
57+
root.bind('<KeyPress-Up>', up)
58+
root.bind('<KeyPress-Down>', down)
59+
root.bind('<KeyPress-Left>', left)
60+
root.bind('<KeyPress-Right>', right)
61+
62+
63+
root.mainloop()
64+

Chapter 9/Exercise9_14.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from tkinter import Tk, Canvas, Frame, Button, SUNKEN, LEFT, RIGHT
2+
3+
def move(dx, dy):
4+
global x, y, canvas
5+
canvas.create_line(x, y, x + dx, y + dy)
6+
x += dx
7+
y += dy
8+
9+
root = Tk()
10+
11+
# canvas with border of size 100 x 150
12+
canvas = Canvas(root, height=100, width=150,
13+
relief=SUNKEN, borderwidth=3)
14+
canvas.pack(side=LEFT)
15+
16+
# frame to hold the 4 buttons
17+
box = Frame(root)
18+
box.pack(side=RIGHT)
19+
20+
# the 4 button widgets have Frame widget box as their master
21+
button = Button(box, text='up', command=lambda: move(0, -10))
22+
button.grid(row=0, column=0, columnspan=2)
23+
button = Button(box, text='left',command=lambda: move(-10, 0))
24+
button.grid(row=1, column=0)
25+
button = Button(box, text='right', command=lambda: move(10, 0))
26+
button.grid(row=1, column=1)
27+
button = Button(box, text='down', command=lambda: move(0, 10))
28+
button.grid(row=2, column=0, columnspan=2)
29+
30+
x, y = 50, 75 # pen position, initially in the middle
31+
32+
33+
34+
35+
root.mainloop()
36+

Chapter 9/Exercise9_15.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from tkinter import Tk, Canvas, Frame, Button, SUNKEN, LEFT, RIGHT
2+
3+
lastx = 0
4+
lasty = 0
5+
identity = 0
6+
7+
# event handlers
8+
def up():
9+
'move pen up 10 pixels'
10+
global identity, y, canvas, lasty, lastx # y is modified
11+
identity += 1
12+
canvas.create_line(x, y, x, y-10, tags='last'+str(identity))
13+
y -= 10
14+
lasty = 10
15+
lastx = 0
16+
17+
def down():
18+
'move pen down 10 pixels'
19+
global identity, y, canvas, lasty, lastx # y is modified
20+
identity += 1
21+
canvas.create_line(x, y, x, y+10, tags='last'+str(identity))
22+
y += 10
23+
lastx = 0
24+
lasty = -10
25+
26+
def left():
27+
'move pen left 10 pixels'
28+
global identity, x, canvas, lastx, lasty # x is modified
29+
identity += 1
30+
canvas.create_line(x, y, x-10, y, tags='last'+str(identity))
31+
x -= 10
32+
lasty = 0
33+
lastx = 10
34+
35+
def right():
36+
'move pen right 10 pixels'
37+
global identity, x, canvas, lastx, lasty # x is modified
38+
identity += 1
39+
canvas.create_line(x, y, x+10, y, tags='last'+str(identity))
40+
x += 10
41+
lasty = 0
42+
lastx = -10
43+
44+
def clear():
45+
global canvas
46+
canvas.addtag_all(all)
47+
canvas.delete(all)
48+
49+
50+
def delete():
51+
global identity, canvas, x, y, lastx, lasty
52+
canvas.delete('last'+str(identity))
53+
x += lastx
54+
y += lasty
55+
56+
root = Tk()
57+
58+
# canvas with border of size 100 x 150
59+
canvas = Canvas(root, height=100, width=150,
60+
relief=SUNKEN, borderwidth=3)
61+
canvas.pack(side=LEFT)
62+
63+
# frame to hold the 4 buttons
64+
box = Frame(root)
65+
box.pack(side=RIGHT)
66+
67+
# the 4 button widgets have Frame widget box as their master
68+
button = Button(box, text='up', command=up)
69+
button.grid(row=0, column=0, columnspan=2)
70+
button = Button(box, text='left',command=left)
71+
button.grid(row=1, column=0)
72+
button = Button(box, text='right', command=right)
73+
button.grid(row=1, column=1)
74+
button = Button(box, text='down', command=down)
75+
button.grid(row=2, column=0, columnspan=2)
76+
button = Button(box, text='clear', command=clear)
77+
button.grid(row=3, column=0)
78+
button = Button(box, text='delete', command=delete)
79+
button.grid(row=3, column=1)
80+
81+
x, y = 50, 75 # pen position, initially in the middle
82+
83+
84+
root.mainloop()
85+

Chapter 9/Problem9_16.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from tkinter import *
2+
from tkinter.messagebox import showinfo
3+
4+
def bmi():
5+
global height, weight
6+
7+
altura = float(height.get())
8+
peso = float(weight.get())
9+
height.delete(0, 4)
10+
weight.delete(0, 4)
11+
bmi = peso/altura**2
12+
message = 'Your BMI is:{}'.format(bmi)
13+
showinfo(message=message)
14+
15+
16+
17+
root = Tk()
18+
19+
frameup = Frame(root)
20+
frameup.grid(row=0)
21+
22+
framebutton = Frame(root)
23+
framebutton.grid(row=1)
24+
25+
labelheight = Label(frameup, text = 'Enter your height:')
26+
labelheight.grid(row=0, column=0)
27+
height = Entry(frameup)
28+
height.grid(row=0, column = 1)
29+
labelweight = Label(frameup, text = 'Enter your weight:')
30+
labelweight.grid(row = 1, column = 0)
31+
weight = Entry(frameup)
32+
weight.grid(row = 1, column = 1)
33+
34+
buttoncompute = Button(framebutton, text = 'Compute BMI', command=bmi)
35+
buttoncompute.pack()
36+
37+
root.mainloop()

Chapter 9/Problem9_17.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from tkinter import *
2+
3+
def calculatemortgage():
4+
global entryloan, entryinterest, entryterm, entrymortgage
5+
a = float(entryloan.get())
6+
r = float(entryinterest.get())
7+
t = float(entryterm.get())
8+
c = r/1200
9+
m = a*c*((1 + c)**t)/((1 + c)**t) -1
10+
entrymortgage.delete(0, 10)
11+
entrymortgage.insert(1, str(m))
12+
13+
14+
root = Tk()
15+
16+
frameentry = Frame(root)
17+
frameentry.grid(row = 0)
18+
framebutton = Frame(root)
19+
framebutton.grid(row=1)
20+
21+
labelloan = Label(frameentry, text='Loan amount:')
22+
labelloan.grid(row = 0, column = 0)
23+
entryloan = Entry(frameentry)
24+
entryloan.grid(row = 0, column = 1)
25+
labelinterest = Label(frameentry, text = 'Interest Rate:')
26+
labelinterest.grid(row = 1, column = 0)
27+
entryinterest = Entry(frameentry)
28+
entryinterest.grid(row = 1, column = 1)
29+
labelterm = Label(frameentry, text = 'Loan term:')
30+
labelterm.grid(row = 2, column = 0)
31+
entryterm = Entry(frameentry)
32+
entryterm.grid(row = 2, column = 1)
33+
labelmortgage = Label(frameentry, text = 'Mortgage')
34+
labelmortgage.grid(row = 3, column = 0)
35+
entrymortgage = Entry(frameentry)
36+
entrymortgage.grid(row = 3, column = 1)
37+
38+
buttonmortgage = Button(framebutton, text = 'Compute Mortgage', command = calculatemortgage)
39+
buttonmortgage.pack()

Chapter 9/Problem9_18.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#9.18 Develop a GUI that contains just one Frame widget of size 480 × 640 that has this
2+
#behavior: Every time the user clicks at some location in the frame, the location coordinates
3+
#are printed in the interactive shell.
4+
#>>>
5+
#you clicked at (55, 227)
6+
#you clicked at (426, 600)
7+
#you clicked at (416, 208)
8+
9+
def click(event):
10+
print('you clicked at ({},{})'.format(event.x, event.y))
11+
12+
from tkinter import *
13+
14+
root = Tk()
15+
16+
frame = Frame(root, height = 480, width = 640)
17+
frame.pack()
18+
frame.bind('<Button>', click)
19+
20+
root.mainloop()

Chapter 9/Problem9_19.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from tkinter import *
2+
3+
def imprint(text):
4+
global entrynumber
5+
6+
entrynumber.insert(len(entrynumber.get()), text)
7+
8+
if len(entrynumber.get()) == 3:
9+
entrynumber.insert(3, '-')
10+
elif len(entrynumber.get()) == 7:
11+
entrynumber.insert(7, '-')
12+
13+
root = Tk()
14+
15+
framedisplay = Frame(root)
16+
framedisplay.pack(side=TOP)
17+
framedigits = Frame(root)
18+
framedigits.pack(side=BOTTOM)
19+
20+
entrynumber = Entry(framedisplay)
21+
entrynumber.pack()
22+
23+
labels = [['1', '2', '3'], # phone dial label texts
24+
['4', '5', '6'], # organized in a grid
25+
['7', '8', '9'],
26+
['*', '0', '#']]
27+
28+
29+
for r in range(4): # for every row r = 0, 1, 2, 3
30+
for c in range(3): # for every row c = 0, 1, 2
31+
# create label for row r and column c
32+
button = Button(framedigits,
33+
relief=RAISED, # raised border
34+
padx=10, # make label wide
35+
text=labels[r][c], # label text
36+
command=lambda text=labels[r][c]:imprint(text))
37+
38+
# place label in row r and column c
39+
button.grid(row=r, column=c)
40+
41+
root.mainloop()

0 commit comments

Comments
 (0)