使用Python写的WingPro7 Pyside2 和 PyQt5插件
阅读原文时间:2023年07月10日阅读:1

pyside2的

import wingapi
import subprocess

pyside2_uic = "pyside2-uic"
pyside2_qrc = "pyside2-rcc"

def Pyside2_uic():
count_wait = 0
count_done = 0
for i in wingapi.gApplication.GetCurrentFiles():
if ".ui" in i:
count_wait += 1
p = subprocess.Popen([pyside2_uic, i, "-o", i.replace(".ui", "_ui.py"), "-x"])
p.wait()
if p.returncode == 0:
count_done += 1
else:
print(i.split("\\")[-1] + " FAILED")
print("{0} ui files need to be converted.".format(count_wait))
print("{0} SUCESSFULLY".format(count_done))

def Pyside2_rcc():
count_wait = 0
count_done = 0
for i in wingapi.gApplication.GetCurrentFiles():
if ".qrc" in i:
print(i)
count_wait += 1
p = subprocess.Popen([pyside2_qrc, i, "-o", i.replace(".qrc", "_rc.py")])
p.wait()
if p.returncode == 0:
count_done += 1
else:
print(i.split("\\")[-1] + " FAILED")
print("{0} qrc files need to be converted.".format(count_wait))
print("{0} SUCESSFULLY".format(count_done))

def Pyside2_pcq():
def readUi(uiPath):
import xml.dom.minidom as xmldom
uiDict = {}
domObj = xmldom.parse(uiPath)
elementObj = domObj.documentElement
windowName = elementObj.getElementsByTagName("widget")
uiDict["class"] = windowName[0].getAttribute("class")
uiDict["name"] = "Ui_" + windowName[0].getAttribute("name")
uiDict["connections"] = []
connections = elementObj.getElementsByTagName("connection")
for i in connections:
connection = {}
sender = i.getElementsByTagName("sender")
connection["sender"] = sender[0].firstChild.data
signal = i.getElementsByTagName("signal")
connection["signal"] = signal[0].firstChild.data
receiver = i.getElementsByTagName("receiver")
connection["receiver"] = "Ui_" + receiver[0].firstChild.data
slot = i.getElementsByTagName("slot")
connection["slot"] = slot[0].firstChild.data
uiDict["connections"].append(connection)
return uiDict

def logUi(uiPath, uiDict):  
    from datetime import datetime  
    with open(uiPath.replace(".ui", ".log"), "w") as f:  
        content = \[\]  
        content.extend(\["Updated Time:" + str(datetime.now()) + "\\n\\n"\])  
        content.extend(\["class=" + uiDict\["class"\] + "\\n"\])  
        content.extend(\["name =" + uiDict\["name"\] + "\\n"\])  
        for i in uiDict\["connections"\]:  
            content.extend(\["\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\\n"\])  
            content.extend(\["sender :" + i\["sender"\] + "\\n"\])  
            content.extend(\["signal :" + i\["signal"\] + "\\n"\])  
            content.extend(\["receiver :" + i\["receiver"\] + "\\n"\])  
            content.extend(\["slot :" + i\["slot"\] + "\\n"\])  
        f.write("".join(content))

