PythonQT5打包exe線程使用
打包:
pyinstaller --noconsole --onefile test.py
–noconsole 表示不需要打開命令行
修改:test.spec
一般項目里面需要用的資源文件,比如lib、png、exe等。
需要單獨修改spec文件
pathex=['.'],
binaries=[
('D:/test.png', '.'),
('D:/simsun.ttc', '.'),
('D:/teds.exe', '.')
],
再執(zhí)行下面的命令即可打包加載資源文件到exe中。
pyinstaller test.spec
在py代碼里面直接使用相對路徑即可。
py里面獲取圖片路徑方法:
def resource_path(relative_path):
""" 獲取資源的絕對路徑 """
try:
# PyInstaller 創(chuàng)建的臨時文件夾,或者當以 --onefile 模式打包時的路徑
base_path = sys._MEIPASS
except Exception:
# 如果不是打包后的環(huán)境,則使用當前工作目錄
base_path = os.path.abspath(".")
rp = os.path.join(base_path, relative_path)
return rp
使用:
font_path = resource_path("simsun.ttc")
print('font_path: ', font_path)
font_size = 16
font = ImageFont.truetype(font_path, font_size)
隱藏和現(xiàn)實QT元素:
def on_hide_show_click(self, params):
if self._is_show == 1:
self.frame_2.hide()
self.frame_3.hide()
# 去除限制最小高度和寬度
self.setMinimumSize(0, 0)
self.resize(self.width(), 50)
self._is_show = 0
else:
self.frame_2.show()
self.frame_3.show()
self.resize(self.width(), self.height())
# self.adjustSize()
self._is_show = 1
按鈕點擊方法帶入?yún)?shù):
self.pushButton.clicked.connect(lambda: self.on_hide_show_click(1))
線程使用:
# 定義線程class
class ThreadDemoHandle(QThread):
# 定義信號,用于從線程中發(fā)送數(shù)據(jù)到主線程
data_received = pyqtSignal(list)
# p1 為參數(shù),可以多個哦。
def __init__(self, p1):
super().__init__()
self.p1 = p1
def run(self):
try:
# 處理邏輯,比如發(fā)送請求等等。計算復雜邏輯。。。
# 發(fā)送信號到主線程
self.data_received.emit([self.p1, '結(jié)果'])
except Exception as e:
# flush=True 會強制把日志刷到文件里面,不加的話,有時候不會寫入文件
print('線程處理錯誤:', e, flush=True)
# 使用
self.worker = ThreadDemoHandle(1200)
# handle_result_data 方法是處理線程返回的結(jié)果
self.worker.data_received.connect(self.handle_result_data)
self.worker.start()
@pyqtSlot(list)
def handle_result_data(self, result):
print('結(jié)果:', result[0], result[1])
# 比如渲染 界面等等。到此這篇關(guān)于PythonQT5打包exe線程使用的文章就介紹到這了,更多相關(guān)PythonQT5打包exe線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)對特定列表進行從小到大排序操作示例
這篇文章主要介紹了Python實現(xiàn)對特定列表進行從小到大排序操作,涉及Python文件讀取、計算、正則匹配、排序等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
Python答題卡識別并給出分數(shù)的實現(xiàn)代碼
本文帶領(lǐng)大家學習Python答題卡識別并給出分數(shù)的實現(xiàn)代碼,代碼實現(xiàn)思路清晰,簡單易懂,Python識別答題卡相關(guān)知識感興趣的朋友一起看看吧2021-06-06
詳解Python調(diào)用華為API實現(xiàn)圖像標簽
華為云圖像標簽可識別上千種通用物體以及數(shù)百種場景標簽,一個圖像可包含多個標簽內(nèi)容,語義內(nèi)容非常豐富。本文將通過Python調(diào)用華為API實現(xiàn)圖像標簽,需要的可以參考一下2022-04-04
動態(tài)規(guī)劃之矩陣連乘問題Python實現(xiàn)方法
這篇文章主要介紹了動態(tài)規(guī)劃之矩陣連乘問題Python實現(xiàn)方法,較為詳細的分析了矩陣連乘問題的概念、原理并結(jié)合實例形式分析了Python相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-11-11

