Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bed1f5c

Browse files
Solution
1 parent d0fc453 commit bed1f5c

38 files changed

+1669
-0
lines changed

‎CH9/Checkpoints

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#9.1
2+
turtle module is used for drawing geometric shapes. Turtle is easy to use and is an
3+
effective pedagogical tool for introducing the fundamentals of programming to beginners.
4+
However, you cannot use turtle to create graphical user interfaces.
5+
Tkinter enables you to develop GUI programs and is an excellent pedagogical tool for
6+
learning object-oriented programming.
7+
8+
#9.2
9+
window = Tk() # Create a window
10+
11+
#9.3
12+
Tkinter GUI programming is event driven. After the user interface is displayed, the program
13+
waits for user interactions such as mouse clicks and key presses, this is achieved by window.mainloop().
14+
15+
#9.4
16+
The first argument of a widget class is always the
17+
parent container (i.e., the container in which the widget will be placed).
18+
19+
#9.5
20+
For binding method to handler of the widget.
21+
22+
#9.6
23+
lbl = Label(window,text = "welcome",fg ="white",bg ="red")
24+
25+
#9.7
26+
btn = Button(window,text = "OK",fg = "white",bg="red",command= self.processOK)
27+
28+
#9.8
29+
cb = Checkbutton(window,text = "apple", fg = "white",bg="red", variable = self.v1 , command = self.processApple )
30+
31+
#9.9
32+
rb = Radiobutton(window, text = "senior", fg = "white", bg = "red", variable = v1, command = processSenior)
33+
34+
#9.10
35+
ent = Entry(window,fg = "white", bg = "red", variable = v1)
36+
37+
#9.11
38+
msg = Message(window, text = "programming is fun", fg = "white", bg = "red")
39+
40+
#9.12
41+
from tkinter import *
42+
print(LEFT)
43+
print(RIGHT)
44+
print(CENTER)
45+
46+
#9.13
47+
canvas.create_line(34, 50, 50, 90)
48+
49+
#9.14
50+
canvas.create_rectangle(70-50, 70-50, 70+50, 70+50,fill="red")
51+
52+
#9.15
53+
canvas.create_oval(70 – 100, 70 - 50, 70 + 100, 70 + 50, fill = "red")
54+
55+
#9.16
56+
canvas.create_arc(10, 10, 80, 80, start=30, extent = 45)
57+
58+
#9.17
59+
canvas.create_polygon(10, 10, 15, 30, 140, 10, 10, 100)
60+
61+
#9.18
62+
Using width argument to specify pen size in pixels
63+
64+
#9.19
65+
The arrow argument can be used with create_line to draw an arrowed line.
66+
The arrow can appear at the start, end, or both ends of the line with argument value "first", "end", or "both".
67+
68+
#9.20
69+
The activefill argument makes the shape change color when you move the mouse over it.
70+
71+
#9.21
72+
Should identify parameter name, button.pack(side = LEFT)
73+
74+
#9.22
75+
Grid
76+
77+
#9.23
78+
The place manager is not compatible with all computers
79+
80+
#9.24
81+
import tkinter
82+
83+
print(X)
84+
print(Y)
85+
print(BOTH)
86+
print(S)
87+
print(N)
88+
print(E)
89+
print(W)
90+
print(NW)
91+
print(NE)
92+
print(SW)
93+
print(SE)
94+
95+
#9.25
96+
GIF only.
97+
98+
#9.26
99+
should be: image = PhotoImage(file = "image/us.gif")
100+
101+
#9.27
102+
img = PhotoImage(file = "c:\\pybook\\image\canada.gif")
103+
btn = Button(window,image = img)
104+
105+
#9.28
106+
menubar = Menu(window)
107+
window.config(menu = menubar)
108+
109+
#9.29
110+
menu.post(event.x_root, event.y_root)
111+
112+
#9.30
113+
canvas.bind("<Button-1>", self.p())
114+
115+
#9.31
116+
<Bi-Motion>
117+
118+
#9.32
119+
<Double-Button-1>
120+
121+
#9.33
122+
<Tripple-Button-2>
123+
124+
#9.34
125+
event
126+
127+
#9.35
128+
event.x and event.y
129+
130+
#9.36
131+
event.char
132+
133+
#9.37
134+
canvas.sleep(milliseconds)
135+
136+
#9.38
137+
canvas.update()
138+
139+
#9.39
140+
Text, Canvas, Listbox
141+
142+
#9.40
143+
Here is an example to associate a Text with a scrollbar.
144+
145+
text = Text(frame1, width = 40, height = 10, wrap = WORD,
146+
yscrollcommand = scrollbar.set)
147+
text.pack()
148+
scrollbar.config(command = text.yview)
149+
150+
#9.41
151+
tkinter.messagebox.showinfo("showinfo", "Welcome to Python")
152+
153+
#9.42
154+
age = tkinter.simpledialog.askinteger(
155+
"askinteger", "Enter your ages")
156+
print(age)
157+
158+
weight = tkinter.simpledialog.askfloat(
159+
"askfloat", "Enter your weight")
160+
print(weight)
161+
162+
name = tkinter.simpledialog.askstring(
163+
"askstring", "Enter your name")
164+
print(name)
165+

