myjgptoico.py 这个是一个UI转换过来的文件,命名不太规范。
#myjgptoico.py
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'myjgptoico.ui'
##
## Created by: Qt User Interface Compiler version 6.1.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import * # type: ignore
from PySide6.QtGui import * # type: ignore
from PySide6.QtWidgets import * # type: ignore
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(542, 441)
self.widget = QWidget(Form)
self.widget.setObjectName(u"widget")
self.widget.setGeometry(QRect(150, 90, 258, 225))
self.verticalLayout = QVBoxLayout(self.widget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.pushButton = QPushButton(self.widget)
self.pushButton.setObjectName(u"pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.label = QLabel(self.widget)
self.label.setObjectName(u"label")
self.verticalLayout.addWidget(self.label, 0, Qt.AlignHCenter)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
self.pushButton.setText(QCoreApplication.translate("Form", u"\u6587\u4ef6\u9009\u62e9", None))
#if QT_CONFIG(whatsthis)
self.label.setWhatsThis(QCoreApplication.translate("Form", u"<html><head/><body><p>\u8fd9\u662f\u4ec0\u4e48</p></body></html>", None))
#endif // QT_CONFIG(whatsthis)
self.label.setText(QCoreApplication.translate("Form", u"\u8fd9\u91cc\u8f93\u51fa\u6587\u4ef6\u8def\u5f84", None))
# retranslateUi
#JPG2ICO copy.py
# from PyQt5.Qt import *
from PySide6.QtWidgets import QApplication, QMainWindow, QFileDialog,QWidget,QPushButton, QLabel
import sys , os
from PySide6.QtCore import Slot
from myjgptoico import *
@Slot()
def say_hello():
print("Button clicked, Hello!")
class MyWindow(QMainWindow, Ui_Form):
def __init__(self, parent=None):
# 继承自己设计的Ui界面
super(MyWindow, self).__init__(parent)
self.setupUi(self)
# 调用自己的函数补充ui界面
# self.initUi()
self.setWindowTitle(QCoreApplication.translate("Form", u"QFileDialog文件选择对话框 - 图片转ico图标工具 by chunk", None))
# QMainWindow.setObjectName(u"QFileDialog文件选择对话框 - 图片转ico图标工具 by chunk")
self.pushButton.clicked.connect(self.print_filename)
def print_filename(self):
FileDialog = QFileDialog(self, '文件选择', './', 'jpg(*.jpg *.jpeg);;png(*.png);;python(*.py)')
FileDialog.setAcceptMode(QFileDialog.AcceptOpen)
FileDirectory = FileDialog.getOpenFileNames(FileDialog,'请选择你要处理的图片','./', 'jpg(*.jpg *.jpeg);;png(*.png);;python(*.py)')
# print(f"{FileDirectory}")
# print(list(FileDirectory[0]))
FileDirectory = str(FileDirectory[0])
FileName = FileDirectory.replace('[','').replace(']','').replace('\'','')
self.label.setText(FileName)
fname,fename = os.path.splitext(FileName)
fname = fname + '.ico'
from jpg2ico import jpg_to_ico
# print(str(os.path.splitext(FileName)) + 'ico666')
jpg_to_ico(FileName,fname)
# Create the Qt Application
app = QApplication(sys.argv)
# Create a button, connect it and show it
# button = QPushButton("Click me")
# button.move(200, 200)
# button.setText('打开对话框')
# button.resize(100, 30)
# button.clicked.connect(say_hello)
# button.show()
myWin = MyWindow()
# 展示界面
myWin.show()
# Run the main Qt loop
app.exec()
打开文件选择,选择需要转换的图片就可以转换为ico了。
这次是续这上次的python脚本套上了pyqt外壳GUI。
基本思路是:
- 使用pyqt设计器设计UI
- 转换UI文件为py脚本
- 主脚本导入UI py的脚本模块