PythonQT5打包exe線程使用
打包:
pyinstaller --noconsole --onefile test.py
–noconsole 表示不需要打開命令行
修改:test.spec
一般項(xiàng)目里面需要用的資源文件,比如lib、png、exe等。
需要單獨(dú)修改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)建的臨時文件夾,或者當(dāng)以 --onefile 模式打包時的路徑
base_path = sys._MEIPASS
except Exception:
# 如果不是打包后的環(huán)境,則使用當(dāng)前工作目錄
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)實(shí)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
按鈕點(diǎn)擊方法帶入?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ā)送請求等等。計(jì)算復(fù)雜邏輯。。。
# 發(fā)送信號到主線程
self.data_received.emit([self.p1, '結(jié)果'])
except Exception as e:
# flush=True 會強(qiáng)制把日志刷到文件里面,不加的話,有時候不會寫入文件
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寫個自動SSH登錄遠(yuǎn)程服務(wù)器的小工具(實(shí)例)
下面小編就為大家?guī)硪黄胮ython寫個自動SSH登錄遠(yuǎn)程服務(wù)器的小工具(實(shí)例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Django框架實(shí)現(xiàn)在線考試系統(tǒng)的示例代碼
這篇文章主要介紹了Django框架實(shí)現(xiàn)在線考試系統(tǒng)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python中TypeError:unhashable?type:'dict'錯誤的解決辦法
這篇文章主要給大家介紹了關(guān)于Python中TypeError:unhashable?type:'dict'錯誤的解決辦法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-04-04

