亚当·库里洛的 Python
外观
本书是一个未开发的草稿或大纲。 您可以帮助 开发作品,或者您可以在 项目室 中寻求帮助。 |
Python
Python 是一种编程语言,可能是最容易学习的一种。
有用网站
制作独特的数字列表
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
我认为 tkinter 是一个很棒的 Python 功能,它允许您创建很棒的用户界面,并以示例中所示的绝妙方式显示信息。
使用 Tkinter 为测验创建用户界面
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()
在 Tkinter 中,您可以使用鼠标的不同部分来调用不同的函数,但您可能很难通过函数添加参数。以下是如何做到这一点
b1.bind("<Button-1>",lambda event: b1p1(event,other_parameter))
b1.bind("<Button-3>",lambda event: b1p2(event,another_parameter))
'<Button-1>' 和 '<Button-3>' 代表左键单击和右键单击,'<Button-2>' 代表滚动滚轮的按下操作。这意味着如果他们在悬停在按钮上时执行该操作,它将执行您告诉它执行的函数。您调用的函数必须有一个名为 event 的参数,然后是您尝试添加的其他参数。
Pygame
Python 的另一个很棒的部分是 pygame。Pygame 允许您执行从游戏到图形甚至声音的几乎所有操作。您需要从 www.pygame.org 下载 Pygame。以下是一个使用 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()