自己今天第一次做一個python生日賀卡:
效果圖
參考之前有位大佬的作品,自己稍微的改造了一下下
首先:上程序
Birthday.py
""" --------------------------------------- Author:Zjh Date: 2019-10-10 09:05 --------------------------------------- """ #coding=utf-8 import turtle import random import time from turtle import * from random import * # set the background color for the page import time import pygame def make_cake(turtle,x=0, y=0): tina = turtle tina.penup() tina.color('white') tina.goto(x, y) tina.pendown() tina.begin_fill() tina.goto(x + 20, y) tina.goto(x + 20, y + 20) tina.goto(x - 20, y + 20) tina.goto(x - 20, y) tina.goto(x, y) tina.end_fill() tina.goto(x, y + 20) tina.color('blue') tina.goto(x, y + 35) tina.goto(x, y + 30) tina.color('black') tina.goto(x, y + 20) tina.penup() tina.goto(x, y + 10) file='media/front.mp3' pygame.mixer.init() #把mp3初始化出來 #print("播放音樂1") track = pygame.mixer.music.load(file) pygame.mixer.music.play() #time.sleep(60) #tommy.reset() bg = turtle.Screen() bg.bgcolor("black") tommy = turtle.Turtle() tommy.shape("turtle") tommy.speed(1) tommy.penup() tommy.goto(-250,0) tommy.color("orange") tommy.write(",,ԾㅂԾ,,請小壽星插上耳機后觀看哦 ",font=('微軟雅黑', 25, 'normal')) time.sleep(5) tommy.clear() x = -300 y = 250 tommy.goto(x,y) tommy.color("pink") pygame.mixer.music.stop() file='media/Happy.mp3' # pygame.mixer.init() #把mp3初始化出來 #print("播放音樂1") track = pygame.mixer.music.load(file) pygame.mixer.music.play() time.sleep(4) tommy.penup() tommy.goto(x,y-75-25-25-25-25-25-25-25-25-25-50-25) #每次減25 tommy.write(" φ(゜▽゜*)♪ 有“聲音啦”,接下來我再增加點“五彩斑斕的顏色哦”。。。 ",font=('微軟雅黑', 15, 'normal')) time.sleep(4) tommy.clear() tommy.pensize(6) # draw lines tommy.penup() tommy.goto(-190, -180) tommy.color("yellow") tommy.pensize(6) tommy.pendown() tommy.goto(190,-180) tommy.penup() tommy.penup() tommy.goto(-160, -150) tommy.color("purple") tommy.pensize(6) tommy.pendown() tommy.goto(160,-150) tommy.penup() tommy.penup() tommy.goto(-130, -120) tommy.color("teal") tommy.pensize(6) tommy.pendown() tommy.goto(130,-120) tommy.penup() # draw cake tommy.goto(-74,-110) #這個是畫蛋糕 tommy.begin_fill() tommy.pendown() tommy.color("white") tommy.goto(50,-110) tommy.left(90) tommy.forward(60) tommy.left(90) tommy.forward(125) tommy.left(90) tommy.forward(60) tommy.end_fill() tommy.penup() tommy.goto(-74,-110) #這個是畫蛋糕,到這個坐標去,這個是黃色那層 tommy.begin_fill() tommy.pendown() #放下畫筆 tommy.color("yellow") tommy.goto(50,-110) tommy.left(90) #tommy.forward(60) #向前60碼,這個是那個橫線 tommy.left(90) tommy.forward(45) #這個是一層的厚度 tommy.left(90) #這個是根據角度來轉方像 tommy.forward(125) tommy.left(90) #這個是根據角度來轉方像 tommy.forward(45) tommy.end_fill() tommy.penup() tommy.goto(-74,-110) #這個是畫蛋糕,到這個坐標去,這個是黃色那層 tommy.begin_fill() tommy.pendown() #放下畫筆 tommy.color("#40E0D0") tommy.goto(50,-110) tommy.left(90) #tommy.forward(60) #向前60碼,這個是那個橫線 tommy.left(90) tommy.forward(30) #這個是一層的厚度 tommy.left(90) #這個是根據角度來轉方像 tommy.forward(125) tommy.left(90) #這個是根據角度來轉方像 tommy.forward(30) tommy.end_fill() tommy.penup() tommy.goto(-74,-110) #這個是畫蛋糕,到這個坐標去,最下面那層 tommy.begin_fill() tommy.pendown() #放下畫筆 tommy.color("#BA55D3") tommy.goto(50,-110) tommy.left(90) #tommy.forward(60) #向前60碼,這個是那個橫線 tommy.left(90) tommy.forward(15) #這個是一層的厚度 tommy.left(90) #這個是根據角度來轉方像 tommy.forward(125) tommy.left(90) #這個是根據角度來轉方像 tommy.forward(15) tommy.end_fill() tommy.penup() #draw candles tommy.goto(-60, -40) tommy.color("aquamarine") tommy.pendown() tommy.pensize(3) tommy.goto(-60, -20) tommy.penup() tommy.goto(-40, -40) tommy.color("yellow") tommy.pendown() tommy.pensize(3) tommy.goto(-40, -20) tommy.penup() tommy.goto(-20, -40) tommy.color("green") tommy.pendown() tommy.pensize(3) tommy.goto(-20, -20) tommy.penup() tommy.goto(0, -40) tommy.color("red") tommy.pendown() tommy.pensize(3) tommy.goto(0, -20) tommy.penup() tommy.goto(20, -40) tommy.color("blue") tommy.pendown() tommy.pensize(3) tommy.goto(20, -20) tommy.penup() tommy.goto(40, -40) tommy.color("#556B2F") tommy.pendown() tommy.pensize(3) tommy.goto(40, -20) tommy.penup() # print message bg.bgcolor("pink") def snow(): tommy.hideturtle() tommy.speed(100) tommy.pensize(2) for i in range(100): r=random() g=random() b=random() tommy.pencolor(r,g,b) tommy.penup() tommy.setx(randint(-350,350)) tommy.sety(randint(1,270)) tommy.pendown() dens=randint(8,12) snowsize=randint(10,14) for j in range(dens): tommy.forward(snowsize) tommy.backward(snowsize) tommy.right(360/dens) snow() tommy.goto(-250, 90) tommy.color("orange") tommy.pendown() tommy.write( "漫天 。・゚・(ノД`)ヽ(゚Д゚ ) 煙花送給你~",font=('微軟雅黑', 24, 'normal')) pygame.mixer.music.stop() file='media/sweet.mp3' #print("播放音樂1") track = pygame.mixer.music.load(file) pygame.mixer.music.play() time.sleep(4) tommy.penup() tommy.goto(-250,-250) tommy.write("祝你生日快樂喲,小壽星 ο(=•ω<=)p⌒☆!",font=('微軟雅黑', 24, 'normal')) time.sleep(5) # send the turtle to the corner tommy.penup() tommy.goto(-250, 250) #tommy.exitonclick() tommy.hideturtle() time.sleep(20) tommy.clear() #清空屏幕了,然后設置背景色為黑色,滾動演員表 bg.bgcolor("white") tommy.penup() tommy.goto(-300, 90) tommy.color("black") tommy.pendown() tommy.write(" …(⊙_⊙;)…天吶!是不是很好看呀?堅持看到了這里",font=('微軟雅黑', 24, 'normal')) tommy.penup() tommy.goto(-300, 60) tommy.write(" (〃` 3′〃)那好咯,后面還有一點點結尾!",font=('微軟雅黑', 24, 'normal')) tommy.penup() time.sleep(4) tommy.goto(-250, 90) tommy.clear() tommy.write("(python友情支持)就是我啦 ┑( ̄Д  ̄)┍ ",font=('微軟雅黑', 24, 'normal')) time.sleep(4) # tommy.clear() # tommy.write("音樂 、后期、導演、聲優、編輯、構思 ",font=('微軟雅黑', 24, 'normal')) # time.sleep(4) # # tommy.clear() # tommy.write("♂ 是你那不善言辭的大豬蹄子男票 ",font=('微軟雅黑', 24, 'normal')) # time.sleep(4) # tommy.clear() tommy.write("(◍•ᴗ•◍)ゝ特用此小程序祝你生日快樂~ ",font=('微軟雅黑', 24, 'normal')) time.sleep(4) tommy.clear() tommy.write("( ◜◡‾) 天天開心~ ☻ ",font=('微軟雅黑', 24, 'normal')) time.sleep(4) tommy.clear() tommy.write("最后biu 一個小心心給你把~",font=('微軟雅黑', 24, 'normal')) time.sleep(3) #這兒最后畫一個小心心, tommy.clear() #coding=utf-8 #畫一個圓的東西出來 import turtle #turtle = tommy turtle.speed(1) turtle.pensize(6)#:設置畫筆的寬度; turtle.left(30) turtle.circle(90,300) #半徑大小 #畫眼睛 turtle.right(100) turtle.forward(60) turtle.left(150) turtle.forward(55) turtle.right(110) turtle.forward(40) turtle.penup() turtle.goto(-15,90) #畫眼睛了 turtle.begin_fill() turtle.color("black") turtle.circle(8) turtle.end_fill() turtle.penup() turtle.goto(-75,90) #畫眼睛了 turtle.begin_fill() turtle.color("black") turtle.circle(8) turtle.end_fill() turtle.penup() turtle.goto(0,70) #畫眼睛了 turtle.begin_fill() turtle.color("red") turtle.circle(8) turtle.end_fill() turtle.penup() turtle.goto(-85,70) #畫眼睛了 turtle.begin_fill() turtle.color("red") turtle.circle(8) turtle.end_fill() turtle.penup() turtle.goto(-50,50) #畫眼睛了 # turtle.begin_fill() turtle.pendown() turtle.color("black") turtle.left(45) turtle.circle(20,90) # turtle.end_fill() #畫另一只手 turtle.penup() turtle.color("black") turtle.goto(0,0) turtle.right(35) turtle.pendown() turtle.forward(75) turtle.left(85) turtle.forward(25) turtle.right(165) turtle.forward(25) turtle.left(90) turtle.forward(35) turtle.right(155) turtle.forward(35) turtle.left(75) turtle.forward(20) turtle.right(85) turtle.forward(20) turtle.right(85) turtle.forward(25) turtle.left(70) turtle.forward(35) turtle.left(15) turtle.forward(20) turtle.left(60) turtle.forward(40) #愛心 # turtle.begin_fill() turtle.penup() turtle.color("red") turtle.right(180) turtle.goto(135,135) turtle.pendown() turtle.circle(20,170) turtle.forward(15) turtle.left(45) turtle.forward(55) turtle.penup() turtle.color("red") turtle.right(250) turtle.goto(135,135) turtle.pendown() turtle.circle(-20,170) turtle.forward(15) turtle.right(45) turtle.forward(55) # turtle.end_fill() turtle.penup() turtle.goto(135,135) turtle.pendown() # turtle.right(90) turtle.forward(45) turtle.left(160) turtle.forward(75) turtle.right(160) turtle.forward(55) turtle.goto(135,100) turtle.penup() turtle.goto(-100,-100) turtle.write("The end",font=("微軟雅黑",24,"normal")) time.sleep(2) done() #可以使窗口保持 pygame.mixer.music.stop()
理解完成之后想要在自己的筆記上面跑起來:結果出了好幾個小小的問題
問題1:我安裝是Python3.6.3的版本,但是直接運行程序的話提示沒有pygame 模塊,所以自己就在自己Pycharm中進行下載,但是搜索都搜索不到,所以就到cmd中去利用pip命令去安裝了
pip命令去安裝:
下載地址:https://pypi.org/
搜索pygame,轉到pygame1.9.6之后點擊pygame1.9.6,然后轉到另一個界面
轉到下面的界面,之后選擇cp36的下載即可,因為自己安裝的python是64位的,所以下載了最后一個pygame-1.9.6-cp36-cp36m-win_amd64.whl
由於自己的之后黑窗口安裝
打開cmd命令窗口,通過“pip install"命令安裝pygame安裝包。當然的pip命令最好是在whl文件所在的目錄下面,這樣的話可以輸入幾個開頭字母之后自動補全
pip install pygame-1.9.6-cp36-cp36m-win_amd64.whl
檢測是否安裝成功,在cmd命令窗口下,輸入”python“,進入python交互命令窗口,輸入”import pygame“,如果運行成功,表示安裝成功。
之后運行成功
最后想要到別的電腦上也能夠運行,所以就想將程序打成exe可執行程序
接下來說干就干
大家都知道,Python是一種腳本語言,也就是解釋型的語言,需要解釋器來進行解釋以后才可以執行,而Python源代碼需要在Python虛擬機上面運行,但是我們做好的程序,不可能給用戶使用的時候還讓他安裝一個Python環境,所以今天我們講解如何將Python程序打包為exe可執行文件。
提示: 以下的教程都是再有Python環境的基礎上的,如果沒有安裝,請先參考:Python安裝以及環境變量配置
一、安裝pyinsatller
打開命令行窗口,輸入如下指令
pip3 install pyinstaller
然后靜待pyinstaller自動安裝好
但是自己在安裝pyinstaller的過程中出現了問題
問題解決加上參數 --no-use-pep517
pip3 install pyinstaller --no-use-pep517
成功的安裝了 輸入pyinstaller --version去驗證是否成功,顯示版本也就是成功了
如果還未解決的話,可以繼續的參考鏈接解決哦 https://blog.csdn.net/qq_26561157/article/details/89812450
二、使用pyinstaller打包Python程序(參考鏈接https://blog.csdn.net/qq_33462307/article/details/90479045)
①在你的Python程序文件夾上(不點進去)按住shift並且右鍵,在彈出的選項中點擊"在此處打開命令行窗口"或者“在此處打開PowerShell窗口”
在上面的目錄下按住shift 之后右鍵選擇打開powershell
下面的是自己的界面:
>>>或者先進入命令行窗口,然后使用cd指令進入程序文件夾,示例:
cd E:\課件\python數據分析與數據挖掘\數據挖掘方法及應用\Python數據挖掘\程序\Gp201820192\BigData\BirthdayFinal
②輸入以下指令,開始打包
pyinstaller -F -w (-i icofile) filename
filename表示你的Python程序文件名
-w 表示隱藏程序運行時的命令行窗口(不加-w會有黑色窗口)
括號內的為可選參數,-i icofile表示給程序加上圖標,圖標必須為.ico格式 icofile表示圖標的位置,建議直接放在程序文件夾里面,這樣子打包的時候直接寫文件名就好
具體的將文件改為.ico格式的工具網站 http://www.bitbug.net/
接着 pyinstaller -F -w (-i icofile) filename 命令輸入完成,按回車,就會開始自動打包了,第一次打包過程可能比較緩慢
輸入示例:
pyinstaller -F -w -i birth.ico Birthday.py
(pyinstaller中的各個參數的含義:https://blog.csdn.net/weixin_39000819/article/details/80942423)
打成exe成果的效果
然后我們進入到程序目錄里面會再看到一個名稱為dist目錄,打包好的exe程序就在里面
注意:如果原本的程序中使用到了相應的資源,需要復制到dist目錄下,否則程序會無法運行
比如我這里在程序中使用到了好多的音頻,所以就需要將該文件放到dist目錄下,我這里是在dist目錄下新建了一個目錄:media,把那些文件放了進去
形成exe目錄在之后的結構
最后進入到自己的dist目錄下,點擊exe程序即可啟動程序,具體的程序效果可見鏈接 https://www.bilibili.com/video/av53114885
如果點擊exe程序,出現錯誤彈出框
Pygame使用pyinstaller打包exe無法運行,顯示 failed to execute script的問題
原因剖析
pygame需要的資源如圖片音樂等pyinstaller是不會給你打包的,需手動加入
本來自己用pyinstaller 形成的dist命令是沒有依賴的那些mp3和圖片等文件的,自己需要去加入到和exe一樣的dist目錄下,如果不加入的話會提示 failed to execute script