对生成的pyqt5文件再次清洗

前言

这里的pyqt5文件,意思是指一个ui文件转换而成的py文件。

用Qt Designer设计窗口,生成ui文件,如果要把ui文件转换成py文件

要么通过python的命令,要么通过PyUic,但无论如何,其实我对转换出来的py文件,并不是很

满意,看着很不爽。

希望

用的pycharm的专业版,按ctrl+r,就会出现替换功能(好像社区版没有,我忘记了)😛😛😛😛,

如图

每次写窗体。生成的py文件,都是通过这个替换功能进行清洗,该修改的修改,改删除的删除。

很明显,替换功能的实现,就是通过re,正则表达式,

我希望写个类,一运行,生成我所希望的pyqt5文件,就不需要慢慢替换了。

正文

(因为情况特殊)

没有在代码里面写注释,代码里面没有注释,没有注释的

代码里面类似注释的东西--要匹配的字符串,也不能修改,多个或者少个空格都会导致,清洗失败

import datetime
import re
class Clear_py:
    def __init__(self,title,class_name):
        self.title=title
        self.class_name=class_name
    def start(self,path,rename):
        with open(path) as f:
            data=f.read()
        na=path.split('.')[0]
        top=f"""
# Form implementation generated from reading ui file '{na}.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
"""
        a=re.sub(top,'',data)
        widget="""QtWidgets\."""
        b=re.sub(widget,'',a)
        widget="""QtCore\."""
        b=re.sub(widget,'',b)
        widget="""QtGui\."""
        b=re.sub(widget,'',b)
        kuohao="""QRect\("""
        c=re.sub(kuohao,'',b)
        name="""(.*?).setObjectName\(.*?\)"""
        d=re.sub(name,'',c)
        e=re.findall('def setupUi\(self, (.*)\):',d)[0]
        if e=='Dialog':
            f=re.sub(e,'self',d)
        elif e=='Form':
            f=re.sub(e,'self',d)
        trans="""_translate\(\"self\"\, """
        g=re.sub(trans,'',f)
        other="""
        self\.retranslateUi\(self\)
        QMetaObject\.connectSlotsByName\(self\)

    def retranslateUi\(self\, self\)\:
        _translate \= QCoreApplication\.translate
"""
        h=re.sub(other,'',g)
        poc="""
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_self\(object\)\:
    def setupUi\(self, self\)\:
"""
        place=f"""
\"\"\"
文件:{rename}
创建者:QE
时间:{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
诗: 鲸鱼安慰了大海
            -燕七
    不是所有的树
    都能在自己的家乡终老
    不是所有的轨道
    都通往春暖花开的方向
    不是所有约定的人
    都会到来
    我知道,是流星赞美了黑夜
    鲸鱼安慰了大海
\"\"\"
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class {self.class_name}(QWidget):
    def __init__(self):
        super().__init__()
        self.initui()
    def initui(self):
        """
        i=re.sub(poc,place,h)
        right="""\)\)"""
        j=re.sub(right,')',i)
        start=f"""
if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle(QStyleFactory.create('Fusion'))
    a = {self.class_name}()
    a.show()
    sys.exit(app.exec_())
        """
        end=j+start
        window="""        self.setWindowTitle\(\"self\"\)"""
        title=f"""        self.setWindowTitle("{self.title}")"""
        end=re.sub(window,title,end)
        with open(rename,'w') as f:
            f.write(end)

参数的说明

”“”
   init方法
title,是指的窗体的名字
class_name 类的名字
   start 方法
path ui生成的py文件
rename  重用名的py文件(1.py)
中间有许多abcd等,都是变量名,按照字符顺序取的(0.0)(0.0)
再次说明-----正文里面没有注释
“”“

操作

随便拉些控件

生成ui,去个名,叫ok.ui,通过PyUic转换成生成ok.py