def generatePy(uiPath, uiDict):

    def importGen():  
        from datetime import datetime  
        return("# -\*- coding: utf-8 -\*-\\n"  
               "#Generated by \[pyside2 pcq\]\\n"  
               "#Created By Lulu\\n"  
               "#Updated Time:{2}\\n"  
               "#WARNING! All changes made in this file will be lost!\\n"  
               "from PySide2 import QtWidgets\\n"  
               "from {0} import {1} as Parent"  
               "\\n\\n".format(uiPath.split("\\\\")\[-1\].replace(".ui", "\_ui"), uiDict\["name"\], datetime.now()))

    def classGen():  
        return("class {0}(QtWidgets.{1},Parent):\\n"  
               "\\n"  
               "    def \_\_init\_\_(self):\\n"  
               "        '''Constructor'''\\n"  
               "        super().\_\_init\_\_()\\n"  
               "        self.setupUi(self)\\n"  
               "        \\n\\n".format(uiDict\["name"\].replace("Ui\_", "Win\_"), uiDict\["class"\]))

    def slotGen():  
        slotContent = \[\]  
        slots = \[\]  
        for i in uiDict\["connections"\]:  
            slots.extend(\[i\["slot"\]\])  
        slots = list(set(slots))  
        for i in slots:  
            slotContent.extend(\[  
                "    def {0}(self):\\n"  
                "        \\n"  
                "        pass\\n\\n".format(i\[:-2\])\])  
        return "".join(slotContent)

    def mainGen():  
        return(  
            'if \_\_name\_\_ == "\_\_main\_\_": \\n'  
            '    import sys\\n'  
            '    app = QtWidgets.QApplication(sys.argv)\\n'  
            '    {0} = {1}()\\n'  
            '    {0}.show()\\n'  
            '    sys.exit(app.exec\_())\\n'.format(uiDict\["name"\].replace("Ui\_", "Win\_").lower(), uiDict\["name"\].replace("Ui\_", "Win\_")))  
    with open(uiPath.replace(".ui", "\_Win.py"), "w") as f:  
        f.write(importGen() + classGen() + slotGen() + mainGen())

for i in wingapi.gApplication.GetCurrentFiles():  
    if ".ui" in i:  
        uiDict = readUi(i)  
        logUi(i, uiDict)  
        generatePy(i, uiDict)

Pyside2_uic.contexts = [
wingapi.kContextProject(),
]
Pyside2_rcc.contexts = [
wingapi.kContextProject(),
]
Pyside2_pcq.contexts = [
wingapi.kContextProject(),
]

pyqt5的

import wingapi
import subprocess

pyuic5 = "pyuic5"
pyrcc5 = "pyrcc5"

def PyQt5_uic():
count_wait = 0
count_done = 0
for i in wingapi.gApplication.GetCurrentFiles():
if ".ui" in i:
count_wait += 1
p = subprocess.Popen([pyuic5, i, "-o", i.replace(".ui", "_ui.py"), "-x"])
p.wait()
if p.returncode == 0:
count_done += 1
else:
print(i.split("\\")[-1] + " FAILED")
print("{0} ui files need to be converted.".format(count_wait))
print("{0} SUCESSFULLY".format(count_done))

def PyQt5_rcc():
count_wait = 0
count_done = 0
for i in wingapi.gApplication.GetCurrentFiles():
if ".qrc" in i:
print(i)
count_wait += 1
p = subprocess.Popen([pyrcc5, i, "-o", i.replace(".qrc", "_rc.py")])
p.wait()
if p.returncode == 0:
count_done += 1
else:
print(i.split("\\")[-1] + " FAILED")
print("{0} qrc files need to be converted.".format(count_wait))
print("{0} SUCESSFULLY".format(count_done))

def PyQt5_pcq():
def readUi(uiPath):
import xml.dom.minidom as xmldom
uiDict = {}
domObj = xmldom.parse(uiPath)
elementObj = domObj.documentElement
windowName = elementObj.getElementsByTagName("widget")
uiDict["class"] = windowName[0].getAttribute("class")
uiDict["name"] = "Ui_" + windowName[0].getAttribute("name")
uiDict["connections"] = []
connections = elementObj.getElementsByTagName("connection")
for i in connections:
connection = {}
sender = i.getElementsByTagName("sender")
connection["sender"] = sender[0].firstChild.data
signal = i.getElementsByTagName("signal")
connection["signal"] = signal[0].firstChild.data
receiver = i.getElementsByTagName("receiver")
connection["receiver"] = "Ui_" + receiver[0].firstChild.data
slot = i.getElementsByTagName("slot")
connection["slot"] = slot[0].firstChild.data
uiDict["connections"].append(connection)
return uiDict

