Movatterモバイル変換


[0]ホーム

URL:


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

Commit3024506

Browse files
committed
Update Ch 17 exercise solutions
1 parent8002ec9 commit3024506

9 files changed

+241
-150
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 17.1 - Add GUI Elements with EasyGUI
2+
# Review Exercises
3+
4+
importeasyguiasgui
5+
6+
7+
# Exercise #1
8+
gui.msgbox(msg="Warning!",title="Watch out!",ok_button="I'll be careful")
9+
10+
11+
# Exercise #2
12+
gui.enterbox(msg="What is your name?")

‎ch17-graphical-user-interfaces/6-challenge-return-of-the-poet.pyrenamed to‎ch17-graphical-user-interfaces/10-challenge-return-of-the-poet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 17.6 - Challenge: Return of the Poet
1+
# 17.10 - Challenge: Return of the Poet
22
# Solution to challenge
33

44
# Please note that there are many ways to solve this challenge. The code

‎ch17-graphical-user-interfaces/1-add-gui-elements-with-easy-gui.pyrenamed to‎ch17-graphical-user-interfaces/2-example-app-pdf-page-rotator.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
# 17.1 - Add GUI Elements With EasyGUI
2-
# Review Exercise #4
3-
4-
# NOTE: The first three exercises in this section are instructional
5-
# and do not require solutions to be shown here. For this reason,
6-
# only the solution to the fourth exercise is presented.
1+
# 17.2 - Example App: PDF Page Rotator
2+
# Review Exercise #1
73

84
importeasyguiasgui
95
fromPyPDF2importPdfFileReader,PdfFileWriter

‎ch17-graphical-user-interfaces/2-challenge-use-gui-elements-to-help-a-user-modify-files.pyrenamed to‎ch17-graphical-user-interfaces/3-challenge-use-gui-elements-to-help-a-user-modify-files.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
# 17.2 - Challenge: Use GUI Elements to Help a User Modify Files
1+
# 17.3 - Challenge: Use GUI Elements to Help a User Modify Files
22
# Solution to challenge
33

44
# save part of a PDF based on a user-supplied page range using a GUI
55

66
importeasyguiasgui
77
fromPyPDF2importPdfFileReader,PdfFileWriter
88

9-
#letthe userchoose an inputfile
9+
#1. Askthe userto select a PDFfile to open.
1010
input_file_path=gui.fileopenbox("","Select a PDF to trim...","*.pdf")
11-
ifinput_file_pathisNone:# exit on "Cancel"
12-
exit()
1311

14-
#get the page length of the input file
15-
input_file=PdfFileReader(input_file_path)
16-
total_pages=input_file.getNumPages()
12+
#2. If no PDF file is chosen, exit the program.
13+
ifinput_file_pathisNone:
14+
exit()
1715