ok.py复制过来,根本不想看,用上面的类再次清洗

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ok.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(702, 577)
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(120, 320, 113, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.spinBox = QtWidgets.QSpinBox(Form)
        self.spinBox.setGeometry(QtCore.QRect(200, 30, 46, 22))
        self.spinBox.setObjectName("spinBox")
        self.treeView = QtWidgets.QTreeView(Form)
        self.treeView.setGeometry(QtCore.QRect(40, 70, 256, 192))
        self.treeView.setObjectName("treeView")
        self.toolButton = QtWidgets.QToolButton(Form)
        self.toolButton.setGeometry(QtCore.QRect(310, 90, 47, 21))
        self.toolButton.setObjectName("toolButton")
        self.horizontalScrollBar = QtWidgets.QScrollBar(Form)
        self.horizontalScrollBar.setGeometry(QtCore.QRect(510, 40, 160, 16))
        self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalScrollBar.setObjectName("horizontalScrollBar")
        self.line = QtWidgets.QFrame(Form)
        self.line.setGeometry(QtCore.QRect(230, 380, 118, 3))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.line_2 = QtWidgets.QFrame(Form)
        self.line_2.setGeometry(QtCore.QRect(120, 460, 3, 61))
        self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        self.graphicsView = QtWidgets.QGraphicsView(Form)
        self.graphicsView.setGeometry(QtCore.QRect(400, 80, 256, 192))
        self.graphicsView.setObjectName("graphicsView")
        self.treeView_2 = QtWidgets.QTreeView(Form)
        self.treeView_2.setGeometry(QtCore.QRect(20, 350, 256, 192))
        self.treeView_2.setObjectName("treeView_2")
        self.comboBox = QtWidgets.QComboBox(Form)
        self.comboBox.setGeometry(QtCore.QRect(380, 40, 87, 22))
        self.comboBox.setObjectName("comboBox")
        self.spinBox_2 = QtWidgets.QSpinBox(Form)
        self.spinBox_2.setGeometry(QtCore.QRect(40, 310, 46, 22))
        self.spinBox_2.setObjectName("spinBox_2")
        self.calendarWidget = QtWidgets.QCalendarWidget(Form)
        self.calendarWidget.setGeometry(QtCore.QRect(330, 290, 296, 236))
        self.calendarWidget.setObjectName("calendarWidget")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.toolButton.setText(_translate("Form", "..."))

运行代码

Clear_py('窗体','a').start('ok.py','1.py')
# 窗口的名字叫窗体,类名叫,a
# ok文件。重命名为1.py

运行后的1.py

# -*- coding: utf-8 -*-


"""
文件:1.py
创建者:QE
时间:2023-01-30 23:24:38
诗: 鲸鱼安慰了大海
            -燕七
    不是所有的树
    都能在自己的家乡终老
    不是所有的轨道
    都通往春暖花开的方向
    不是所有约定的人
    都会到来
    我知道,是流星赞美了黑夜
    鲸鱼安慰了大海
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class a(QWidget):
    def __init__(self):
        super().__init__()
        self.initui()
    def initui(self):
        
        self.resize(702, 577)
        self.lineEdit = QLineEdit(self)
        self.lineEdit.setGeometry(120, 320, 113, 21)

        self.spinBox = QSpinBox(self)
        self.spinBox.setGeometry(200, 30, 46, 22)

        self.treeView = QTreeView(self)
        self.treeView.setGeometry(40, 70, 256, 192)

        self.toolButton = QToolButton(self)
        self.toolButton.setGeometry(310, 90, 47, 21)

        self.horizontalScrollBar = QScrollBar(self)
        self.horizontalScrollBar.setGeometry(510, 40, 160, 16)
        self.horizontalScrollBar.setOrientation(Qt.Horizontal)

        self.line = QFrame(self)
        self.line.setGeometry(230, 380, 118, 3)
        self.line.setFrameShape(QFrame.HLine)
        self.line.setFrameShadow(QFrame.Sunken)

        self.line_2 = QFrame(self)
        self.line_2.setGeometry(120, 460, 3, 61)
        self.line_2.setFrameShape(QFrame.VLine)
        self.line_2.setFrameShadow(QFrame.Sunken)

        self.graphicsView = QGraphicsView(self)
        self.graphicsView.setGeometry(400, 80, 256, 192)

        self.treeView_2 = QTreeView(self)
        self.treeView_2.setGeometry(20, 350, 256, 192)

        self.comboBox = QComboBox(self)
        self.comboBox.setGeometry(380, 40, 87, 22)

        self.spinBox_2 = QSpinBox(self)
        self.spinBox_2.setGeometry(40, 310, 46, 22)

        self.calendarWidget = QCalendarWidget(self)
        self.calendarWidget.setGeometry(330, 290, 296, 236)

        self.setWindowTitle("窗体")
        self.toolButton.setText("...")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle(QStyleFactory.create('Fusion'))
    a = a()
    a.show()
    sys.exit(app.exec_())

你直接复制,如果你有pyqt5,且版本没有太大变化,绝对可以运行。没有问题。

如图

总结

你可以根据自己的情况进行修改正文的类,生成你所希望的文件。

当然,数据还可以在修改,继续用re,慢慢来

😛😛😛😛😛😛

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值