def logUi(uiPath, uiDict):  
    from datetime import datetime  
    with open(uiPath.replace(".ui", ".log"), "w") as f:  
        content = \[\]  
        content.extend(\["Updated Time:" + str(datetime.now()) + "\\n\\n"\])  
        content.extend(\["class=" + uiDict\["class"\] + "\\n"\])  
        content.extend(\["name =" + uiDict\["name"\] + "\\n"\])  
        for i in uiDict\["connections"\]:  
            content.extend(\["\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\\n"\])  
            content.extend(\["sender :" + i\["sender"\] + "\\n"\])  
            content.extend(\["signal :" + i\["signal"\] + "\\n"\])  
            content.extend(\["receiver :" + i\["receiver"\] + "\\n"\])  
            content.extend(\["slot :" + i\["slot"\] + "\\n"\])  
        f.write("".join(content))

def generatePy(uiPath, uiDict):

    def importGen():  
        from datetime import datetime  
        return("# -\*- coding: utf-8 -\*-\\n"  
               "#Generated by \[PyQt5 pcq\]\\n"  
               "#Created By Lulu\\n"  
               "#Updated Time:{2}\\n"  
               "#WARNING! All changes made in this file will be lost!\\n"  
               "from PyQt5 import QtWidgets\\n"  
               "from {0} import {1} as Parent"  
               "\\n\\n".format(uiPath.split("\\\\")\[-1\].replace(".ui", "\_ui"), uiDict\["name"\], datetime.now()))

    def classGen():  
        return("class {0}(QtWidgets.{1},Parent):\\n"  
               "\\n"  
               "    def \_\_init\_\_(self):\\n"  
               "        '''Constructor'''\\n"  
               "        super().\_\_init\_\_()\\n"  
               "        self.setupUi(self)\\n"  
               "        \\n\\n".format(uiDict\["name"\].replace("Ui\_", "Win\_"), uiDict\["class"\]))

    def slotGen():  
        slotContent = \[\]  
        slots = \[\]  
        for i in uiDict\["connections"\]:  
            slots.extend(\[i\["slot"\]\])  
        slots = list(set(slots))  
        for i in slots:  
            slotContent.extend(\[  
                "    def {0}(self):\\n"  
                "        \\n"  
                "        pass\\n\\n".format(i\[:-2\])\])  
        return "".join(slotContent)

    def mainGen():  
        return(  
            'if \_\_name\_\_ == "\_\_main\_\_": \\n'  
            '    import sys\\n'  
            '    app = QtWidgets.QApplication(sys.argv)\\n'  
            '    {0} = {1}()\\n'  
            '    {0}.show()\\n'  
            '    sys.exit(app.exec\_())\\n'.format(uiDict\["name"\].replace("Ui\_", "Win\_").lower(), uiDict\["name"\].replace("Ui\_", "Win\_")))  
    with open(uiPath.replace(".ui", "\_Win.py"), "w") as f:  
        f.write(importGen() + classGen() + slotGen() + mainGen())

for i in wingapi.gApplication.GetCurrentFiles():  
    if ".ui" in i:  
        uiDict = readUi(i)  
        logUi(i, uiDict)  
        generatePy(i, uiDict)

PyQt5_uic.contexts = [
wingapi.kContextProject(),
]
PyQt5_rcc.contexts = [
wingapi.kContextProject(),
]
PyQt5_pcq.contexts = [
wingapi.kContextProject(),
]

代码分屏插件

import wingapi

def split_horizontally():
wingapi.gApplication.ExecuteCommand("split-horizontally")

def split_vertically():
wingapi.gApplication.ExecuteCommand("split-vertically")

def join_all_split():
wingapi.gApplication.ExecuteCommand("unsplit")

split_horizontally.contexts = [
wingapi.kContextEditor(),
]
split_vertically.contexts = [
wingapi.kContextEditor(),
]
join_all_split.contexts = [
wingapi.kContextEditor(),
]

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章