18-
#let the user choose a beginning page
16+
#3. Ask for a starting page number.
1917
page_start=gui.enterbox(
2018
"Enter the number of the first page to use:","Where to begin?"
2119
)
22-
ifpage_startisNone:# exit on "Cancel"
20+
21+
# 4. If the user does not enter a starting page number, exit the program.
22+
ifpage_startisNone:
2323
exit()
24-
# check for possible problems and try again:
25-
# 1) input page number isn't a (non-negative) digit
26-
# or 2) input page number is 0
27-
# or 3) page number is greater than total number of pages
24+
25+
# 5. Valid page numbers are positive integers. If the user enters an invalid page number:
26+
# - Warn the user that the entry is invalid
27+
# - Return to step 3.
28+
# This solution also checks that the starting page number is not beyond the last page of the PDF!# 3. Asl for a starting page number.
29+
input_file=PdfFileReader(input_file_path)# Open the PDF
30+
total_pages=input_file.getNumPages()# Get the total number of pages
2831
while (
2932
notpage_start.isdigit()
3033
orpage_start=="0"
@@ -37,17 +40,20 @@
3740
ifpage_startisNone:# exit on "Cancel"
3841
exit()
3942

40-
#let the user choosean ending page
43+
#6. Ask foran ending page number
4144
page_end=gui.enterbox(
4245
"Enter the number of the last page to use:","Where to end?"
4346
)
47+
48+
# 7. If the user does not enter and ending page number, exit the program.
4449
ifpage_endisNone:# exit on "Cancel"
4550
exit()
46-
# check for possible problems and try again:
47-
# 1) input page number isn't a (non-negative) digit
48-
# or 2) input page number is 0
49-
# or 3) input page number is greater than total number of pages
50-
# or 4) input page number for end is less than page number for beginning
51+
52+
# 8. If the user enters an invalid page number:
53+
# - Warn the user that the entry is invalid
54+
# - Return to step 6.
55+
# This solution also check that the ending page number is not less than the starting
56+
# page number, and that it is not greater than the last page in the PDF
5157
while (
5258
notpage_end.isdigit()
5359
orpage_end=="0"
@@ -61,21 +67,29 @@
6167
ifpage_endisNone:# exit on "Cancel"
6268
exit()
6369

64-
#lettheuser choose an output file name
70+
#9. Ask forthelocation to save the extracted pages
6571
output_file_path=gui.filesavebox("","Save the trimmed PDF as...","*.pdf")
72+
73+
# 10. If the user does not select a save location, exit the program.
74+
ifoutput_file_pathisNone:
75+
exit()
76+
77+
# 11. If the chosen save location is the same as the input file path:
78+
# - Warn the user that they can not overwrite the input file.
79+
# - Return the step 9.
6680
whileinput_file_path==output_file_path:# cannot use same file as input
6781
gui.msgbox(
6882
"Cannot overwrite original file!","Please choose another file..."
6983
)
7084
output_file_path=gui.filesavebox(
7185
"","Save the trimmed PDF as...","*.pdf"
7286
)
73-
ifoutput_file_pathisNone:
74-
exit()# exit on "Cancel"
87+
ifoutput_file_pathisNone:
88+
exit()
7589

76-
#read in file, write new file with trimmedpagerange and save the new file
90+
#12. Perform thepageextraction
7791
output_PDF=PdfFileWriter()
78-
# subtract 1 from supplied start page number to get correct index value
92+
7993
forpage_numinrange(int(page_start)-1,int(page_end)):
8094
page=input_file.getPage(page_num)
8195
output_PDF.addPage(page)

‎ch17-graphical-user-interfaces/4-control-layout-with-geometry-managers.py

Lines changed: 0 additions & 119 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 17.4 - Introduction to Tkinter
2+
# Review exercises
3+
4+
importtkinterastk
5+
6+
# Exercise 1
7+
window=tk.Tk()
8+
label=tk.Label(text="GUIs are great!")
9+
label.pack()
10+
window.mainloop()
11+
12+
13+
# Exercise 2
14+
window=tk.Tk()
15+
label=tk.Label(text="Python rocks!")
16+
label.pack()
17+
window.mainloop()
18+
19+
20+
# Exercise 3
21+
window=tk.Tk()
22+
label=tk.Label(text="Engage!")
23+
label.pack()
24+
window.mainloop()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 17.4 - Introduction to Tkinter
2+
# Review exercises
3+
4+
importtkinterastk
5+
6+
7+
# Exercise 1 asks you to recreate all the windows in the chapter.
8+
# The source code for each window can be found in the chapter text.
9+
10+
11+
# Exercise 2
12+
window=tk.Tk()
13+
button=tk.Button(
14+
width=50,height=25,bg="white",fg="blue",text="Click here"
15+
)
16+
button.pack()
17+
window.mainloop()
18+
19+
20+
# Exercise 3
21+
window=tk.Tk()
22+
entry=tk.Entry(width=40)
23+
entry.insert(0,"What is your name?")
24+
entry.pack()
25+
window.mainloop()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp