Adam Curylo's Python
This book is an undeveloped draft or outline. You can help to develop the work, or you can ask for assistance in the project room. |
Python:
Python is a programming language, probably one of the easiest ones to learn.
USEFULL WEBSITES:
Python Programming/Getting Python
MAKING UNIQUE LISTS OF NUMBERS:
import random
random_numbers = [random.randint(0,9),random.randint(0,9),random.randint(0,9),random.randint(0,9)]
random_numbers = list(set(random_numbers))
while len(random_numbers) < 4:
random_numbers = [random.randint(0,9),random.randint(0,9),random.randint(0,9),random.randint(0,9)]
random_numbers = list(set(random_numbers))
print(random_numbers)
Tkinter:
I think that tkinter is a great Python feature which allows you to create great user interfaces and allows you to show information in a fantastic way as shown in the example below.
Using Tkinter for a user interface for a quiz:
from tkinter import *
import tkinter as tk
def tkinter_quiz():
q = 0
s = -1
count = 0
correct = 0
incorrect = 0
question = ["Give 1 Value A Boolean Can Be","Write the first integer","What is 5 in binary","What is 101 in normal numbers"]
answer = ["true","1","101","5"]
answer2 = ["false","0","101","5"]
root = Tk()
name = tk.Label(root,text = "Programming Quiz")
name.pack()
label = tk.Label(root,text = question[0])
label.pack()
entry = tk.Entry(root)
entry.pack()
def out():
global q,correct,incorrect,s,count
count = count + 1
ans = entry.get()
if count < 4:
if answer[q] == ans or answer2[q] == ans :
q = q + 1
entry.delete(0, END)
correct = correct + 1
label.config(text = question[q])
else:
q = q + 1
entry.delete(0, END)
incorrect = incorrect + 1
label.config(text = question[q])
else:
entry.delete(0, END)
label.config(text = "Correct: "+str(correct) + " Incorrect: "+str(incorrect))
def stop():
global q,correct,incorrect
q = 0
correct = 0
incorrect = 0
entry.delete(0, END)
label.config(text = question[0])
button = tk.Button(root,text = "Submit",command = out)
button.pack()
button_two = tk.Button(root,text = "Restart",command = stop)
button_two.pack()
root.mainloop()
In Tkinter you can use different parts of the mouse to call different functions, but it may be hard for you to add parameters through the functions.This is how you do it:
b1.bind("<Button-1>",lambda event: b1p1(event,other_parameter))
b1.bind("<Button-3>",lambda event: b1p2(event,another_parameter))
'<Button-1>' and '<Button-3>' mean left click and right click '<Button-2>' is the press of the scroll wheel.This means that if they do that action while hovering above the button it will do the function you are telling it to do. The function you are calling has to have a parameter called event and then the other parameters you are trying to add.
Pygame:
Another great part of Python is pygame.Pygame allows you to do pretty much anything from games to graphs and even sound.You need to download Pygame from www.pygame.org.Here is an example of a bar chart creator with pygame.
import pygame
import time
import random
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Bar Charts')
##icon = pygame.image.load("chart.png")
##pygame.display.set_icon(icon)
white = (255,255,255)
black = (0,0,0)
red = (150,0,0)
light_red = (255,0,0)
yellow = (150,150,0)
light_yellow =(255,255,0)
green = (34,177,76)
light_green = (0,255,0)
clock = pygame.time.Clock()
bars = 1
Y_Units = 1
Unit_Size = 0
Bar_Size = Unit_Size
smallfont = pygame.font.SysFont("comicsansms", 25)
medfont = pygame.font.SysFont("comicsansms", 50)
largefont = pygame.font.SysFont("comicsansms", 85)
BarList = []
def text_objects(text, color,size = "small"):
if size == "small":
textSurface = smallfont.render(text, True, color)
if size == "medium":
textSurface = medfont.render(text, True, color)
if size == "large":
textSurface = largefont.render(text, True, color)
return textSurface, textSurface.get_rect()
def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size = "small"):
textSurf, textRect = text_objects(msg,color,size)
textRect.center = ((buttonx+(buttonwidth/2)), buttony+(buttonheight/2))
gameDisplay.blit(textSurf, textRect)
def message_to_screen(msg,color, y_displace = 0, size = "small"):
textSurf, textRect = text_objects(msg,color,size)
textRect.center = (int(display_width / 2), int(display_height / 2)+y_displace)
gameDisplay.blit(textSurf, textRect)
def button(text, x, y, width, height, inactive_color, active_color, action = None):
global bars
global Y_Units
global Unit_Size
global Bar_Size
global BarList
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x,y,width,height))
if click[0] == 1 and action != None:
if action == "quit":
pygame.quit()
quit()
if action == "start":
draw_grid()
if action == "controls":
information()
if action == "play":
start_app()
if action == "bars+":
if bars != 10:
bars += 1
print (bars)
if action == "bars-":
if bars != 1:
bars -= 1
print (bars)
if action == "Y Units+":
if Y_Units != 10:
Y_Units += 1
print (Y_Units)
if action == "Y Units-":
if Y_Units != 1:
Y_Units -= 1
print (Y_Units)
if action == "Unit_Size+":
Bar_Size= 0
Unit_Size += 1
print (Unit_Size)
if action == "Unit_Size-":
Unit_Size -= 1
print (Unit_Size)
if action == "Bar_Size+":
if Bar_Size <= ((Y_Units * Unit_Size)-1):
Bar_Size += Unit_Size
print (Bar_Size)
if action == "Bar_Size-":
if Bar_Size > 1:
Bar_Size -= Unit_Size
print (Bar_Size)
if action == "enter":
if len(BarList)!= bars:
BarList.append(Bar_Size)
print(BarList)
else:
pygame.draw.rect(gameDisplay, inactive_color, (x,y,width,height))
text_to_button(text,black,x,y,width,height)
def information():
global bars
global BarList
info = True
while info:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
text = smallfont.render("Number of Bars Assigned: "+str(len(BarList)), True, red)
gameDisplay.blit(text, [50,500])
pygame.draw.rect(gameDisplay,black,(55,105,130,50))
button("Plus Bar", 50,100,130,50, green, light_green, action = "bars+")
pygame.draw.rect(gameDisplay,black,(205,105,130,50))
button("Minus Bar", 200,100,130,50, red, light_red, action = "bars-")
text = smallfont.render("Bars: "+str(bars), True, black)
gameDisplay.blit(text, [350,100])
pygame.draw.rect(gameDisplay,black,(55,205,130,50))
button("+ Y Units", 50,200,130,50, green, light_green, action = "Y Units+")
pygame.draw.rect(gameDisplay,black,(205,205,130,50))
button("- Y Units", 200,200,130,50, red, light_red, action = "Y Units-")
text = smallfont.render("Y Units: "+str(Y_Units), True, black)
gameDisplay.blit(text, [350,200])
pygame.draw.rect(gameDisplay,black,(55,305,130,50))
button("+ Unit Size", 50,300,130,50, green, light_green, action = "Unit_Size+")
pygame.draw.rect(gameDisplay,black,(205,305,130,50))
button("- Unit Size", 200,300,130,50, red, light_red, action = "Unit_Size-")
text = smallfont.render("Unit Size: "+str(Unit_Size), True, black)
gameDisplay.blit(text, [350,300])
pygame.draw.rect(gameDisplay,black,(55,405,130,50))
button("+ Bar Size", 50,400,130,50, green, light_green, action = "Bar_Size+")
pygame.draw.rect(gameDisplay,black,(205,405,130,50))
button("- Bar Size", 200,400,130,50, red, light_red, action = "Bar_Size-")
text = smallfont.render("Bar Size: "+str(Bar_Size), True, black)
gameDisplay.blit(text, [500,400])
pygame.draw.rect(gameDisplay,black,(355,405,130,50))
button("Enter", 350,400,130,50, green, light_green, action = "enter")
pygame.draw.rect(gameDisplay,black,(455,55,130,50))
button("Back", 450,50,130,50, green, light_green, action = "play")
## number_of_bars = Entry(root)
## number_of_y_units = Entry(root)
## bar_size = Entry(root)
pygame.display.update()
clock.tick(15)
def draw_grid():
global bars
global Y_Units
global Unit_Size
global Bar_Size
global BarList
gameDisplay.fill(white)
grid = True
while grid:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#things(thingx, thingy, thingw, thingh)
pygame.draw.line(gameDisplay,black,(50,98), (50, 500))
pygame.draw.line(gameDisplay,black,(50, 500), (750, 500))
for b in range(0,bars):
pygame.draw.line(gameDisplay,black,(50,478 - ((40 * (BarList[b]/Unit_Size)))+20),(750,(478 - ((40 * (BarList[b]/Unit_Size)))+20)))
text = smallfont.render("0-", True, black)
gameDisplay.blit(text, [0,478])
for x in range(0,Y_Units + 1):
text = smallfont.render(str(Unit_Size * x)+'-', True, black)
gameDisplay.blit(text, [0,(478 - (40 * x))])
for a in range(0,bars):
pygame.draw.rect(gameDisplay,red,((a + 1)*60,(478 - ((40 * (BarList[a]/Unit_Size)))+20),50,(40*(BarList[a]/Unit_Size))))
for c in range(0,bars):
text = smallfont.render('Bar'+str(c+1), True, black)
gameDisplay.blit(text, [(c + 1)*60,500])
pygame.display.update()
def start_app():
global start
start = True
while start:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
start = False
elif event.key == pygame.K_q:
pygame.quit()
quit()
gameDisplay.fill(white)
message_to_screen("Bar Chart Creation",green,-100,size="large")
message_to_screen("This application makes bar charts for you",black,-30)
message_to_screen("Firstly go to 'Enter Info' and enter your info",black,10)
message_to_screen("Then press 'Back' and then press'Start'",black,50)
#message_to_screen("Press C to play, P to pause or Q to quit",black,180)
pygame.draw.rect(gameDisplay,black,(155,505,100,50))
pygame.draw.rect(gameDisplay,black,(355,505,130,50))
pygame.draw.rect(gameDisplay,black,(555,505,100,50))
button("Start", 150,500,100,50, green, light_green, action = "start")
button("Enter Info", 350,500,130,50, yellow, light_yellow, action = "controls")
button("Quit", 550,500,100,50, red, light_red, action = "quit")
pygame.display.update()
clock.tick(15)
start_app()