‎CH9/EX9.1.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 9.1 (Move the ball) Write a program that moves a ball in a panel. You should define a
2+
# panel class for displaying the ball and provide the methods for moving the ball
3+
# left, right, up, and down, as shown in Figure 9.22a. Check the boundaries to prevent
4+
# the ball from moving out of sight completely.
5+
from tkinter import *
6+
7+
8+
class Demo:
9+
def __init__(self):
10+
window = Tk()
11+
self.canvas = Canvas(window, width=500, height=500, bg="white")
12+
self.canvas.pack()
13+
self.circle = self.canvas.create_oval(100, 100, 150, 150, fill="red")
14+
frame = Frame(window)
15+
self.var = IntVar()
16+
lBtn = Button(frame, text="Left", command=self.moveLeft).grid(row=1, column=1)
17+
rBtn = Button(frame, text="right", command=self.moveRight).grid(row=1, column=2)
18+
uBtn = Button(frame, text="up", command=self.moveUp).grid(row=1, column=3)
19+
dBtn = Button(frame, text="down", command=self.moveDown).grid(row=1, column=4)
20+
frame.pack()
21+
22+
window.mainloop()
23+
24+
def moveLeft(self):
25+
x1, y1, x2, y2 = self.canvas.coords(self.circle)
26+
if x1 > 0:
27+
self.canvas.move(self.circle, -5, 0)
28+
29+
def moveRight(self):
30+
x1, y1, x2, y2 = self.canvas.coords(self.circle)
31+
if x1 < 450:
32+
self.canvas.move(self.circle, 5, 0)
33+
34+
def moveUp(self):
35+
x1, y1, x2, y2 = self.canvas.coords(self.circle)
36+
if y1 > 0:
37+
self.canvas.move(self.circle, 0, -5)
38+
39+
def moveDown(self):
40+
x1, y1, x2, y2 = self.canvas.coords(self.circle)
41+
if y1 < 450:
42+
self.canvas.move(self.circle, 0, 5)
43+
44+
45+
Demo()

‎CH9/EX9.10.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 9.10 (Display a pie chart) Write a program that uses a pie chart to display the percentages
2+
# of the overall grade represented by the project, quizzes, the midterm exam,
3+
# and the final exam, as shown in Figure 9.26b. Suppose that project is weighted as
4+
# 20 percent of the grade and is displayed in red, quizzes are 10 percent and are displayed
5+
# in blue, the midterm exam is 30 percent and is displayed in green, and the
6+
# final exam is 40 percent and is displayed in orange.
7+
from tkinter import * # Import tkinter
8+
import math
9+
10+
radius = 100
11+
width = 300
12+
height = 300
13+
14+
15+
class MainGUI:
16+
def drawAPie(self, start, extent, color, title):
17+
self.canvas.create_arc(width / 2 - radius, height / 2 - radius,
18+
width / 2 + radius, height / 2 + radius,
19+
start=start, extent=extent, fill=color)
20+
x = width / 2 + radius * math.cos(math.radians(extent / 2 + start))
21+
y = height / 2 - radius * math.sin(math.radians(extent / 2 + start))
22+
self.canvas.create_text(x, y, text=title)
23+
24+
def __init__(self):
25+
window = Tk() # Create a window
26+
window.title("Pie Chart") # Set a title
27+
28+
self.canvas = Canvas(window, bg="white", width=width, height=height)
29+
self.canvas.pack()
30+
31+
self.drawAPie(0, 360 * 0.2, "red", "Project -- 20%")
32+
self.drawAPie(360 * 0.2, 360 * 0.1, "blue", "Quizzes -- 10%")
33+
self.drawAPie(360 * 0.2 + 360 * 0.1, 360 * 0.3, "green", "Midterm -- 30%")
34+
self.drawAPie(360 * 0.2 + 360 * 0.1 + 360 * 0.3, 360 * 0.4, "orange", "Final -- 40%")
35+
36+
window.mainloop() # Create an event loop
37+
38+
39+
MainGUI()

