pyinstaller打包——图片资源无法显示问题
pyinstaller -F -w xxx.py
import sys
import urllib
from json import JSONDecodeError
import requests
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class XZS(QWidget):
def __init__(self):
super(XZS, self).__init__()
self.resize(520,600)
self.center() #使窗口居中
self.setWindowTitle("小助手")
self.setWindowIcon(QIcon('1.jpg'))
font = QtGui.QFont()
font.setFamily("宋体")
self.textEdit = QLineEdit(self)
self.textEdit.move(60, 50)
self.textEdit.setFont(font)
self.textEdit2 = QTextEdit(self)
self.textEdit2.move(60, 100)
self.textEdit2.setFixedSize(300, 400)
self.textEdit2.setFont(font)
self.textEdit2.setFocusPolicy(QtCore.Qt.NoFocus)
self.label1 = QLabel(self)
self.label1.setFont(font)
self.label1.setText("城市天气查询")
# self.label1.setFixedSize(300, 400)
self.label1.move(50, 30)
btn_1 = QPushButton(self)
btn_1.setText("查询")
btn_1.move(250, 30)
btn_1.setFixedSize(100, 50)
btn_1.clicked.connect(self.look)
btn_2 = QPushButton(self)
btn_2.setText("退出")
btn_2.move(400, 30)
btn_2.setFixedSize(100, 50)
btn_2.clicked.connect(QCoreApplication.quit)
def look(self):
city = self.textEdit.text()
print(city)
html = f'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city)
try:
info = requests.get(html)
# print(info)
info.encoding = 'utf-8'
except requests.ConnectionError:
raise
try:
info_json = info.json()
# print(info_json)
except JSONDecodeError:
return '无法查询'
try:
for i in range(1): # 将每一天的数据放入列表中
# 天气情况
data = info_json['data']
city = f"城市:{data['city']}\n"
# 以 f开头表示在字符串内支持大括号内的python 表达式
today = data['forecast'][i]
date = f"日期:{today['date']}\n"
now = f"实时温度:{data['wendu']}度\n"
temperature = f"温度:{today['high']} {today['low']}\n"
fengxiang = f"风向:{today['fengxiang']}\n"
type = f"天气:{today['type']}\n"
tips = f"贴士:{data['ganmao']}\n"
self.textEdit2.setText(city + date + now + temperature + fengxiang + type + tips + "\n\n")
print(city + date + now + temperature + fengxiang + type + tips + "\n\n")
self.textEdit.clear()
except:
self.textEdit2.setText('请输入正确的城市名称')
self.textEdit.clear()
def center(self): # 控制窗口显示在屏幕中心的方法
# 获得窗口
qr = self.frameGeometry()
# 获得屏幕中心点
cp = QDesktopWidget().availableGeometry().center()
# 显示到屏幕中心
qr.moveCenter(cp)
self.move(qr.topLeft())
def keyPressEvent(self,e):
# 设置快捷键
if e.key() == Qt.Key_Return:
self.look()
# if event.key() == QtCore.Qt.Key_Escape: # 当我们按住键盘是esc按键时
# self.close() # 关闭程序
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
my = XZS()
my.show()
sys.exit(app.exec_())
picpy.py
将图像文件转换为py文件
# -*- coding: utf-8 -*-
import base64
def pic2py(picture_name):
"""
将图像文件转换为py文件
:param picture_name:
:return:
"""
open_pic = open("%s" % picture_name, 'rb')
b64str = base64.b64encode(open_pic.read())
open_pic.close()
# 注意这边b64str一定要加上.decode()
write_data = 'img = "%s"' % b64str.decode()
f = open('%s.py' % picture_name.replace('.', '_'), 'w+')
f.write(write_data)
f.close()
if __name__ == '__main__':
pics = ["Gitee.png"]
for i in pics:
pic2py(i)
print("完成^_^")
import base64
import os
import sys
import urllib
from json import JSONDecodeError
import requests
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from Gitee_png import img as one
class XZS(QWidget):
def __init__(self):
super(XZS, self).__init__()
self.resize(520,600)
self.center() #使窗口居中
self.setWindowTitle("小助手")
self.setWindowIcon(QIcon('favicons.png'))
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QPixmap("favicons.png")))
self.setPalette(palette)
font = QtGui.QFont()
font.setFamily("宋体")
self.textEdit = QLineEdit(self)
self.textEdit.move(60, 50)
self.textEdit.setFont(font)
self.textEdit2 = QTextEdit(self)
self.textEdit2.move(60, 100)
self.textEdit2.setFixedSize(300, 400)
self.textEdit2.setFont(font)
self.textEdit2.setFocusPolicy(QtCore.Qt.NoFocus)
self.label1 = QLabel(self)
self.label1.setFont(font)
self.label1.setText("城市天气查询")
# self.label1.setFixedSize(300, 400)
self.label1.move(60, 30)
btn_1 = QPushButton(self)
btn_1.setText("查询")
btn_1.move(250, 30)
btn_1.setFixedSize(100, 50)
btn_1.clicked.connect(self.look)
btn_2 = QPushButton(self)
btn_2.setText("退出")
btn_2.move(400, 30)
btn_2.setFixedSize(100, 50)
btn_2.clicked.connect(QCoreApplication.quit)
def look(self):
city = self.textEdit.text()
print(city)
html = f'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city)
try:
info = requests.get(html)
# print(info)
info.encoding = 'utf-8'
except requests.ConnectionError:
raise
try:
info_json = info.json()
# print(info_json)
except JSONDecodeError:
return '无法查询'
try:
for i in range(1): # 将每一天的数据放入列表中
# 天气情况
data = info_json['data']
city = f"城市:{data['city']}\n"
# 以 f开头表示在字符串内支持大括号内的python 表达式
today = data['forecast'][i]
date = f"日期:{today['date']}\n"
now = f"实时温度:{data['wendu']}度\n"
temperature = f"温度:{today['high']} {today['low']}\n"
fengxiang = f"风向:{today['fengxiang']}\n"
type = f"天气:{today['type']}\n"
tips = f"贴士:{data['ganmao']}\n"
self.textEdit2.setText(city + date + now + temperature + fengxiang + type + tips + "\n\n")
print(city + date + now + temperature + fengxiang + type + tips + "\n\n")
self.textEdit.clear()
except:
self.textEdit2.setText('请输入正确的城市名称')
self.textEdit.clear()
def center(self): # 控制窗口显示在屏幕中心的方法
# 获得窗口
qr = self.frameGeometry()
# 获得屏幕中心点
cp = QDesktopWidget().availableGeometry().center()
# 显示到屏幕中心
qr.moveCenter(cp)
self.move(qr.topLeft())
def keyPressEvent(self,event):
# 设置快捷键
if event.key() == Qt.Key_Return:
self.look()
if event.key() == QtCore.Qt.Key_Escape: # 当我们按住键盘是esc按键时
os.remove('favicons.png') # 用完可以删除这个临时图片
self.close() # 关闭程序
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
tmp = open('favicons.png', 'wb') # 创建临时的文件
tmp.write(base64.b64decode(one)) ##把这个one图片解码出来,写入文件中去。
tmp.close()
my = XZS()
my.show()
os.remove('favicons.png') # 用完可以删除这个临时图片
sys.exit(app.exec_())