├── .gitattributes ├── 978-1-4842-8715-6.jpg ├── Bonuses ├── Bonus 1.pdf └── reference sheet.pdf ├── Chapter 03 ├── Bonus - Data Types.pdf ├── desktop.ini ├── dictionary.py ├── list1.py ├── list2d.py ├── sets.py └── tuple.py ├── Chapter 04 ├── adder.py ├── break.py ├── continue.py ├── desktop.ini ├── forloop.py ├── forloop2.py ├── inchestocm.py ├── multiselection.py ├── selection.py └── whileloop.py ├── Chapter 05 ├── Bonus - File Access Modes.pdf ├── Bonus - File Handling.pdf ├── Bonus - Random File Access.pdf ├── data.txt ├── desktop.ini ├── file.py ├── fileread.py ├── filereadbin.py ├── filereadbin2.py ├── filereadline.py ├── filesearch.py ├── fileseek.py ├── fileseek2.py ├── filewritebin.py └── filewritebin2.py ├── Chapter 06 ├── addsum.py ├── desktop.ini ├── functions.py ├── functionsmain.py ├── module.txt ├── myfunctions.py ├── recursion.py ├── recursion1.py └── recursion3.py ├── Chapter 08 ├── desktop.ini ├── exception.py └── raise.py ├── Chapter 09 ├── Bonus - Classes and Objects.pdf ├── Bonus - Inheritance.pdf ├── Bonus - Polymorphism.pdf ├── class.py ├── desktop.ini ├── inherit.py ├── methodoverride.py ├── poly.py ├── polyclass.py ├── polyclass2.py └── vehicle2.py ├── Chapter 10 ├── buttons.py ├── checkbox.py ├── colorchart.pdf ├── colorchart.py ├── converter.py ├── desktop.ini ├── gridlayout.py ├── image.py ├── labelframe.py ├── labels.py ├── listbox.py ├── logo.png ├── menu.py ├── messagebox.py ├── pentagon.py ├── polygon.py ├── rectangle.py ├── rocket.png ├── textfield.py └── window.py ├── Chapter 11 ├── Bonus - Putting it all together.pdf ├── anim01.py ├── anim02.py ├── anim03.py ├── animfinal.py ├── audio.py ├── background.mp3 ├── bullet.png ├── bullet.wav ├── explosion.png ├── explosion.wav ├── final.py ├── frame1.png ├── frame2.png ├── frame3.png ├── game01.py ├── game02.py ├── game03.py ├── game04.py ├── game05.py ├── game06.py ├── game07.py ├── game08.py ├── game09.py ├── game10.py ├── game11.py ├── rocket.png ├── shapes.py ├── spriteamin.py ├── text.py ├── ufo.png └── window.py ├── Chapter 12 ├── contactus.html ├── contactus.py ├── desktop.ini ├── flask │ ├── app.py │ ├── app01.py │ ├── app02.py │ ├── app03.py │ ├── desktop.ini │ ├── static │ │ ├── desktop.ini │ │ ├── images │ │ │ ├── books.jpg │ │ │ ├── click.jpg │ │ │ ├── desktop.ini │ │ │ ├── fullnavbar.png │ │ │ ├── logo.png │ │ │ └── nav.png │ │ └── styles.css │ └── templates │ │ ├── desktop.ini │ │ ├── index.html │ │ └── index2.html └── script.py ├── Contributing.md ├── LICENSE.txt ├── Projects ├── Invaders │ ├── My Invaders Project - Week01.pdf │ ├── My Invaders Project - Week01.py │ ├── My Invaders Project - Week02.py │ ├── bg.jpg │ ├── bullet.png │ ├── rocket.png │ └── ufo.png └── readme.txt ├── README.md ├── Solutions ├── Ch02 Lab Exercises Solutions.pdf ├── Ch03 Lab Exercises Solutions.pdf ├── Ch04 Lab Exercises Solutions.pdf ├── Ch05 Lab Exercises Solutions.pdf ├── Ch06 Lab Exercises Solutions.pdf ├── Ch07 Lab Exercises Solutions.pdf └── Ch09 Lab Exercises Solutions.pdf └── errata.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /978-1-4842-8715-6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/978-1-4842-8715-6.jpg -------------------------------------------------------------------------------- /Bonuses/Bonus 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Bonuses/Bonus 1.pdf -------------------------------------------------------------------------------- /Bonuses/reference sheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Bonuses/reference sheet.pdf -------------------------------------------------------------------------------- /Chapter 03/Bonus - Data Types.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 03/Bonus - Data Types.pdf -------------------------------------------------------------------------------- /Chapter 03/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 03/dictionary.py: -------------------------------------------------------------------------------- 1 | userData = { 'ID' : 1234 , 2 | 'Surname' : 'Davies' , 3 | 'Forname' : 'Sarah' } 4 | 5 | 6 | print( 'Reference:' , userData['ID'] ) 7 | print( 'Reference:' , userData['Surname'] ) 8 | 9 | userData['Surname'] = 'Daniels' 10 | 11 | print( '\nReference:' , userData['ID'] ) 12 | print( 'Reference:' , userData['Surname'] ) 13 | -------------------------------------------------------------------------------- /Chapter 03/list1.py: -------------------------------------------------------------------------------- 1 | shoppingList = [ 'Bread' , 'Milk' , 'Coffee' , 'Cereal'] 2 | 3 | #print original list 4 | print( 'First Item :' , shoppingList[0] ) 5 | print( 'Second Item :' , shoppingList[1] ) 6 | print( 'Third Item :' , shoppingList[2] ) 7 | print( 'Fourth Item :' , shoppingList[3] ) 8 | 9 | #update fourth item in list 10 | shoppingList[3] = 'Pizza' 11 | 12 | #print updated list 13 | print ('\nUpdated list...') 14 | print( 'First Item :' , shoppingList[0] ) 15 | print( 'First Item :' , shoppingList[1] ) 16 | print( 'First Item :' , shoppingList[2] ) 17 | print( 'First Item :' , shoppingList[3] ) 18 | -------------------------------------------------------------------------------- /Chapter 03/list2d.py: -------------------------------------------------------------------------------- 1 | scoreSheet = [ 2 | [ 21, 8, 17, 4 ], 3 | [ 2, 16, 9, 19 ], 4 | [ 8, 21, 14, 3 ], 5 | [ 3, 18, 15, 5 ] 6 | ] 7 | 8 | #print original list 9 | print( 'Item :' , scoreSheet[1][2] ) 10 | 11 | #change item [1][2] in grid to 21 12 | scoreSheet[1][2] = 21 13 | 14 | #print original list 15 | print( 'Item :' , scoreSheet[1][2] ) 16 | -------------------------------------------------------------------------------- /Chapter 03/sets.py: -------------------------------------------------------------------------------- 1 | animal = { 'Lion' , 'Cheetah' , 'Elephant' } 2 | 3 | print( '\nSet:', animal) 4 | 5 | animal.add('Mouse') 6 | 7 | print( '\nSet:', animal) 8 | -------------------------------------------------------------------------------- /Chapter 03/tuple.py: -------------------------------------------------------------------------------- 1 | Palette = ( 'Red' , 'Orange' , 'Yellow' , 'Green' , 'Blue') 2 | 3 | print ('Colour is: ', Palette[2]) 4 | 5 | print ('\nYour palette: ', Palette) 6 | -------------------------------------------------------------------------------- /Chapter 04/adder.py: -------------------------------------------------------------------------------- 1 | a = input ("Enter first number: ") 2 | b = input ("Enter second number: ") 3 | result = int(a) + int(b) 4 | print (result) 5 | -------------------------------------------------------------------------------- /Chapter 04/break.py: -------------------------------------------------------------------------------- 1 | counter=0 2 | while (counter < 10): 3 | if counter == 5: 4 | break 5 | counter = counter + 1 6 | -------------------------------------------------------------------------------- /Chapter 04/continue.py: -------------------------------------------------------------------------------- 1 | list = [1, 2, 3, 4, 5, 6, 7, 8] 2 | for number in list: 3 | if number % 2 == 0: #if number even 4 | continue 5 | print(number) -------------------------------------------------------------------------------- /Chapter 04/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 04/forloop.py: -------------------------------------------------------------------------------- 1 | fruitlist = ( 'Banana' , 'Orange' , 'Cherry' , 'Lemon' , 'Strawberry', 'Peach') 2 | 3 | for item in fruitlist: 4 | print(item , end = '\n' ) 5 | -------------------------------------------------------------------------------- /Chapter 04/forloop2.py: -------------------------------------------------------------------------------- 1 | for counter in range(1,10): 2 | print(counter , end = '\n' ) 3 | -------------------------------------------------------------------------------- /Chapter 04/inchestocm.py: -------------------------------------------------------------------------------- 1 | centimeter = int(input("Enter length in centimeters:")) 2 | inches = centimeter * 0.393701 3 | print (centimeter, "cm is", inches, "inches") 4 | -------------------------------------------------------------------------------- /Chapter 04/multiselection.py: -------------------------------------------------------------------------------- 1 | mark = int(input("Enter student grade: ")) 2 | if mark >= 70: 3 | print('Grade A') 4 | elif mark >= 60: 5 | print('Grade B') 6 | elif mark >= 50: 7 | print('Grade C') 8 | elif mark >= 40: 9 | print('Grade D') 10 | else: 11 | print('Fail') 12 | -------------------------------------------------------------------------------- /Chapter 04/selection.py: -------------------------------------------------------------------------------- 1 | 2 | mark = int(input("Enter your score")) 3 | if mark > 70: 4 | print("You've passed!") 5 | else: 6 | print("You've failed, try again.") 7 | -------------------------------------------------------------------------------- /Chapter 04/whileloop.py: -------------------------------------------------------------------------------- 1 | temperature = 0 2 | 3 | while temperature <= 26: 4 | print ("Current temperature is:", temperature, "C") 5 | temperature = temperature + 1 6 | -------------------------------------------------------------------------------- /Chapter 05/Bonus - File Access Modes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 05/Bonus - File Access Modes.pdf -------------------------------------------------------------------------------- /Chapter 05/Bonus - File Handling.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 05/Bonus - File Handling.pdf -------------------------------------------------------------------------------- /Chapter 05/Bonus - Random File Access.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 05/Bonus - Random File Access.pdf -------------------------------------------------------------------------------- /Chapter 05/data.txt: -------------------------------------------------------------------------------- 1 | Jack jack@test.com 2 | Pete pete@test.com 3 | Jill jill@site.com 4 | Mike mike@web.com 5 | -------------------------------------------------------------------------------- /Chapter 05/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 05/file.py: -------------------------------------------------------------------------------- 1 | #get some information 2 | username = input('Enter your name: ') 3 | useremail = input('Enter your email: ') 4 | 5 | 6 | #open file for writing 7 | file = open( 'data.txt' , 'a' ) 8 | 9 | 10 | #write the data to the file data.txt 11 | file.writelines (username) 12 | file.writelines (' ') #add space between 13 | file.writelines (useremail) 14 | 15 | 16 | #close the file 17 | file.close() 18 | -------------------------------------------------------------------------------- /Chapter 05/fileread.py: -------------------------------------------------------------------------------- 1 | #open file for reading 2 | file = open ('data.txt' , 'r') 3 | 4 | dataInFile = file.read() 5 | 6 | print (dataInFile) 7 | 8 | #close the file 9 | file.close() 10 | -------------------------------------------------------------------------------- /Chapter 05/filereadbin.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | 3 | file = open("data.dat", "rb") 4 | 5 | data = pickle.load(file) 6 | 7 | print (data) 8 | 9 | file.close() 10 | -------------------------------------------------------------------------------- /Chapter 05/filereadbin2.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | 3 | file = open("data2.dat", "rb") 4 | 5 | data = pickle.load(file) 6 | 7 | print (data) 8 | 9 | file.close() 10 | -------------------------------------------------------------------------------- /Chapter 05/filereadline.py: -------------------------------------------------------------------------------- 1 | #open file for reading 2 | file = open ('data.txt' , 'r') 3 | 4 | dataInFile = file.readline(2) 5 | 6 | print (dataInFile) 7 | 8 | #close the file 9 | file.close() 10 | -------------------------------------------------------------------------------- /Chapter 05/filesearch.py: -------------------------------------------------------------------------------- 1 | #get some information 2 | searchterm = input('Enter your search') 3 | 4 | #open file for reading 5 | file = open ('data.txt' , 'r') 6 | 7 | linenumber = int(1) 8 | 9 | # Check line-by-line if string is in data.txt 10 | for line in file: 11 | if searchterm in line: 12 | print ("String found on line", linenumber) 13 | print (line) 14 | linenumber = linenumber + 1 15 | 16 | #close the file 17 | file.close() 18 | -------------------------------------------------------------------------------- /Chapter 05/fileseek.py: -------------------------------------------------------------------------------- 1 | #open file for reading 2 | file = open ('data.txt' , 'r') 3 | 4 | #move pointer to the 6th character in the file 5 | file.seek (5) 6 | 7 | #read line in file starting from 6th character 8 | dataInFile = file.readline() 9 | 10 | print (dataInFile) 11 | 12 | 13 | #close the file 14 | file.close() 15 | -------------------------------------------------------------------------------- /Chapter 05/fileseek2.py: -------------------------------------------------------------------------------- 1 | #open file for reading 2 | file = open ('data3.txt' , 'w') 3 | 4 | i=0 5 | for i in range(0,8): #range 0 to 7 6 | file.write ("*") 7 | file.write("\n") 8 | 9 | i=0 10 | position=17 11 | for i in range(0,8): 12 | file.write("*") 13 | file.seek(position) 14 | file.write("*") 15 | file.write("\n") 16 | position+=10 17 | 18 | i=0 19 | for i in range(0,8): 20 | file.write ("*") 21 | 22 | 23 | #close the file 24 | file.close() 25 | -------------------------------------------------------------------------------- /Chapter 05/filewritebin.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | 3 | file = open("data.dat", "wb") 4 | 5 | text = "This is text to be written to the file...!" 6 | 7 | pickle.dump(text, file) 8 | 9 | file.close() 10 | -------------------------------------------------------------------------------- /Chapter 05/filewritebin2.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | 3 | file = open("data2.dat", "ab") 4 | 5 | mailing = [] 6 | 7 | 8 | 9 | pickle.dump(mailing, file) 10 | 11 | file.close() 12 | -------------------------------------------------------------------------------- /Chapter 06/addsum.py: -------------------------------------------------------------------------------- 1 | a = int(2) 2 | b = int(3) 3 | 4 | def addsum(firstnum, secondnum): 5 | sum = firstnum + secondnum 6 | return sum 7 | 8 | result = addsum(a, b) 9 | print(result) 10 | -------------------------------------------------------------------------------- /Chapter 06/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 06/functions.py: -------------------------------------------------------------------------------- 1 | def addNum(num1, num2): 2 | return num1 + num2 3 | 4 | 5 | 6 | result = addNum(4, 4) 7 | print (result) -------------------------------------------------------------------------------- /Chapter 06/functionsmain.py: -------------------------------------------------------------------------------- 1 | import myfunctions 2 | 3 | result = myfunctions.addNum(4, 4) 4 | print (result) 5 | -------------------------------------------------------------------------------- /Chapter 06/module.txt: -------------------------------------------------------------------------------- 1 | import turtle 2 | 3 | turtle.shape("turtle") 4 | 5 | turtle.forward(100) 6 | turtle.right(90) 7 | turtle.forward(100) 8 | 9 | turtle.pendown() 10 | turtle.penup() 11 | turtle.pensize() 12 | 13 | turtle.color() 14 | turtle.pencolor() 15 | 16 | turtle.reset() 17 | turtle.clear() 18 | turtle.write() -------------------------------------------------------------------------------- /Chapter 06/myfunctions.py: -------------------------------------------------------------------------------- 1 | def addNum(num1, num2): 2 | return num1 + num2 -------------------------------------------------------------------------------- /Chapter 06/recursion.py: -------------------------------------------------------------------------------- 1 | def fibonacci(n): 2 | if n <= 1: 3 | return (n) 4 | else: 5 | return fibonacci(n-1) + fibonacci(n-2) 6 | 7 | 8 | i=0 9 | for i in range(20): 10 | print (fibonacci(i)) 11 | 12 | -------------------------------------------------------------------------------- /Chapter 06/recursion1.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | if n <= 1: 3 | return 1 4 | else: 5 | f = factorial(n-1) 6 | a = f * n 7 | print (f, "x", n, "=", a) 8 | return a 9 | 10 | 11 | print("\nFactorial of 7 is", factorial(7)) 12 | -------------------------------------------------------------------------------- /Chapter 06/recursion3.py: -------------------------------------------------------------------------------- 1 | def printNum(n): 2 | if (n<=100): 3 | print(n) 4 | printNum(n+1) 5 | 6 | 7 | printNum(1) 8 | -------------------------------------------------------------------------------- /Chapter 08/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 08/exception.py: -------------------------------------------------------------------------------- 1 | try: 2 | file = open("file.txt", "r") 3 | data = file.read() 4 | print (data) 5 | file.close() 6 | except (FileNotFoundError): 7 | print("File not found") 8 | -------------------------------------------------------------------------------- /Chapter 08/raise.py: -------------------------------------------------------------------------------- 1 | number = input("Enter a positive number: ") 2 | 3 | if int(number) < 0: 4 | raise ValueError ("Negative numbers are now allowed.") 5 | -------------------------------------------------------------------------------- /Chapter 09/Bonus - Classes and Objects.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 09/Bonus - Classes and Objects.pdf -------------------------------------------------------------------------------- /Chapter 09/Bonus - Inheritance.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 09/Bonus - Inheritance.pdf -------------------------------------------------------------------------------- /Chapter 09/Bonus - Polymorphism.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 09/Bonus - Polymorphism.pdf -------------------------------------------------------------------------------- /Chapter 09/class.py: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | 3 | class Person : 4 | 5 | #define initialisations 6 | def __init__(self, name, dob, email): 7 | self.name = name 8 | self.dob = dob 9 | self.email = email 10 | 11 | #define class methods 12 | def getAge(self): 13 | currentDate = date.today() 14 | age = currentDate.year - self.dob.year 15 | return age 16 | 17 | 18 | #create an object 19 | person = Person ( 20 | "Sophie", #name 21 | date(1999, 4, 2), #DOB (year, month, day) 22 | "Sophie@mymail.com", #email 23 | ) 24 | 25 | 26 | print(person.name) 27 | print(person.email) 28 | print(person.getAge()) 29 | 30 | -------------------------------------------------------------------------------- /Chapter 09/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 09/inherit.py: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | 3 | class Person : 4 | 5 | #define initialisations 6 | def __init__(self, name, dob, email): 7 | self.name = name 8 | self.dob = dob 9 | self.email = email 10 | 11 | #define class methods 12 | def getAge(self): 13 | currentDate = date.today() 14 | age = currentDate.year - self.dob.year 15 | return age 16 | 17 | 18 | class Student(Person): 19 | def __init__(self, name, dob, email, course, year): 20 | 21 | #inherit the methods and properties from parent class 22 | super().__init__(name, dob, email) 23 | 24 | #add any new attributes for child class 25 | self.course = course 26 | self.year = year 27 | 28 | #add any methods for child class 29 | def getGradYear(self): 30 | return self.year + 4 31 | 32 | 33 | class Staff(Person): 34 | def __init__(self, name, dob, email, salary): 35 | 36 | #inherit the methods and properties from parent class 37 | super().__init__(name, dob, email) 38 | 39 | #add any new attributes for child class 40 | self.salary = salary 41 | 42 | 43 | 44 | 45 | #create an object 46 | student = Student ( 47 | "Sophie", #name 48 | date(1999, 4, 2), #DOB (year, month, day) 49 | "Sophie@mymail.com", #email 50 | "Computing", 51 | 2019 52 | ) 53 | 54 | 55 | print("Name: ", student.name) 56 | print("Email: ", student.email) 57 | print("Age: ", student.getAge()) 58 | print("Course: ", student.course) 59 | print("Graduation year: ", student.getGradYear()) 60 | 61 | 62 | #create an object 63 | lecturer = Staff ( 64 | "John", #name 65 | date(1977, 4, 2), #DOB (year, month, day) 66 | "John@mymail.com", #email 67 | 44000 68 | ) 69 | 70 | print ("\nStaff Member: ", lecturer.name) 71 | print ("Salary: ", "£", lecturer.salary) 72 | 73 | 74 | -------------------------------------------------------------------------------- /Chapter 09/methodoverride.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | 4 | # Parent class 5 | class Polygon: 6 | def getSides(self): 7 | return 0 8 | 9 | def getgetArea(self): 10 | return 0 11 | 12 | 13 | # Child class inherited from Polygon 14 | class Triangle(Polygon): 15 | def getSides(self): 16 | return 3 17 | 18 | def getArea(self, base, height): 19 | area = base * height / 2 20 | return area 21 | 22 | 23 | # Child class inherited from Polygon 24 | class Square(Polygon): 25 | def getSides(self): 26 | return 4 27 | 28 | def getArea(self, length, width): 29 | area = length * width 30 | return area 31 | 32 | 33 | # Child class inherited from Polygon 34 | class Pentagon(Polygon): 35 | def getSides(self): 36 | return 5 37 | 38 | def getArea(self, a): 39 | area = 1 / 4 * math.sqrt(5 * (5 + 2 * math.sqrt(5))) * a ** 2 40 | return area 41 | 42 | 43 | # Create triangle object 44 | tri = Triangle() 45 | 46 | # Call getArea method for triangle 47 | print (tri.getArea(22, 22)) 48 | 49 | # Create pentagon object 50 | pent = Pentagon() 51 | 52 | # Call getArea method for pentagon 53 | print (pent.getArea(22)) 54 | 55 | 56 | -------------------------------------------------------------------------------- /Chapter 09/poly.py: -------------------------------------------------------------------------------- 1 | class Polygon: 2 | def __init__(self, width, height): 3 | self.width = width 4 | self.height = height 5 | 6 | class Triangle(Polygon): 7 | def getArea(self): 8 | return self.width * self.height / 2 9 | 10 | class Square(Polygon): 11 | def getArea(self): 12 | return self.width * self.height 13 | 14 | 15 | square = Square(3,3) 16 | 17 | print (square.getArea()) 18 | 19 | 20 | triangle = Triangle(3,4) 21 | 22 | print (triangle.getArea()) 23 | 24 | -------------------------------------------------------------------------------- /Chapter 09/polyclass.py: -------------------------------------------------------------------------------- 1 | class Polygon: 2 | def __init__(self, width, height): 3 | self.width = width 4 | self.height = height 5 | 6 | class Triangle(Polygon): 7 | def getArea(self): 8 | return self.width * self.height / 2 9 | 10 | class Square(Polygon): 11 | def getArea(self): 12 | return self.width * self.height 13 | 14 | class Circle: 15 | def __init__(self, radius): 16 | self.radius = radius 17 | 18 | def getArea(self): 19 | return 3.14 * self.radius ** 2 20 | 21 | 22 | 23 | square = Square(3,3) 24 | triangle = Triangle(3,4) 25 | circ = Circle(3) 26 | 27 | 28 | print(square.getArea()) 29 | 30 | print(triangle.getArea()) 31 | 32 | print(circ.getArea()) 33 | -------------------------------------------------------------------------------- /Chapter 09/polyclass2.py: -------------------------------------------------------------------------------- 1 | from math import pi 2 | 3 | 4 | class Polygon: 5 | def __init__(self, name): 6 | self.name = name 7 | def getArea(self): 8 | pass 9 | def getName(self): 10 | return "Polygon" 11 | def __str__(self): 12 | return self.name 13 | 14 | 15 | class Square(Polygon): 16 | def __init__(self, length): 17 | super().__init__("Square") 18 | self.length = length 19 | def getArea(self): 20 | return self.length**2 21 | def getName(self): 22 | return "Square" 23 | 24 | 25 | class Circle(Polygon): 26 | def __init__(self, radius): 27 | super().__init__("Circle") 28 | self.radius = radius 29 | def getArea(self): 30 | return pi*self.radius**2 31 | 32 | 33 | 34 | squareObj = Square(7) 35 | circleObj = Circle(7) 36 | 37 | 38 | print(circleObj.name) 39 | 40 | print(squareObj.getArea()) 41 | 42 | print(circleObj.getName()) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Chapter 09/vehicle2.py: -------------------------------------------------------------------------------- 1 | class Vehicle : 2 | def __init__(self, name, speed, mileage): 3 | self.name = name 4 | self.speed = speed 5 | self.mileage = mileage 6 | def getName(self): 7 | return self.name 8 | 9 | 10 | class Taxi (Vehicle): 11 | def __init__ (self, name, speed, mileage): 12 | super().__init__(name, speed, mileage) 13 | 14 | def getFare(self, mileage): 15 | self.fare = mileage * 0.20 16 | 17 | 18 | #get data from user 19 | rn = input("Enter route name: ") 20 | mil = int(input("Enter Mileage: ")) 21 | 22 | 23 | #create object pass data 24 | route = Taxi(rn, 0, mil) 25 | 26 | 27 | #call object method to calculate fare 28 | route.getFare(mil) 29 | 30 | 31 | #print results 32 | 33 | print("\n\nRoute name is %s" % (route.getName())) 34 | 35 | print("Mileage is %d" % (route.mileage)) 36 | 37 | print("The fare is $%f" % (route.fare)) 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Chapter 10/buttons.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #add title to titlebar of window 7 | window.title( 'Window Title' ) 8 | 9 | #window size (width x height + position-x-coord + position-y-coord) 10 | window.geometry("640x480+500+20") 11 | 12 | 13 | #create a button 14 | myButton = Button(window, text="Exit", command=exit) 15 | 16 | #add button to window 17 | myButton.pack() 18 | 19 | 20 | 21 | window.mainloop() 22 | -------------------------------------------------------------------------------- /Chapter 10/checkbox.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from tkinter import messagebox 3 | 4 | 5 | def dialog() : 6 | if box1Checked.get() == 1: 7 | messagebox.showinfo( 'Message Box' , "Red" ) 8 | if box2Checked.get() == 1: 9 | messagebox.showinfo( 'Message Box' , "Green" ) 10 | if box3Checked.get() == 1: 11 | messagebox.showinfo( 'Message Box' , "Blue" ) 12 | 13 | 14 | #create window 15 | window = Tk() 16 | 17 | #add title to titlebar of window 18 | window.title( 'Window Title' ) 19 | 20 | #window size (width x height + position-x-coord + position-y-coord) 21 | window.geometry("640x480+500+20") 22 | 23 | 24 | #create variable to assign 'onvalue' if checked 25 | box1Checked = IntVar() 26 | box2Checked = IntVar() 27 | box3Checked = IntVar() 28 | 29 | #create checkboxes 30 | box1 = Checkbutton(window, text="Red", variable=box1Checked, onvalue=1) 31 | box2 = Checkbutton(window, text="Green", variable=box2Checked, onvalue=1) 32 | box3 = Checkbutton(window, text="Blue", variable=box3Checked, onvalue=1) 33 | 34 | #add checkboxes to window 35 | box1.pack() 36 | box2.pack() 37 | box3.pack() 38 | 39 | 40 | #create a button 41 | myButton = Button(window, text="Click Me", command=dialog) 42 | 43 | #add button to window 44 | myButton.pack(padx=20, pady=20) 45 | 46 | window.mainloop() 47 | -------------------------------------------------------------------------------- /Chapter 10/colorchart.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 10/colorchart.pdf -------------------------------------------------------------------------------- /Chapter 10/colorchart.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | MAX_ROWS = 36 4 | FONT_SIZE = 10 # (pixels) 5 | 6 | COLORS = ['snow', 'ghost white', 'white smoke', 'gainsboro', 'floral white', 'old lace', 7 | 'linen', 'antique white', 'papaya whip', 'blanched almond', 'bisque', 'peach puff', 8 | 'navajo white', 'lemon chiffon', 'mint cream', 'azure', 'alice blue', 'lavender', 9 | 'lavender blush', 'misty rose', 'dark slate gray', 'dim gray', 'slate gray', 10 | 'light slate gray', 'gray', 'light grey', 'midnight blue', 'navy', 'cornflower blue', 'dark slate blue', 11 | 'slate blue', 'medium slate blue', 'light slate blue', 'medium blue', 'royal blue', 'blue', 12 | 'dodger blue', 'deep sky blue', 'sky blue', 'light sky blue', 'steel blue', 'light steel blue', 13 | 'light blue', 'powder blue', 'pale turquoise', 'dark turquoise', 'medium turquoise', 'turquoise', 14 | 'cyan', 'light cyan', 'cadet blue', 'medium aquamarine', 'aquamarine', 'dark green', 'dark olive green', 15 | 'dark sea green', 'sea green', 'medium sea green', 'light sea green', 'pale green', 'spring green', 16 | 'lawn green', 'medium spring green', 'green yellow', 'lime green', 'yellow green', 17 | 'forest green', 'olive drab', 'dark khaki', 'khaki', 'pale goldenrod', 'light goldenrod yellow', 18 | 'light yellow', 'yellow', 'gold', 'light goldenrod', 'goldenrod', 'dark goldenrod', 'rosy brown', 19 | 'indian red', 'saddle brown', 'sandy brown', 20 | 'dark salmon', 'salmon', 'light salmon', 'orange', 'dark orange', 21 | 'coral', 'light coral', 'tomato', 'orange red', 'red', 'hot pink', 'deep pink', 'pink', 'light pink', 22 | 'pale violet red', 'maroon', 'medium violet red', 'violet red', 23 | 'medium orchid', 'dark orchid', 'dark violet', 'blue violet', 'purple', 'medium purple', 24 | 'thistle', 'snow2', 'snow3', 25 | 'snow4', 'seashell2', 'seashell3', 'seashell4', 'AntiqueWhite1', 'AntiqueWhite2', 26 | 'AntiqueWhite3', 'AntiqueWhite4', 'bisque2', 'bisque3', 'bisque4', 'PeachPuff2', 27 | 'PeachPuff3', 'PeachPuff4', 'NavajoWhite2', 'NavajoWhite3', 'NavajoWhite4', 28 | 'LemonChiffon2', 'LemonChiffon3', 'LemonChiffon4', 'cornsilk2', 'cornsilk3', 29 | 'cornsilk4', 'ivory2', 'ivory3', 'ivory4', 'honeydew2', 'honeydew3', 'honeydew4', 30 | 'LavenderBlush2', 'LavenderBlush3', 'LavenderBlush4', 'MistyRose2', 'MistyRose3', 31 | 'MistyRose4', 'azure2', 'azure3', 'azure4', 'SlateBlue1', 'SlateBlue2', 'SlateBlue3', 32 | 'SlateBlue4', 'RoyalBlue1', 'RoyalBlue2', 'RoyalBlue3', 'RoyalBlue4', 'blue2', 'blue4', 33 | 'DodgerBlue2', 'DodgerBlue3', 'DodgerBlue4', 'SteelBlue1', 'SteelBlue2', 34 | 'SteelBlue3', 'SteelBlue4', 'DeepSkyBlue2', 'DeepSkyBlue3', 'DeepSkyBlue4', 35 | 'SkyBlue1', 'SkyBlue2', 'SkyBlue3', 'SkyBlue4', 'LightSkyBlue1', 'LightSkyBlue2', 36 | 'LightSkyBlue3', 'LightSkyBlue4', 'SlateGray1', 'SlateGray2', 'SlateGray3', 37 | 'SlateGray4', 'LightSteelBlue1', 'LightSteelBlue2', 'LightSteelBlue3', 38 | 'LightSteelBlue4', 'LightBlue1', 'LightBlue2', 'LightBlue3', 'LightBlue4', 39 | 'LightCyan2', 'LightCyan3', 'LightCyan4', 'PaleTurquoise1', 'PaleTurquoise2', 40 | 'PaleTurquoise3', 'PaleTurquoise4', 'CadetBlue1', 'CadetBlue2', 'CadetBlue3', 41 | 'CadetBlue4', 'turquoise1', 'turquoise2', 'turquoise3', 'turquoise4', 'cyan2', 'cyan3', 42 | 'cyan4', 'DarkSlateGray1', 'DarkSlateGray2', 'DarkSlateGray3', 'DarkSlateGray4', 43 | 'aquamarine2', 'aquamarine4', 'DarkSeaGreen1', 'DarkSeaGreen2', 'DarkSeaGreen3', 44 | 'DarkSeaGreen4', 'SeaGreen1', 'SeaGreen2', 'SeaGreen3', 'PaleGreen1', 'PaleGreen2', 45 | 'PaleGreen3', 'PaleGreen4', 'SpringGreen2', 'SpringGreen3', 'SpringGreen4', 46 | 'green2', 'green3', 'green4', 'chartreuse2', 'chartreuse3', 'chartreuse4', 47 | 'OliveDrab1', 'OliveDrab2', 'OliveDrab4', 'DarkOliveGreen1', 'DarkOliveGreen2', 48 | 'DarkOliveGreen3', 'DarkOliveGreen4', 'khaki1', 'khaki2', 'khaki3', 'khaki4', 49 | 'LightGoldenrod1', 'LightGoldenrod2', 'LightGoldenrod3', 'LightGoldenrod4', 50 | 'LightYellow2', 'LightYellow3', 'LightYellow4', 'yellow2', 'yellow3', 'yellow4', 51 | 'gold2', 'gold3', 'gold4', 'goldenrod1', 'goldenrod2', 'goldenrod3', 'goldenrod4', 52 | 'DarkGoldenrod1', 'DarkGoldenrod2', 'DarkGoldenrod3', 'DarkGoldenrod4', 53 | 'RosyBrown1', 'RosyBrown2', 'RosyBrown3', 'RosyBrown4', 'IndianRed1', 'IndianRed2', 54 | 'IndianRed3', 'IndianRed4', 'sienna1', 'sienna2', 'sienna3', 'sienna4', 'burlywood1', 55 | 'burlywood2', 'burlywood3', 'burlywood4', 'wheat1', 'wheat2', 'wheat3', 'wheat4', 'tan1', 56 | 'tan2', 'tan4', 'chocolate1', 'chocolate2', 'chocolate3', 'firebrick1', 'firebrick2', 57 | 'firebrick3', 'firebrick4', 'brown1', 'brown2', 'brown3', 'brown4', 'salmon1', 'salmon2', 58 | 'salmon3', 'salmon4', 'LightSalmon2', 'LightSalmon3', 'LightSalmon4', 'orange2', 59 | 'orange3', 'orange4', 'DarkOrange1', 'DarkOrange2', 'DarkOrange3', 'DarkOrange4', 60 | 'coral1', 'coral2', 'coral3', 'coral4', 'tomato2', 'tomato3', 'tomato4', 'OrangeRed2', 61 | 'OrangeRed3', 'OrangeRed4', 'red2', 'red3', 'red4', 'DeepPink2', 'DeepPink3', 'DeepPink4', 62 | 'HotPink1', 'HotPink2', 'HotPink3', 'HotPink4', 'pink1', 'pink2', 'pink3', 'pink4', 63 | 'LightPink1', 'LightPink2', 'LightPink3', 'LightPink4', 'PaleVioletRed1', 64 | 'PaleVioletRed2', 'PaleVioletRed3', 'PaleVioletRed4', 'maroon1', 'maroon2', 65 | 'maroon3', 'maroon4', 'VioletRed1', 'VioletRed2', 'VioletRed3', 'VioletRed4', 66 | 'magenta2', 'magenta3', 'magenta4', 'orchid1', 'orchid2', 'orchid3', 'orchid4', 'plum1', 67 | 'plum2', 'plum3', 'plum4', 'MediumOrchid1', 'MediumOrchid2', 'MediumOrchid3', 68 | 'MediumOrchid4', 'DarkOrchid1', 'DarkOrchid2', 'DarkOrchid3', 'DarkOrchid4', 69 | 'purple1', 'purple2', 'purple3', 'purple4', 'MediumPurple1', 'MediumPurple2', 70 | 'MediumPurple3', 'MediumPurple4', 'thistle1', 'thistle2', 'thistle3', 'thistle4', 71 | 'gray1', 'gray2', 'gray3', 'gray4', 'gray5', 'gray6', 'gray7', 'gray8', 'gray9', 'gray10', 72 | 'gray11', 'gray12', 'gray13', 'gray14', 'gray15', 'gray16', 'gray17', 'gray18', 'gray19', 73 | 'gray20', 'gray21', 'gray22', 'gray23', 'gray24', 'gray25', 'gray26', 'gray27', 'gray28', 74 | 'gray29', 'gray30', 'gray31', 'gray32', 'gray33', 'gray34', 'gray35', 'gray36', 'gray37', 75 | 'gray38', 'gray39', 'gray40', 'gray42', 'gray43', 'gray44', 'gray45', 'gray46', 'gray47', 76 | 'gray48', 'gray49', 'gray50', 'gray51', 'gray52', 'gray53', 'gray54', 'gray55', 'gray56', 77 | 'gray57', 'gray58', 'gray59', 'gray60', 'gray61', 'gray62', 'gray63', 'gray64', 'gray65', 78 | 'gray66', 'gray67', 'gray68', 'gray69', 'gray70', 'gray71', 'gray72', 'gray73', 'gray74', 79 | 'gray75', 'gray76', 'gray77', 'gray78', 'gray79', 'gray80', 'gray81', 'gray82', 'gray83', 80 | 'gray84', 'gray85', 'gray86', 'gray87', 'gray88', 'gray89', 'gray90', 'gray91', 'gray92', 81 | 'gray93', 'gray94', 'gray95', 'gray97', 'gray98', 'gray99'] 82 | 83 | root = Tk() 84 | root.title("Named colour chart") 85 | row = 0 86 | col = 0 87 | for color in COLORS: 88 | e = Label(root, text=color, background=color, 89 | font=(None, -FONT_SIZE)) 90 | e.grid(row=row, column=col, sticky=E+W) 91 | row += 1 92 | if (row > 36): 93 | row = 0 94 | col += 1 95 | 96 | root.mainloop() -------------------------------------------------------------------------------- /Chapter 10/converter.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from tkinter.ttk import * 3 | 4 | def convert(): 5 | if conversions.current() == 0: 6 | n = float(userInput.get()) * 0.39 7 | textLabel = Label(window, text=n) 8 | textLabel.grid( row = 3, column = 4, padx = 10, pady=5) 9 | elif conversions.current() == 1: 10 | n = float(userInput.get()) * 2.54 11 | textLabel = Label(window, text=n) 12 | textLabel.grid( row = 3, column = 4, padx = 10, pady=5) 13 | elif conversions.current() == 2: 14 | n = float(userInput.get()) * 0.62 15 | textLabel = Label(window, text=n) 16 | textLabel.grid( row = 3, column = 4, padx = 10, pady=5) 17 | elif conversions.current() == 3: 18 | n = float(userInput.get()) * 1.60 19 | textLabel = Label(window, text=n) 20 | textLabel.grid( row = 3, column = 4, padx = 10, pady=5) 21 | 22 | #create window 23 | window = Tk() 24 | 25 | #add title to titlebar of window 26 | window.title( 'Unit Converter' ) 27 | 28 | #window size (width x height + position-x-coord + position-y-coord) 29 | window.geometry("525x180+500+20") 30 | 31 | #load image and add to imgLbl label 32 | img = PhotoImage(file="logo.png") 33 | imgLbl = Label(window, image=img) 34 | 35 | #add image to grid 36 | imgLbl.grid( row = 1, column = 1, padx = 10, pady=10, columnspan=2, rowspan=3) 37 | 38 | #create a label 39 | textLabel = Label(window, text="Convert:") 40 | 41 | #add label to grid 42 | textLabel.grid( row = 1, column = 3, padx = 10, pady=10) 43 | 44 | #create drop down menu 45 | conversions = Combobox(window, values=[ 46 | "Cm to Inch", 47 | "Inch to Cm", 48 | "Km to Miles", 49 | "Miles to Km"]) 50 | 51 | #set combobox default selection 52 | conversions.current(0) 53 | 54 | #add combo box to grid 55 | conversions.grid( row = 1, column = 4, padx = 10, pady=10) 56 | 57 | #create a text field 58 | userInput = Entry( window ) 59 | 60 | #add text field to grid 61 | userInput.grid( row = 2, column = 4, padx = 10, pady=10) 62 | 63 | #add label for result 64 | textLabel = Label(window, text="") 65 | 66 | #add result label to grid 67 | textLabel.grid( row = 3, column = 4, padx = 10, pady=5) 68 | 69 | #create a button 70 | myButton = Button(window, text="OK", command=convert) 71 | 72 | #add button to grid 73 | myButton.grid( row = 2, column = 6, padx = 10, pady=10) 74 | 75 | window.mainloop() 76 | -------------------------------------------------------------------------------- /Chapter 10/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 10/gridlayout.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #add title to titlebar of window 7 | window.title( 'Window Title' ) 8 | 9 | #window size (width x height + position-x-coord + position-y-coord) 10 | window.geometry("250x100+500+20") 11 | 12 | #create a label 13 | textLabel = Label(window, text="Enter Name:") 14 | 15 | #add label to grid 16 | textLabel.grid( row = 1, column = 1, padx = 10, pady=10) 17 | 18 | #create a text field 19 | userInput = Entry( window ) 20 | 21 | #add text field to grid 22 | userInput.grid( row = 1, column = 2, padx = 10, pady=10) 23 | 24 | 25 | #create a button 26 | myButton = Button(window, text="OK", command=exit) 27 | 28 | #add button to grid 29 | myButton.grid( row = 2, column = 2, padx = 10, pady=10) 30 | 31 | window.mainloop() 32 | -------------------------------------------------------------------------------- /Chapter 10/image.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #add title to titlebar of window 7 | window.title( 'Window Title' ) 8 | 9 | #window size (width x height + position-x-coord + position-y-coord) 10 | window.geometry("640x480+500+20") 11 | 12 | myCanvas = Canvas(window, bg="blue", height=300, width=300) 13 | 14 | #load image 15 | img = PhotoImage(file="rocket.png") 16 | 17 | #resize to a 6th of the original size 18 | img = PhotoImage.subsample(img, x=6, y=6) 19 | 20 | #paste image on canvas at co-ords (x=20, y=60) 21 | myCanvas.create_image(20,60, anchor=NW, image=img) 22 | 23 | myCanvas.pack() 24 | window.mainloop() 25 | -------------------------------------------------------------------------------- /Chapter 10/labelframe.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #add title to titlebar of window 7 | window.title( 'Window Title' ) 8 | 9 | #window size (width x height + position-x-coord + position-y-coord) 10 | window.geometry("640x480+500+20") 11 | 12 | 13 | #create labelframe group 14 | group1 = LabelFrame(window, text="Contact Details", padx=5, pady=5) 15 | 16 | #create widgets and add to group 17 | textLabel = Label(group1, text="Name: ") 18 | name = Entry(group1) 19 | 20 | 21 | #add widgets to your window 22 | group1.pack(padx=10, pady=10) 23 | textLabel.pack(side=LEFT) 24 | name.pack(side=RIGHT) 25 | 26 | window.mainloop() 27 | -------------------------------------------------------------------------------- /Chapter 10/labels.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #add title to titlebar of window 7 | window.title( 'Window Title' ) 8 | 9 | #window size (width x height + position-x-coord + position-y-coord) 10 | window.geometry("640x480+500+20") 11 | 12 | textLabel = Label(window, text="Enter Name:") 13 | 14 | textLabel.pack() 15 | 16 | window.mainloop() 17 | -------------------------------------------------------------------------------- /Chapter 10/listbox.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from tkinter import messagebox 3 | 4 | 5 | def dialog() : 6 | selectedItem = list.curselection() 7 | messagebox.showinfo( 'Message Box' , list.get(selectedItem) ) 8 | 9 | 10 | #create window 11 | window = Tk() 12 | 13 | #add title to titlebar of window 14 | window.title( 'Window Title' ) 15 | 16 | #window size (width x height + position-x-coord + position-y-coord) 17 | window.geometry("640x480+500+20") 18 | 19 | 20 | #create a list box 21 | list = Listbox(window) 22 | list.insert(1, 'Item One') 23 | list.insert(2, 'Item Two') 24 | list.insert(3, 'Item Three') 25 | list.insert(4, 'Item Four') 26 | 27 | 28 | #create a button 29 | myButton = Button(window, text="Click Me", command=dialog) 30 | 31 | 32 | #add listbox to window 33 | list.pack(padx=20, pady=20) 34 | 35 | 36 | #add button to window 37 | myButton.pack(padx=20) 38 | 39 | window.mainloop() 40 | -------------------------------------------------------------------------------- /Chapter 10/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 10/logo.png -------------------------------------------------------------------------------- /Chapter 10/menu.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #create menubar 7 | menubar = Menu(window) 8 | 9 | #create menu 10 | filemenu = Menu(menubar, tearoff=0) 11 | menubar.add_cascade(label="File", menu=filemenu) 12 | 13 | #create menu commands 14 | filemenu.add_command(label="New") 15 | 16 | #add menu to window 17 | window.config(menu=menubar) 18 | 19 | 20 | window.mainloop() 21 | -------------------------------------------------------------------------------- /Chapter 10/messagebox.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from tkinter import messagebox 3 | 4 | 5 | 6 | def dialog() : 7 | response = messagebox.askquestion( 'Message Box' , 'Do you want to proceed?' ) 8 | if response == 'yes' : 9 | messagebox.showinfo( 'Message Box', 'Proceeding...' ) 10 | else : 11 | messagebox.showwarning( 'Message Box', 'Canceling...' ) 12 | 13 | 14 | 15 | #create window 16 | window = Tk() 17 | 18 | #add title to titlebar of window 19 | window.title( 'Window Title' ) 20 | 21 | #window size (width x height + position-x-coord + position-y-coord) 22 | window.geometry("640x480+500+20") 23 | 24 | 25 | 26 | #create a button 27 | myButton = Button(window, text="Click Me", command=dialog) 28 | 29 | #add button to window 30 | myButton.pack() 31 | 32 | 33 | 34 | window.mainloop() 35 | -------------------------------------------------------------------------------- /Chapter 10/pentagon.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #add title to titlebar of window 7 | window.title( 'Window Title' ) 8 | 9 | #window size (width x height + position-x-coord + position-y-coord) 10 | window.geometry("640x480+500+20") 11 | 12 | myCanvas = Canvas(window, bg="blue", height=300, width=300) 13 | 14 | pent = myCanvas.create_polygon(100,150, 52,185, 71,240, 129,240, 148,185, fill="lime green") 15 | 16 | 17 | myCanvas.pack() 18 | window.mainloop() 19 | -------------------------------------------------------------------------------- /Chapter 10/polygon.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #add title to titlebar of window 7 | window.title( 'Window Title' ) 8 | 9 | #window size (width x height + position-x-coord + position-y-coord) 10 | window.geometry("640x480+500+20") 11 | 12 | myCanvas = Canvas(window, bg="blue", height=300, width=300) 13 | 14 | tri = myCanvas.create_polygon(100,150, 57,225, 143,225, fill="lime green") 15 | 16 | 17 | myCanvas.pack() 18 | window.mainloop() 19 | -------------------------------------------------------------------------------- /Chapter 10/rectangle.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #add title to titlebar of window 7 | window.title( 'Window Title' ) 8 | 9 | #window size (width x height + position-x-coord + position-y-coord) 10 | window.geometry("640x480+500+20") 11 | 12 | myCanvas = Canvas(window, bg="blue", height=300, width=300) 13 | 14 | rect = myCanvas.create_rectangle(100, 100, 25, 50, fill="yellow") 15 | 16 | 17 | 18 | myCanvas.pack() 19 | window.mainloop() 20 | -------------------------------------------------------------------------------- /Chapter 10/rocket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 10/rocket.png -------------------------------------------------------------------------------- /Chapter 10/textfield.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from tkinter import messagebox 3 | 4 | 5 | def dialog() : 6 | messagebox.showinfo( 'Message Box' , userInput.get() ) 7 | 8 | 9 | #create window 10 | window = Tk() 11 | 12 | #add title to titlebar of window 13 | window.title( 'Window Title' ) 14 | 15 | #window size (width x height + position-x-coord + position-y-coord) 16 | window.geometry("640x480+500+20") 17 | 18 | 19 | #create a text field 20 | userInput = Entry( window ) 21 | 22 | 23 | #create a button 24 | myButton = Button(window, text="Click Me", command=dialog) 25 | 26 | #add button to window 27 | myButton.pack(padx=20, pady=20) 28 | 29 | 30 | #add text field to window 31 | userInput.pack() 32 | 33 | 34 | window.mainloop() 35 | -------------------------------------------------------------------------------- /Chapter 10/window.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | #create window 4 | window = Tk() 5 | 6 | #add title to titlebar of window 7 | window.title( 'Window Title' ) 8 | 9 | #window size (width x height + position-x-coord + position-y-coord) 10 | window.geometry("640x480+500+20") 11 | 12 | window.mainloop() 13 | -------------------------------------------------------------------------------- /Chapter 11/Bonus - Putting it all together.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/Bonus - Putting it all together.pdf -------------------------------------------------------------------------------- /Chapter 11/anim01.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | #initialise running state to 1 for game loop 9 | running = 1 10 | 11 | 12 | #set horizontal by vertical 13 | speed = [1, 0] 14 | 15 | 16 | #load ufo image 17 | ufo = pygame.image.load('ufo.png') 18 | 19 | #assign image onto rectangle so we can manipulate it 20 | ufo_rect = ufo.get_rect() 21 | 22 | 23 | #game loop 24 | while running: 25 | 26 | 27 | # move ufo 28 | ufo_rect.move_ip(speed) 29 | 30 | 31 | gamewindow.fill((0,0,0)) #clear screen 32 | 33 | gamewindow.blit(ufo,ufo_rect) #redraw ufo in new position 34 | 35 | pygame.display.update() #update screen 36 | 37 | 38 | pygame.quit() 39 | -------------------------------------------------------------------------------- /Chapter 11/anim02.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | #initialise game clock 9 | clock = pygame.time.Clock() 10 | 11 | #initialise running state to 1 for game loop 12 | running = 1 13 | 14 | 15 | #set horizontal by vertical list 16 | speed = [10, 0] 17 | 18 | 19 | #load ufo image 20 | ufo = pygame.image.load('ufo.png') 21 | 22 | #assign image to rectangle so we can manipulate its position 23 | ufo_rect = ufo.get_rect() 24 | 25 | 26 | #game loop 27 | while running: 28 | 29 | #execute loop at 25 frames per second 30 | clock.tick(25) 31 | 32 | 33 | # move ufo 34 | ufo_rect.move_ip(speed) 35 | 36 | 37 | gamewindow.fill((0,0,0)) #clear screen 38 | 39 | gamewindow.blit(ufo,ufo_rect) #redraw ufo in new position 40 | 41 | pygame.display.update() #update screen 42 | 43 | 44 | pygame.quit() 45 | -------------------------------------------------------------------------------- /Chapter 11/anim03.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | #initialise game clock 9 | clock = pygame.time.Clock() 10 | 11 | #initialise running state to 1 for game loop 12 | running = 1 13 | 14 | #set horizontal and vertical offset 15 | speed = [10, 10] 16 | 17 | #load ufo image 18 | ufo = pygame.image.load('ufo.png') 19 | 20 | #assign image to rectangle so we can manipulate its position 21 | ufo_rect = ufo.get_rect() 22 | 23 | 24 | #game loop 25 | while running: 26 | 27 | #execute loop at 30 frames per second 28 | clock.tick(30) 29 | 30 | # move ufo by given offset (x,y) 31 | ufo_rect.move_ip(speed) #ufo_rect.move_ip (x, y) 32 | 33 | 34 | #bounce the ufo off the 4 edges 35 | #if ufo goes off left edge x reverse direction 36 | if ufo_rect.left < 0: 37 | speed[0] = -speed[0] 38 | 39 | #if ufo goes off right edge reverse x direction 40 | if ufo_rect.right > 640: 41 | speed[0] = -speed[0] 42 | 43 | #if ufo goes off top edge reverse y direction 44 | if ufo_rect.top < 0: 45 | speed[1] = -speed[1] 46 | 47 | #if ufo goes off bottom edge reverse y direction 48 | if ufo_rect.bottom > 480: 49 | speed[1] = -speed[1] 50 | 51 | 52 | gamewindow.fill((0,0,0)) #clear screen 53 | 54 | gamewindow.blit(ufo,ufo_rect) #redraw ufo in new position 55 | 56 | pygame.display.update() #update screen 57 | 58 | 59 | pygame.quit() 60 | -------------------------------------------------------------------------------- /Chapter 11/animfinal.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | #load rocket image 9 | sprite = pygame.image.load('rocket.png') 10 | 11 | #load ufo image 12 | ufo = pygame.image.load('ufo.png') 13 | 14 | #initialise running state to 1 for game loop 15 | running = 1 16 | 17 | # set initial position for image 18 | x = 250 19 | y = 280 20 | ux=10 21 | uy=50 22 | 23 | #create our game clock 24 | clock = pygame.time.Clock() 25 | 26 | #turn on key repeat 27 | pygame.key.set_repeat(1, 25) 28 | 29 | 30 | #game loop 31 | while running: 32 | #execute loop at 25 frames per second 33 | clock.tick(25) 34 | 35 | #move ufo 36 | if ux > 600: 37 | ux = 0 38 | ux+=3 39 | 40 | 41 | for event in pygame.event.get(): 42 | if event.type == pygame.QUIT: 43 | running = 0 #close 44 | elif event.type == pygame.KEYDOWN: 45 | if event.key == pygame.K_LEFT: 46 | x = x - 10 #shift image left 10 pixels 47 | elif event.key == pygame.K_RIGHT: 48 | x = x + 10 #shift image right 10 pixels 49 | 50 | gamewindow.fill((0,0,0)) #clear screen 51 | gamewindow.blit(ufo,(ux,uy)) #redraw ufo in new position 52 | gamewindow.blit(sprite, (x,y)) #redraw sprite in new position 53 | pygame.display.update() #update screen 54 | 55 | 56 | pygame.quit() 57 | -------------------------------------------------------------------------------- /Chapter 11/audio.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | #load image and assign to 'sprite' 9 | sprite = pygame.image.load('rocket.png') 10 | 11 | #load sound file 12 | bulletSound = pygame.mixer.Sound('background.mp3') 13 | 14 | #play sound file 15 | bulletSound.play() 16 | 17 | running = 1 18 | x=250 19 | y=280 20 | 21 | fontColor=(0,150,250) 22 | 23 | while running: 24 | #add image to game window 25 | gamewindow.blit(sprite, (x,y)) 26 | 27 | gameFont = pygame.font.Font('C:\Windows\Fonts\helvetica.ttf', 32) 28 | 29 | gameText = gameFont.render('Alien Invader', True, 'White') 30 | 31 | gamewindow.blit(gameText,(200,44)) 32 | 33 | #update the game window display 34 | pygame.display.update() 35 | 36 | 37 | pygame.quit() 38 | -------------------------------------------------------------------------------- /Chapter 11/background.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/background.mp3 -------------------------------------------------------------------------------- /Chapter 11/bullet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/bullet.png -------------------------------------------------------------------------------- /Chapter 11/bullet.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/bullet.wav -------------------------------------------------------------------------------- /Chapter 11/explosion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/explosion.png -------------------------------------------------------------------------------- /Chapter 11/explosion.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/explosion.wav -------------------------------------------------------------------------------- /Chapter 11/final.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import math 3 | import random 4 | 5 | pygame.init() 6 | 7 | gamewindow = pygame.display.set_mode((800,600)) 8 | pygame.display.set_caption("Game Window") 9 | 10 | #load rocket image 11 | sprite = pygame.image.load('rocket.png') 12 | 13 | #load ufo image 14 | ufo = pygame.image.load('ufo.png') 15 | 16 | #load bullet image 17 | bulletImage = pygame.image.load('bullet.png') 18 | 19 | #load explosion image 20 | exp = pygame.image.load('explosion.png') 21 | 22 | #initialise running state to 1 for game loop 23 | running = 1 24 | 25 | #set horizontal and vertical offset ufo moves 5 pixels at a time 26 | speed = [5, 5] 27 | 28 | # set initial position for rocket image 29 | x = 250 30 | y = 400 31 | 32 | #ufo direction 33 | ufo_X_dir = 0 34 | ufo_Y_dir = 0 35 | 36 | #ufo position 37 | ufo_X_pos = 0 38 | ufo_Y_pos = 0 39 | 40 | #initialise bullets 41 | bullet_X = 0 42 | bullet_Y = y 43 | bullet_Xchange = 0 44 | bullet_Ychange = 3 45 | bullet_state = "nofire" 46 | 47 | #create our game clock 48 | clock = pygame.time.Clock() 49 | 50 | #turn on key repeat 51 | pygame.key.set_repeat(1, 25) 52 | 53 | #assign image to rectangle so we can manipulate its position 54 | ufo_rect = ufo.get_rect() 55 | 56 | #chech for a collision between bullet and ufo 57 | def isCollision(ufo_X_pos, ufo_Y_pos, bullet_X, bullet_Y): 58 | distance = math.sqrt(math.pow(ufo_X_pos - bullet_X, 2) + (math.pow(ufo_Y_pos - bullet_Y, 2))) 59 | if distance < 35: 60 | return True 61 | else: 62 | return False 63 | 64 | #game loop 65 | while running: 66 | #execute loop at 25 frames per second 67 | clock.tick(25) 68 | 69 | # move ufo by given offset (x,y) 70 | ufo_rect.move_ip(speed) #ufo_rect.move_ip (x, y) 71 | 72 | #bounce the ufo off the 4 edges 73 | #if ufo goes off left edge x reverse direction 74 | if ufo_rect.left < 0: 75 | speed[0] = -speed[0] 76 | ufo_X_dir=ufo_X_dir-1 77 | 78 | #if ufo goes off right edge reverse x direction 79 | if ufo_rect.right > 800: 80 | speed[0] = -speed[0] 81 | ufo_X_dir=ufo_X_dir+1 82 | 83 | #if ufo goes off top edge reverse y direction 84 | if ufo_rect.top < 0: 85 | speed[1] = -speed[1] 86 | ufo_Y_dir=ufo_Y_dir-1 87 | 88 | #if ufo goes off bottom edge reverse y direction 89 | if ufo_rect.bottom > 600: 90 | speed[1] = -speed[1] 91 | ufo_Y_dir=ufo_Y_dir+1 92 | 93 | #set position of UFO (center of the ufo rectangle 94 | ufo_X_pos = ufo_rect.centerx 95 | ufo_Y_pos = ufo_rect.centery 96 | 97 | #clear screen for next frame 98 | gamewindow.fill((0,0,0)) 99 | 100 | # Collision 101 | collision = isCollision(ufo_X_pos, ufo_Y_pos, bullet_X, bullet_Y) 102 | if collision: 103 | bullet_Y = 380 104 | bullet_state = "nofire" 105 | gamewindow.blit(exp, (ufo_X_pos, ufo_Y_pos)) #show explosion image 106 | #place ufo back on screen in random position 107 | ufo_rect.centerx = random.randint(0, 736) 108 | ufo_rect.centery = random.randint(50, 300) 109 | 110 | #keypress event handler 111 | for event in pygame.event.get(): 112 | if event.type == pygame.QUIT: 113 | running = 0 #close 114 | elif event.type == pygame.KEYDOWN: 115 | if event.key == pygame.K_LEFT: 116 | x = x - 10 #shift image left 10 pixels 117 | elif event.key == pygame.K_RIGHT: 118 | x = x + 10 #shift image right 10 pixels 119 | elif event.key == pygame.K_SPACE: # file bullet 120 | if bullet_state == "nofire": 121 | bullet_X = x + 40 #move bullet to rocket position 122 | gamewindow.blit(bulletImage, (bullet_X, bullet_Y)) 123 | bullet_state = "fire" 124 | 125 | 126 | # move bullet after user presses space 127 | if bullet_Y <= 0: 128 | bullet_Y = y 129 | bullet_state = "nofire" 130 | if bullet_state == "fire": 131 | gamewindow.blit(bulletImage, (bullet_X, bullet_Y)) 132 | bullet_state = "fire" 133 | bullet_Y -= bullet_Ychange 134 | 135 | 136 | gamewindow.blit(ufo,ufo_rect) #redraw ufo in new position 137 | gamewindow.blit(sprite, (x,y)) #redraw rocket sprite in new position 138 | pygame.display.update() #update screen 139 | 140 | 141 | pygame.quit() 142 | -------------------------------------------------------------------------------- /Chapter 11/frame1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/frame1.png -------------------------------------------------------------------------------- /Chapter 11/frame2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/frame2.png -------------------------------------------------------------------------------- /Chapter 11/frame3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/frame3.png -------------------------------------------------------------------------------- /Chapter 11/game01.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | #create game window 6 | gamewindow = pygame.display.set_mode((640,480)) 7 | 8 | #set window title 9 | pygame.display.set_caption("Game Window") 10 | 11 | 12 | 13 | 14 | pygame.quit() 15 | -------------------------------------------------------------------------------- /Chapter 11/game02.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | clock = pygame.time.Clock() 9 | 10 | 11 | pygame.quit() 12 | -------------------------------------------------------------------------------- /Chapter 11/game03.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | clock = pygame.time.Clock() 9 | 10 | sprite = pygame.image.load('rocket.png') 11 | 12 | 13 | 14 | 15 | pygame.quit() 16 | -------------------------------------------------------------------------------- /Chapter 11/game04.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | 9 | #load image and assign to 'sprite' 10 | sprite = pygame.image.load('rocket.png') 11 | 12 | #add image to game window 13 | gamewindow.blit(sprite, (50,55)) 14 | 15 | #update the game window display 16 | pygame.display.update() 17 | 18 | 19 | pygame.quit() 20 | -------------------------------------------------------------------------------- /Chapter 11/game05.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | #load image and assign to 'sprite' 9 | sprite = pygame.image.load('rocket.png') 10 | 11 | running = 1 12 | x=250 13 | y=280 14 | 15 | while running: 16 | #add image to game window 17 | gamewindow.blit(sprite, (x,y)) 18 | 19 | #update the game window display 20 | pygame.display.update() 21 | 22 | 23 | pygame.quit() 24 | -------------------------------------------------------------------------------- /Chapter 11/game06.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | sprite = pygame.image.load('rocket.png') 9 | 10 | 11 | #initialize our variables 12 | running = 1 13 | x=250 14 | y=280 15 | 16 | while running: 17 | #add image to game window 18 | gamewindow.blit(sprite, (x,y)) 19 | 20 | #update the game window display 21 | pygame.display.update() 22 | 23 | 24 | pygame.quit() 25 | -------------------------------------------------------------------------------- /Chapter 11/game07.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | sprite = pygame.image.load('rocket.png') 9 | 10 | 11 | #initialize our variables 12 | running = 1 13 | x=250 14 | y=280 15 | 16 | while running: 17 | for event in pygame.event.get(): 18 | 19 | 20 | gamewindow.blit(sprite, (x,y)) 21 | pygame.display.update() 22 | 23 | 24 | pygame.quit() 25 | -------------------------------------------------------------------------------- /Chapter 11/game08.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | sprite = pygame.image.load('rocket.png') 9 | 10 | 11 | #initialize our variables 12 | running = 1 13 | x=250 14 | y=280 15 | 16 | 17 | while running: 18 | 19 | for event in pygame.event.get(): 20 | if event.type == pygame.KEYDOWN: 21 | if event.key == pygame.K_LEFT: 22 | x = x - 10 #shift image left 10 pixels 23 | elif event.key == pygame.K_RIGHT: 24 | x = x + 10 #shift image right 10 pixels 25 | 26 | gamewindow.blit(sprite, (x,y)) 27 | pygame.display.update() 28 | 29 | 30 | pygame.quit() 31 | -------------------------------------------------------------------------------- /Chapter 11/game09.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | sprite = pygame.image.load('rocket.png') 9 | 10 | 11 | #initialize our variables 12 | running = 1 13 | x=250 14 | y=280 15 | 16 | 17 | while running: 18 | 19 | for event in pygame.event.get(): 20 | if event.type == pygame.KEYDOWN: 21 | if event.key == pygame.K_LEFT: 22 | x = x - 10 #shift image left 10 pixels 23 | elif event.key == pygame.K_RIGHT: 24 | x = x + 10 #shift image right 10 pixels 25 | 26 | gamewindow.blit(sprite, (x,y)) 27 | pygame.display.update() 28 | gamewindow.fill((0,0,0)) #clear screen 29 | 30 | pygame.quit() 31 | -------------------------------------------------------------------------------- /Chapter 11/game10.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | sprite = pygame.image.load('rocket.png') 9 | 10 | 11 | #initialize our variables 12 | running = 1 13 | x=250 14 | y=280 15 | 16 | 17 | while running: 18 | 19 | for event in pygame.event.get(): 20 | if event.type == pygame.KEYDOWN: 21 | if event.key == pygame.K_LEFT: 22 | x = x - 10 #shift image left 10 pixels 23 | elif event.key == pygame.K_RIGHT: 24 | x = x + 10 #shift image right 10 pixels 25 | if event.type == pygame.QUIT: 26 | running = 0 #close 27 | 28 | gamewindow.blit(sprite, (x,y)) 29 | pygame.display.update() 30 | gamewindow.fill((0,0,0)) #clear screen 31 | 32 | pygame.quit() 33 | -------------------------------------------------------------------------------- /Chapter 11/game11.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | sprite = pygame.image.load('rocket.png') 9 | ufo = pygame.image.load('ufo.png') 10 | 11 | #initialize our variables 12 | running = 1 13 | badguy=1 14 | x=250 15 | y=280 16 | 17 | 18 | while running: 19 | 20 | for event in pygame.event.get(): 21 | if event.type == pygame.KEYDOWN: 22 | if event.key == pygame.K_LEFT: 23 | x = x - 10 #shift image left 10 pixels 24 | elif event.key == pygame.K_RIGHT: 25 | x = x + 10 #shift image right 10 pixels 26 | if event.type == pygame.QUIT: 27 | running = 0 #close 28 | 29 | gamewindow.blit(sprite, (x,y)) 30 | gamewindow.blit(ufo, (10,50)) 31 | pygame.display.update() 32 | gamewindow.fill((0,0,0)) #clear screen 33 | 34 | pygame.quit() 35 | -------------------------------------------------------------------------------- /Chapter 11/rocket.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/rocket.png -------------------------------------------------------------------------------- /Chapter 11/shapes.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | running=1 9 | 10 | #define colours using RGB values 11 | white = (255, 255, 255) 12 | black = (0, 0, 0) 13 | green = (0, 255, 0) 14 | blue = (0, 0, 255) 15 | red = (255, 0, 0) 16 | 17 | while running: 18 | for event in pygame.event.get(): 19 | if event.type == pygame.QUIT: 20 | running = 0 #close 21 | 22 | 23 | #draw a circle 24 | pygame.draw.circle(gamewindow, green, (200, 100), 43, 0) 25 | 26 | #draw an ellipse 27 | pygame.draw.ellipse(gamewindow, blue, (400, 66, 60, 77), 6) 28 | 29 | #draw a rectangle 30 | pygame.draw.rect(gamewindow, red, (300, 200, 100, 150)) 31 | 32 | 33 | 34 | pygame.display.update() #update screen 35 | 36 | 37 | pygame.quit() 38 | -------------------------------------------------------------------------------- /Chapter 11/spriteamin.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | #create our game clock 9 | clock = pygame.time.Clock() 10 | 11 | #turn on key repeat 12 | pygame.key.set_repeat(1, 25) 13 | 14 | counter=0 15 | running=1 16 | x=55 17 | y=55 18 | 19 | 20 | #load animation frames into list 21 | frames = [pygame.image.load('frame1.png'), 22 | pygame.image.load('frame2.png'), 23 | pygame.image.load('frame3.png')] 24 | 25 | 26 | #game loop 27 | while running: 28 | #execute loop at 25 frames per second 29 | clock.tick(25) 30 | 31 | for event in pygame.event.get(): 32 | if event.type == pygame.QUIT: 33 | running = 0 #close 34 | elif event.type == pygame.KEYDOWN: 35 | if event.key == pygame.K_LEFT: 36 | x = x - 10 #shift image right 10 pixels 37 | elif event.key == pygame.K_RIGHT: 38 | x = x + 10 #shift image right 10 pixels 39 | 40 | gamewindow.fill((0,0,0)) #clear screen 41 | 42 | 43 | gamewindow.blit(frames[counter], (x,y)) #redraw sprite in new position 44 | counter = (counter + 1) % len(frames) #move to next frame in frames list 45 | 46 | 47 | pygame.display.update() #update screen 48 | 49 | 50 | pygame.quit() 51 | -------------------------------------------------------------------------------- /Chapter 11/text.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | #load image and assign to 'sprite' 9 | sprite = pygame.image.load('rocket.png') 10 | 11 | running = 1 12 | x=250 13 | y=280 14 | 15 | fontColor=(0,150,250) 16 | 17 | gameFont = pygame.font.Font('C:\Windows\Fonts\helvetica.ttf', 32) 18 | 19 | gameText = gameFont.render('Alien Invader', True, 'White') 20 | 21 | gamewindow.blit(gameText,(200,44)) 22 | 23 | while running: 24 | #add image to game window 25 | gamewindow.blit(sprite, (x,y)) 26 | 27 | 28 | 29 | #update the game window display 30 | pygame.display.update() 31 | 32 | 33 | pygame.quit() 34 | -------------------------------------------------------------------------------- /Chapter 11/ufo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 11/ufo.png -------------------------------------------------------------------------------- /Chapter 11/window.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | gamewindow = pygame.display.set_mode((640,480)) 6 | pygame.display.set_caption("Game Window") 7 | 8 | 9 | 10 | 11 | pygame.quit() 12 | -------------------------------------------------------------------------------- /Chapter 12/contactus.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |Thanks', name, ' for your message.
') 24 | print ('This is what you sent:
') 25 | print (message) 26 | print ('') 27 | print ('') 28 | 29 | # Construct email message 30 | -------------------------------------------------------------------------------- /Chapter 12/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 12/flask/app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | 3 | app = Flask(__name__) 4 | 5 | @app.route('/') 6 | def index(): 7 | return 'This is a Flask Web App' 8 | 9 | @app.route('/shop') 10 | def shop(): 11 | return 'This is the shop page' 12 | 13 | if __name__ == '__main__': 14 | app.run(debug=True, host='0.0.0.0') 15 | -------------------------------------------------------------------------------- /Chapter 12/flask/app01.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | 3 | app = Flask(__name__) 4 | 5 | @app.route('/') 6 | def index(): 7 | return 'This is a Flask Web App' 8 | 9 | @app.route('/shop') 10 | def shop(): 11 | return 'This is the shop page' 12 | 13 | if __name__ == '__main__': 14 | app.run(debug=True, host='0.0.0.0') 15 | -------------------------------------------------------------------------------- /Chapter 12/flask/app02.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | 3 | app = Flask(__name__) 4 | 5 | @app.route('/') 6 | def index(): 7 | return render_template('index.html') 8 | 9 | 10 | if __name__ == '__main__': 11 | app.run(debug=True, host='0.0.0.0') 12 | -------------------------------------------------------------------------------- /Chapter 12/flask/app03.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | 3 | app = Flask(__name__) 4 | 5 | @app.route('/') 6 | def index(): 7 | return render_template('index2.html', title="Windows 10", price="£12.99") 8 | 9 | 10 | if __name__ == '__main__': 11 | app.run(debug=True, host='0.0.0.0') 12 | -------------------------------------------------------------------------------- /Chapter 12/flask/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 12/flask/static/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 12/flask/static/images/books.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 12/flask/static/images/books.jpg -------------------------------------------------------------------------------- /Chapter 12/flask/static/images/click.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 12/flask/static/images/click.jpg -------------------------------------------------------------------------------- /Chapter 12/flask/static/images/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 12/flask/static/images/fullnavbar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 12/flask/static/images/fullnavbar.png -------------------------------------------------------------------------------- /Chapter 12/flask/static/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 12/flask/static/images/logo.png -------------------------------------------------------------------------------- /Chapter 12/flask/static/images/nav.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/absolute-beginners-guide-python/8ec7bd854fc2a5167a0de7025539cabcdc7b851e/Chapter 12/flask/static/images/nav.png -------------------------------------------------------------------------------- /Chapter 12/flask/static/styles.css: -------------------------------------------------------------------------------- 1 | html { 2 | background: white; 3 | } 4 | body { 5 | width: 960px; 6 | margin: 0 auto; 7 | font-family: Helvetica; 8 | } 9 | section, header, footer, nav { 10 | display: block; 11 | } 12 | div, section, h1, h2, h3, p { 13 | margin: 0; 14 | padding: 0; 15 | } 16 | 17 | header { 18 | background: dodgerblue; 19 | height: 100px; 20 | padding: 12px 0 0 32px; 21 | } 22 | 23 | nav { 24 | margin-bottom: 16px; 25 | } 26 | 27 | h1 { 28 | font-weight: normal; 29 | color: white; 30 | } 31 | h2 { 32 | font-weight: normal; 33 | font-variant: small-caps; 34 | } 35 | p { 36 | line-height: 1.5; 37 | } 38 | 39 | .col1 { 40 | float: left; 41 | padding-left: 32px; 42 | width: 512px; 43 | } 44 | 45 | .col2 { 46 | float: right; 47 | padding-right: 32px; 48 | width: 352px; 49 | } -------------------------------------------------------------------------------- /Chapter 12/flask/templates/desktop.ini: -------------------------------------------------------------------------------- 1 | [ . S h e l l C l a s s I n f o ] 2 | C o n f i r m F i l e O p = 0 3 | I c o n R e s o u r c e = C : \ P r o g r a m F i l e s \ G o o g l e \ D r i v e F i l e S t r e a m \ 6 4 . 0 . 4 . 0 \ G o o g l e D r i v e F S . e x e , 2 3 4 | -------------------------------------------------------------------------------- /Chapter 12/flask/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |Elluminet Press, a member of the Independent Book 12 | Publisher’s Association, is a publisher with more 13 | than 100 books in print, beautifully bound in either 14 | hardback or paperback, as well as in electronic formats, 15 | covering a variety of genres
16 | 17 | {% print ("python code") %} 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter 12/flask/templates/index2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |Elluminet Press, a member of the Independent Book 15 | Publisher’s Association, is a publisher with more 16 | than 100 books in print, beautifully bound in either 17 | hardback or paperback, as well as in electronic formats, 18 | covering a variety of genres
19 | 20 | 21 |Here are some of our best sellers:
22 | 23 |Title | 26 |Price | 27 |
{{title}} | 30 |{{price}} | 31 |