‎CH9/EX9.11.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 9.11 (Display a clock) Write a program that displays a clock to show the current time,
2+
# as shown in Figure 9.27a. To obtain the current time, use the datetime class in
3+
# Supplement II.B.
4+
from datetime import datetime
5+
from tkinter import *
6+
7+
now = datetime.now().strftime("%H:%M:%S")
8+
9+
window = Tk()
10+
c = Canvas(window, width=500, height=500, bg="white")
11+
c.pack()
12+
c.create_oval(50, 50, 450, 450)
13+
c.create_text(250, 50, text="12")
14+
c.create_text(50, 250, text="9")
15+
c.create_text(450, 250, text="3")
16+
c.create_text(250, 450, text="6")
17+
c.create_text(250, 470, text=now)
18+
c.create_line(250,250,100,160,width=2)
19+
c.create_line(250,250,230,360,width="3")
20+
c.create_line(250,250,300,300)
21+
22+
23+
window.mainloop()

‎CH9/EX9.12.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 9.12 (Alternate two messages) Write a program to change, with a left mouse click,
2+
# between two messages displayed on a canvas, "Programming is fun" and "It is fun
3+
# to program," as shown in Figure 9.27b–c.
4+
from tkinter import * # Import tkinter
5+
6+
width = 220
7+
height = 100
8+
9+
10+
class MainGUI:
11+
def __init__(self):
12+
window = Tk() # Create a window
13+
window.title("Rotating Message") # Set a title
14+
15+
self.on = False
16+
self.canvas = Canvas(window, bg="white", width=width, height=height)
17+
self.canvas.pack()
18+
self.canvas.create_text(width / 2, height / 2, text="Programming is fun", tags="text")
19+
20+
# Bind canvas with mouse events
21+
self.canvas.bind("<Button-1>", self.rotate)
22+
23+
window.mainloop() # Create an event loop
24+
25+
def rotate(self, event):
26+
self.canvas.delete("text")
27+
if self.on:
28+
self.canvas.create_text(width / 2, height / 2, text="Programming is fun", tags="text")
29+
else:
30+
self.canvas.create_text(width / 2, height / 2, text="It is fun to program", tags="text")
31+
32+
self.on = not self.on
33+
34+
35+
MainGUI()

‎CH9/EX9.13.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 9.13 (Display the mouse position) Write two programs: one that displays the mouse
2+
# position when the mouse is clicked (see Figure 9.28a–b), and the other displays
3+
# the mouse position when the mouse button is pressed and ceases to display it when
4+
# the mouse button is released.
5+
from tkinter import *
6+
7+
8+
class MainGUI1:
9+
def __init__(self):
10+
window = Tk()
11+
self.canvas = Canvas(window, bg="white", width=300, height=300)
12+
self.canvas.pack()
13+
self.canvas.bind("<Button-1>", self.dispPos)
14+
window.mainloop()
15+
16+
def dispPos(self, event):
17+
self.canvas.delete("txt")
18+
x = event.x
19+
y = event.y
20+
txt = "(" + str(x) + " , " + str(y) + ")"
21+
self.canvas.create_text(x, y, text=txt, tags="txt")
22+
23+
24+
class MainGUI2:
25+
def __init__(self):
26+
window = Tk()
27+
self.canvas = Canvas(window, bg="white", width=300, height=300)
28+
self.canvas.pack()
29+
self.canvas.bind("<Button-1>", self.dispPos)
30+
self.canvas.bind("<ButtonRelease-1>", self.deletPos)
31+
window.mainloop()
32+
33+
def dispPos(self, event):
34+
x = event.x
35+
y = event.y
36+
txt = "(" + str(x) + " , " + str(y) + ")"
37+
self.canvas.create_text(x, y, text=txt, tags="txt")
38+
39+
def deletPos(self, event):
40+
self.canvas.delete("txt")
41+
42+
43+
MainGUI2()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /