請在程式最前面加上:
# -*- coding: utf-8 -*-
由于tinker的使用方法簡單,但是無法顯示圖檔,在使用我的方法(參:
https://blog.csdn.net/weixin_43390435/article/details/101158379
)後,pyinstaller正常方法無法打包exe,或者即使程控,打開也會報錯
打包問題解決(請結合第2個大标題下的内容看,否則你可能會走彎路)
隻有輸入以下指令才能打包成exe
pyinstaller -F main.py -p py_dir
其中main.py是主py檔案,不需要輸入額外檔案名,但是要保證打封包件在同一目錄
(參:
https://blog.csdn.net/maixiaochai/article/details/90052333
)不過,此時生成的exe雖然可以打開但是在使用圖檔時會報錯如圖
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPR5keRRUT10keNBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLwkTNxETM0cTMyITOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
打包後調用圖檔依然報錯解決
解決方法(參:
https://www.cnblogs.com/hester/p/11460121.html
)
我使用的是第一種方法
建立b.py輸入
# Fix qt import error
# Include this file before import PyQt5
import os
import sys
import logging
def _append_run_path():
if getattr(sys, 'frozen', False):
pathlist = []
# If the application is run as a bundle, the pyInstaller bootloader
# extends the sys module by a flag frozen=True and sets the app
# path into variable _MEIPASS'.
pathlist.append(sys._MEIPASS)
# the application exe path
_main_app_path = os.path.dirname(sys.executable)
pathlist.append(_main_app_path)
# append to system path enviroment
os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)
logging.error("current PATH: %s", os.environ['PATH'])
_append_run_path()
生成的檔案與主程式,副程式放在同一目錄
主程式導入PyQt5相關庫之前導入
imoort b.py
儲存修改,推出
此時勝利就在前方
保證三個.py檔案都在同一目錄
還是老方法打開指令行
pyinstaller -F main.py -p py_dir
測試後,已經解決了圖檔顯示問題,exe單獨運作沒有問題,但是在顯示圖檔的時候會在控制台列印一些無關緊要的東西,不影響運作
後記
解決PyQt5子產品的程式打開後無法正常傳回tinker子產品的問題
在程式最後加上
os.folk()
即可,經過測試隻有這個可以用,雖然會提示一些錯誤資訊:
os modle not have attribuate 'folk'
諸如此類,但是完美解決在打開圖檔後,程式被異常終止的問題
至此,tinker調用PyQt5使用資源及後引發的種種問題完全解決
待解決的小BUG
點選‘退出’時顯示
name 'exit''is not define
打開圖檔時顯示
module'os'has no attribuate 'folk'
補充圖檔證明方法可用性
tk主界面
調用PyQt5顯示收款碼
今天家人過生日,Copy一個小程式加點料(turtle動态繪制哆啦A夢同時播放音樂)送給他
使用比上文更簡單的打包方法(但是會讓程式産生大量依賴檔案,很占用空間)
安裝pip install cxfreeze
cd到xxx.py目錄執行
cxfreeze xxx.py --target-dir dist
xxx.py是要打包的py檔案
dist是打包後生成的檔案夾
如果用此方法打包
且程式中調用檔案
需要在程式中調用檔案的地方
改用相對路徑(直接打檔案名)
并在打包完成後
把相應資源檔案(要對應寫代碼的時候調用的檔案的名字)放入dist檔案夾根目錄中
否則會調用不了檔案
而且不能操作dist檔案夾中的檔案
否則會有未知錯誤
用這個方法打包,就這樣一個小程式占用80多M,打包上文的程式更是直接占用250多M,很不友好,但是友善,一些送給别人的小程式可以這樣打包
源碼(請忽略PYQT5的部分,本來想用之前的方法整合成1個EXE,後來覺得整合好了估計都要後天了,沒意義,是以就做了一點PYQT的就放棄了)
哆啦的代碼C自
https://www.2cto.com/kf/201806/753261.html
還有畫哆啦A夢的程式雖然是C來的但是他的注釋不夠明确,我多次測試後添加了一些注釋,不過身體顔色問題還是沒解決,如果要播放音樂請自行準備檔案和更改代碼
import threading
def play_music():
import pygame
import time
import b
filepath = r"勾指起誓.mp3";
pygame.mixer.init()
# 加載音樂
pygame.mixer.music.load(filepath)
pygame.mixer.music.play(start=0.0)
#播放時長,沒有此設定,音樂不會播放,會一次性加載完
time.sleep(300)
pygame.mixer.music.play(loops)
#下面是畫哆啦的所有代碼
def painting():
import turtle as t
def my_goto(x,y):
t.penup()
t.goto(x,y)
t.pendown()
def eyes():
t.tracer(False)
a=2.5
for i in range(120):
if 0 <=i<30 or 60<=i<90:
a-=0.05
t.lt(3)
t.fd(a)
else:
a+=0.05
t.lt(3)
t.fd(a)
t.tracer(True)
t.title('哆啦A夢—祝你生日快樂')
t.pensize(3)
t.penup()
t.circle(150,40)
t.pendown()
t.fillcolor("#00a0de")#頭顔色
t.begin_fill()
t.circle(150,280)
t.end_fill()
t.fillcolor("#e70010")#項圈顔色
t.begin_fill()
t.seth(0)
t.fd(200)
t.circle(-5,90)
t.fd(10)
t.circle(-5,90)
t.fd(207)
t.circle(-5,90)
t.fd(10)
t.circle(-5,90)
t.end_fill()
#臉
t.fd(183)
t.fillcolor('#ffffff')#臉顔色
t.begin_fill()
t.lt(45)
t.circle(120,100)
t.seth(90)
eyes()
t.penup()
t.seth(180)
t.fd(60)
t.pendown()
t.seth(90)
eyes()
t.penup()
t.seth(180)
t.fd(60)
t.pendown()
t.seth(215)
t.circle(120,100)
t.end_fill()
my_goto(-10,158)
t.fillcolor('#e70010')#鼻子
t.begin_fill()
t.circle(20)
t.end_fill()
my_goto(5,148)
t.seth(270)
t.fd(100)
t.seth(0)
t.circle(120,50)
t.seth(230)
t.circle(-120,100)
#胡須
my_goto(-37,135)
t.seth(165)
t.fd(60)
my_goto(-37,125)
t.seth(180)
t.fd(60)
my_goto(-37,115)
t.seth(193)
t.fd(60)
my_goto(37,135)
t.seth(15)
t.fd(60)
my_goto(37,125)
t.seth(0)
t.fd(60)
my_goto(37,115)
t.seth(-13)
t.fd(60)
my_goto(0,0)
t.seth(0)
t.penup()
t.circle(150,50)
t.pendown()
#身軀
t.fillcolor('#ffffff')#肚兜和肚子
t.begin_fill()
t.seth(30)
t.end_fill()
t.fillcolor('#00a0de')#右邊胳膊
t.begin_fill()
t.fd(40)
t.seth(70)
t.circle(-30,270)
t.seth(230)
t.fd(80)
t.end_fill()
t.seth(-89)
t.circle(-1000,10)
t.seth(180)
t.fd(70)
t.seth(90)
t.circle(30,180)
t.seth(180)
t.fd(70)
t.seth(100)
t.circle(-1000,9)
t.seth(240)
t.fillcolor('#00a0de')#左邊胳膊
t.begin_fill()
t.fd(40)
t.circle(-30,250)
t.seth(50)
t.fd(70)
t.end_fill()
my_goto(103.74,-197.59)
t.fillcolor('#ffffff')#右腳
t.begin_fill()
t.seth(0)
t.fd(15)
t.circle(-15,180)
t.fd(90)
t.circle(-15,180)
t.fd(10)
t.end_fill()
my_goto(-96.26,-197.59)
t.seth(180)
t.fillcolor('#ffffff')#左腳
t.begin_fill()
t.fd(15)
t.circle(15,180)
t.fd(90)
t.circle(15,180)
t.fd(10)
t.end_fill()
my_goto(-103.42,15.09)
t.seth(0)
t.fd(38)
t.seth(230)
t.fillcolor('#ffffff')#肚子
t.begin_fill()
t.circle(90,260)
t.end_fill()
my_goto(5,-40)
t.seth(0)
t.fillcolor('#ffffff')#百寶袋
t.begin_fill()
t.fd(70)
t.seth(-90)
t.circle(-70,180)
t.seth(0)
t.fd(70)
t.end_fill()
my_goto(-103.42,15.09)
t.fd(90)
t.seth(70)
t.fillcolor('#ffd200')#鈴铛顔色
t.begin_fill()
t.circle(-20)
t.end_fill()
t.goto(-13.42,15.09)
t.seth(250)
t.circle(20,110)
t.seth(90)
t.fd(15)
t.dot(10)
my_goto(0,-150)
t.seth(0)
my_goto(-20,195)
t.fillcolor('#000000')#眼睛顔色
t.begin_fill()
t.circle(13)
t.end_fill()
t.pensize(6)
my_goto(20,205)
t.seth(75)
t.circle(-10,150)
t.pensize(3)
my_goto(-17,200)
t.seth(0)
t.fillcolor('#ffffff')#眼睛裡的光顔色
t.begin_fill()
t.circle(5)
t.end_fill()
my_goto(0,0)
t.mainloop()
threads = []
threads.append(threading.Thread(target=play_music))
threads.append(threading.Thread(target=painting))
if __name__ == '__main__':
for t in threads:
t.start()
'''
t.seth(240)
t.fd(40)
#t.goto(-170,-40)
#t.goto(100,100)
t.fillcolor('#ffffff')#左手
t.begin_fill()
t.circle(30,250)
t.end_fill()
'''
5小時後更新
折騰了幾個小時完全修複了空白,完美播放音樂,可以打包成有依賴的exe檔案,這是送給家人的生日禮物,太累了