清明疫情期间,在家炼金日记(python打造类似jenkins的任务控制脚本,基于pyQt5)

1,118 阅读7分钟

0.什么是PyQt?

PyQt是Python重新实现了一遍Qt的功能,在实现的时候, 几乎保持了全部原有的API,学习完PyQt之后, 只要掌握C++语法之后, 就可以快速的接手Qt的使用。

1. why pyQt5

跨平台,易搭建,如果我windows写的代码,现在要在linux,mac上面跑,那首先C#就做不到,因为C#的本质是有几个windows的核心库,所以开发windows应用可能是利剑,但是跨平台能力难办到,windows的dll是不可能用在其他操作系统上面的。

2.通过pip把依赖库都给导入(拿来吧你)

pyQt是基于Qt存在的,所以这里下载的其实是Qt的那些依赖,在使用Qt的功能

这个是PyQt5的库

pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple

这个是PyQt5的编辑包,应该是可以拖动ui可视化编辑,但是我不用,我觉得python里直接构思ui更酷

pip install PyQt5-tools -i https://pypi.tuna.tsinghua.edu.cn/simple

3.一个什么都没有的ui界面例子

from PyQt5 import QtWidgets
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    w.resize(400, 200)
    w.setWindowTitle("任务控制平台")
    w.show()
    exit(app.exec_())

图片.png

resize:长*宽 setWindowTitle:标题文字 show():实例化窗口对象 exit(app.exec_()):提供内置的点击叉结束该程序线程事件

4.有点东西的ui界面例子

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication


class MainWin(QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()

        self.setWindowTitle("抓取工具")

        self.resize(400, 300)
        self.status = self.statusBar()
        self.status.showMessage('抓取工具')


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWin()
    mainWin.show()

    sys.exit(app.exec_())

图片.png

5.中间有标题的ui界面例子

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow


class MainWin(QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()

        self.setWindowTitle("任务控制平台")

        self.resize(400, 300)
        self.status = self.statusBar()
        self.status.showMessage('当前无任务')

        self.centralWidget = QLabel("当前无可用设备")
        self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.setCentralWidget(self.centralWidget)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWin()
    mainWin.show()
    sys.exit(app.exec_())

图片.png

6.事件的开端,添加工具栏

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt5.QtWidgets import QToolBar
from PyQt5.QtWidgets import QMenu



class MainWin(QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()

        self.setWindowTitle("任务控制平台")

        self.resize(400, 300)
        self.status = self.statusBar()
        self._createToolBars()
        self._createMenuBar()
        self.status.showMessage('当前无任务')

        self.centralWidget = QLabel("当前无可用设备")
        self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.setCentralWidget(self.centralWidget)
        
    def _createToolBars(self):
        # Using a title
        fileToolBar = self.addToolBar("File")
        # Using a QToolBar object
        editToolBar = QToolBar("Edit", self)
        self.addToolBar(editToolBar)
        # Using a QToolBar object and a toolbar area
        helpToolBar = QToolBar("Help", self)
        self.addToolBar(Qt.LeftToolBarArea, helpToolBar)
        
    def _createMenuBar(self):
        menuBar = self.menuBar()
        # Creating menus using a QMenu object
        fileMenu = QMenu("&File", self)
        menuBar.addMenu(fileMenu)
        # Creating menus using a title
        editMenu = menuBar.addMenu("&Edit")
        helpMenu = menuBar.addMenu("&Help")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWin()
    mainWin.show()

    sys.exit(app.exec_())

图片.png

7.添加下拉列表

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt5.QtWidgets import QToolBar
from PyQt5.QtWidgets import QMenu
from PyQt5.QtWidgets import QAction



class MainWin(QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()

        self.setWindowTitle("任务控制平台")

        self.resize(400, 300)
        self.status = self.statusBar()
        self._createActions()
        self._createToolBars()
        self._createMenuBar()
        self.status.showMessage('当前无任务')

        self.centralWidget = QLabel("当前无可用设备")
        self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.setCentralWidget(self.centralWidget)
        
    def _createToolBars(self):
        # Using a title
        fileToolBar = self.addToolBar("File")
        # Using a QToolBar object
        editToolBar = QToolBar("Edit", self)
        self.addToolBar(editToolBar)
        # Using a QToolBar object and a toolbar area
        helpToolBar = QToolBar("Help", self)
        self.addToolBar(Qt.LeftToolBarArea, helpToolBar)
        
    def _createMenuBar(self):
        menuBar = self.menuBar()
        # File menu
        fileMenu = QMenu("&File", self)
        menuBar.addMenu(fileMenu)
        fileMenu.addAction(self.newAction)
        fileMenu.addAction(self.openAction)
        fileMenu.addAction(self.saveAction)
        fileMenu.addAction(self.exitAction)
        # Edit menu
        editMenu = menuBar.addMenu("&Edit")
        editMenu.addAction(self.copyAction)
        editMenu.addAction(self.pasteAction)
        editMenu.addAction(self.cutAction)
        # Help menu
        helpMenu = menuBar.addMenu("&Help")
        helpMenu.addAction(self.helpContentAction)
        helpMenu.addAction(self.aboutAction)

    def _createActions(self):
        # Creating action using the first constructor
        self.newAction = QAction(self)
        self.newAction.setText("&New")
        # Creating actions using the second constructor
        self.openAction = QAction("&Open...", self)
        self.saveAction = QAction("&Save", self)
        self.exitAction = QAction("&Exit", self)
        self.copyAction = QAction("&Copy", self)
        self.pasteAction = QAction("&Paste", self)
        self.cutAction = QAction("C&ut", self)
        self.helpContentAction = QAction("&Help Content", self)
        self.aboutAction = QAction("&About", self)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWin()
    mainWin.show()

    sys.exit(app.exec_())

图片.png

8.添加按钮,通过信号槽,实现关闭事件

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *



class MainWin(QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()

        self.setWindowTitle("任务控制平台")

        self.resize(400, 300)
        self.status = self.statusBar()
        self._createActions()
        self._createToolBars()
        self._createMenuBar()
        self.status.showMessage('当前无任务')

        #self.centralWidget = QLabel("当前无可用设备")
        #self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        #self.setCentralWidget(self.centralWidget)
        self.setup_ui()
    def setup_ui(self):
        btn = QPushButton(self)
        btn.move(50,50)
        btn.resize(200,30)
        btn.setText('笔记本设备1(点击关闭)')
        btn.clicked.connect(self.close)
        
        
    def _createToolBars(self):
        # Using a title
        fileToolBar = self.addToolBar("File")
        # Using a QToolBar object
        editToolBar = QToolBar("Edit", self)
        self.addToolBar(editToolBar)
        # Using a QToolBar object and a toolbar area
        helpToolBar = QToolBar("Help", self)
        self.addToolBar(Qt.LeftToolBarArea, helpToolBar)
        
    def _createMenuBar(self):
        menuBar = self.menuBar()
        # File menu
        fileMenu = QMenu("&File", self)
        menuBar.addMenu(fileMenu)
        fileMenu.addAction(self.newAction)
        fileMenu.addAction(self.openAction)
        fileMenu.addAction(self.saveAction)
        fileMenu.addAction(self.exitAction)
        # Edit menu
        editMenu = menuBar.addMenu("&Edit")
        editMenu.addAction(self.copyAction)
        editMenu.addAction(self.pasteAction)
        editMenu.addAction(self.cutAction)
        # Help menu
        helpMenu = menuBar.addMenu("&Help")
        helpMenu.addAction(self.helpContentAction)
        helpMenu.addAction(self.aboutAction)

    def _createActions(self):
        # Creating action using the first constructor
        self.newAction = QAction(self)
        self.newAction.setText("&New")
        # Creating actions using the second constructor
        self.openAction = QAction("&Open...", self)
        self.saveAction = QAction("&Save", self)
        self.exitAction = QAction("&Exit", self)
        self.copyAction = QAction("&Copy", self)
        self.pasteAction = QAction("&Paste", self)
        self.cutAction = QAction("C&ut", self)
        self.helpContentAction = QAction("&Help Content", self)
        self.aboutAction = QAction("&About", self)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWin()
    mainWin.show()

    sys.exit(app.exec_())

图片.png

9.让按钮飞一会儿

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *



class MainWin(QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()

        self.setWindowTitle("任务控制平台")

        self.resize(400, 300)
        self.status = self.statusBar()
        self._createActions()
        self._createToolBars()
        self._createMenuBar()
        self.status.showMessage('当前无任务')

        #self.centralWidget = QLabel("当前无可用设备")
        #self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        #self.setCentralWidget(self.centralWidget)
        self.setup_ui()
    def setup_ui(self):
        btn = QPushButton(self)
        btn.move(50,50)
        btn.resize(200,30)
        btn.setText('笔记本设备1(点击关闭)')
        btn.clicked.connect(self.close)

        btn2 = QPushButton(self)
        btn2.move(50,100)
        btn2.resize(200,30)
        btn2.setText('笔记本设备1(点击关闭)')
        btn2.clicked.connect(self.close)

        btn2 = QPushButton(self)
        btn2.move(50,150)
        btn2.resize(200,30)
        btn2.setText('隔壁老王家笔记本设备1')
        btn2.clicked.connect(self.close)

        
        
        
    def _createToolBars(self):
        # Using a title
        fileToolBar = self.addToolBar("File")
        # Using a QToolBar object
        editToolBar = QToolBar("Edit", self)
        self.addToolBar(editToolBar)
        # Using a QToolBar object and a toolbar area
        helpToolBar = QToolBar("Help", self)
        self.addToolBar(Qt.LeftToolBarArea, helpToolBar)
        
    def _createMenuBar(self):
        menuBar = self.menuBar()
        # File menu
        fileMenu = QMenu("&File", self)
        menuBar.addMenu(fileMenu)
        fileMenu.addAction(self.newAction)
        fileMenu.addAction(self.openAction)
        fileMenu.addAction(self.saveAction)
        fileMenu.addAction(self.exitAction)
        # Edit menu
        editMenu = menuBar.addMenu("&Edit")
        editMenu.addAction(self.copyAction)
        editMenu.addAction(self.pasteAction)
        editMenu.addAction(self.cutAction)
        # Help menu
        helpMenu = menuBar.addMenu("&Help")
        helpMenu.addAction(self.helpContentAction)
        helpMenu.addAction(self.aboutAction)

    def _createActions(self):
        # Creating action using the first constructor
        self.newAction = QAction(self)
        self.newAction.setText("&New")
        # Creating actions using the second constructor
        self.openAction = QAction("&Open...", self)
        self.saveAction = QAction("&Save", self)
        self.exitAction = QAction("&Exit", self)
        self.copyAction = QAction("&Copy", self)
        self.pasteAction = QAction("&Paste", self)
        self.cutAction = QAction("C&ut", self)
        self.helpContentAction = QAction("&Help Content", self)
        self.aboutAction = QAction("&About", self)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWin()
    mainWin.show()

    sys.exit(app.exec_())

图片.png

10.通过事件打开游戏

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
import os




class MainWin(QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()

        self.setWindowTitle("任务控制平台")

        self.resize(400, 300)
        self.status = self.statusBar()
        self._createActions()
        self._createToolBars()
        self._createMenuBar()
        self.status.showMessage('当前无任务')

        #self.centralWidget = QLabel("当前无可用设备")
        #self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        #self.setCentralWidget(self.centralWidget)
        self.setup_ui()
    def setup_ui(self):
        btn = QPushButton(self)
        btn.move(50,50)
        btn.resize(200,30)
        btn.setText('笔记本设备1(点击打开挺进地牢)')
        btn.clicked.connect(self.button1_clicked)

        btn2 = QPushButton(self)
        btn2.move(50,100)
        btn2.resize(200,30)
        btn2.setText('笔记本设备2(点击关闭)')
        btn2.clicked.connect(self.close)

        btn2 = QPushButton(self)
        btn2.move(50,150)
        btn2.resize(200,30)
        btn2.setText('隔壁老王家笔记本设备1')
        btn2.clicked.connect(self.close)

    def button1_clicked(self):
        os.system('python openGame.py')
    def _createToolBars(self):
        # Using a title
        fileToolBar = self.addToolBar("File")
        # Using a QToolBar object
        editToolBar = QToolBar("Edit", self)
        self.addToolBar(editToolBar)
        # Using a QToolBar object and a toolbar area
        helpToolBar = QToolBar("Help", self)
        self.addToolBar(Qt.LeftToolBarArea, helpToolBar)
        
    def _createMenuBar(self):
        menuBar = self.menuBar()
        # File menu
        fileMenu = QMenu("&File", self)
        menuBar.addMenu(fileMenu)
        fileMenu.addAction(self.newAction)
        fileMenu.addAction(self.openAction)
        fileMenu.addAction(self.saveAction)
        fileMenu.addAction(self.exitAction)
        # Edit menu
        editMenu = menuBar.addMenu("&Edit")
        editMenu.addAction(self.copyAction)
        editMenu.addAction(self.pasteAction)
        editMenu.addAction(self.cutAction)
        # Help menu
        helpMenu = menuBar.addMenu("&Help")
        helpMenu.addAction(self.helpContentAction)
        helpMenu.addAction(self.aboutAction)

    def _createActions(self):
        # Creating action using the first constructor
        self.newAction = QAction(self)
        self.newAction.setText("&New")
        # Creating actions using the second constructor
        self.openAction = QAction("&Open...", self)
        self.saveAction = QAction("&Save", self)
        self.exitAction = QAction("&Exit", self)
        self.copyAction = QAction("&Copy", self)
        self.pasteAction = QAction("&Paste", self)
        self.cutAction = QAction("C&ut", self)
        self.helpContentAction = QAction("&Help Content", self)
        self.aboutAction = QAction("&About", self)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWin()
    mainWin.show()

    sys.exit(app.exec_())

import sys
import os

if __name__ == "__main__":
    print("ok")
    os.system(r'你的绝对路径\EtG.exe')  

图片.png

图片.png

玩了四年的游戏,除了巫妖王(需要四个角色真结局集齐后才能打)没打通几乎背出了每个boss和小兵的出招表,解锁了终极角色

图片.png

商店还差最后一件商品就卖空了,原来10星际币的东西涨到了200,不得不说被压榨了

图片.png

一些需要的改进点

1.需要过比例去调整布局,当前是固定数值,Ui发生改变则固定数值都需要调整
import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
import os


size_x = 800
size_y = 600

class MainWin(QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()

        self.setWindowTitle("任务控制平台")

        self.resize(size_x, size_y)
        self.status = self.statusBar()
        self._createActions()
        self._createToolBars()
        self._createMenuBar()
        self.status.showMessage('当前无任务')

        #self.centralWidget = QLabel("当前无可用设备")
        #self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        #self.setCentralWidget(self.centralWidget)
        self.setup_ui()
    def setup_ui(self):
        btn = QPushButton(self)
        btn.move(50,int(size_y/3*0+50))
        btn.resize(200,30)
        btn.setText('笔记本设备1(点击打开挺进地牢)')
        btn.clicked.connect(self.button1_clicked)
        
        btn2 = QPushButton(self)
        btn2.move(50,int(size_y/3*1+50))
        btn2.resize(200,30)
        btn2.setText('笔记本设备2(点击关闭)')
        btn2.clicked.connect(self.close)

        btn3 = QPushButton(self)
        btn3.move(50,int(size_y/3*2+50))
        btn3.resize(200,30)
        btn3.setText('隔壁老王家笔记本设备1')
        btn3.clicked.connect(self.close)

    def button1_clicked(self):
        os.system('python openGame.py')
    def _createToolBars(self):
        # Using a title
        fileToolBar = self.addToolBar("File")
        # Using a QToolBar object
        editToolBar = QToolBar("Edit", self)
        self.addToolBar(editToolBar)
        # Using a QToolBar object and a toolbar area
        helpToolBar = QToolBar("Help", self)
        self.addToolBar(Qt.LeftToolBarArea, helpToolBar)
        
    def _createMenuBar(self):
        menuBar = self.menuBar()
        # File menu
        fileMenu = QMenu("&File", self)
        menuBar.addMenu(fileMenu)
        fileMenu.addAction(self.newAction)
        fileMenu.addAction(self.openAction)
        fileMenu.addAction(self.saveAction)
        fileMenu.addAction(self.exitAction)
        # Edit menu
        editMenu = menuBar.addMenu("&Edit")
        editMenu.addAction(self.copyAction)
        editMenu.addAction(self.pasteAction)
        editMenu.addAction(self.cutAction)
        # Help menu
        helpMenu = menuBar.addMenu("&Help")
        helpMenu.addAction(self.helpContentAction)
        helpMenu.addAction(self.aboutAction)

    def _createActions(self):
        # Creating action using the first constructor
        self.newAction = QAction(self)
        self.newAction.setText("&New")
        # Creating actions using the second constructor
        self.openAction = QAction("&Open...", self)
        self.saveAction = QAction("&Save", self)
        self.exitAction = QAction("&Exit", self)
        self.copyAction = QAction("&Copy", self)
        self.pasteAction = QAction("&Paste", self)
        self.cutAction = QAction("C&ut", self)
        self.helpContentAction = QAction("&Help Content", self)
        self.aboutAction = QAction("&About", self)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWin()
    mainWin.show()

    sys.exit(app.exec_())

图片.png

2.右侧添加进度条
import sys

from PyQt5.QtCore import Qt, QBasicTimer
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
import os


size_x = 800
size_y = 600

class MainWin(QMainWindow):
    def __init__(self):
        super(MainWin, self).__init__()

        self.setWindowTitle("任务控制平台")

        self.resize(size_x, size_y)
        self.status = self.statusBar()
        self._createActions()
        self._createToolBars()
        self._createMenuBar()
        self.status.showMessage('当前无任务')
        self.timer= QBasicTimer()

        #self.centralWidget = QLabel("当前无可用设备")
        #self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        #self.setCentralWidget(self.centralWidget)
        self.setup_ui()
    def setup_ui(self):
        btn = QPushButton(self)
        btn.move(50,int(size_y/3*0+50))
        btn.resize(200,30)
        btn.setText('笔记本设备1(点击打开挺进地牢)')
        btn.clicked.connect(self.button1_clicked)
        
        btn2 = QPushButton(self)
        btn2.move(50,int(size_y/3*1+50))
        btn2.resize(200,30)
        btn2.setText('笔记本设备2(点击关闭)')
        btn2.clicked.connect(self.close)

        btn3 = QPushButton(self)
        btn3.move(50,int(size_y/3*2+50))
        btn3.resize(200,30)
        btn3.setText('隔壁老王家笔记本设备1')
        btn3.clicked.connect(self.doAction)

        qb = QProgressBar(self)
        qb.move(300,int(size_y/3*2+50))
        qb.resize(200,25)


    def doAction(self, value):
        print("do action")
        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('开始')
        else:
            self.timer.start(100, self)
            self.btn.setText('停止')
        
    def button1_clicked(self):
        os.system('python openGame.py')
    def _createToolBars(self):
        # Using a title
        fileToolBar = self.addToolBar("File")
        # Using a QToolBar object
        editToolBar = QToolBar("Edit", self)
        self.addToolBar(editToolBar)
        # Using a QToolBar object and a toolbar area
        helpToolBar = QToolBar("Help", self)
        self.addToolBar(Qt.LeftToolBarArea, helpToolBar)
        
    def _createMenuBar(self):
        menuBar = self.menuBar()
        # File menu
        fileMenu = QMenu("&File", self)
        menuBar.addMenu(fileMenu)
        fileMenu.addAction(self.newAction)
        fileMenu.addAction(self.openAction)
        fileMenu.addAction(self.saveAction)
        fileMenu.addAction(self.exitAction)
        # Edit menu
        editMenu = menuBar.addMenu("&Edit")
        editMenu.addAction(self.copyAction)
        editMenu.addAction(self.pasteAction)
        editMenu.addAction(self.cutAction)
        # Help menu
        helpMenu = menuBar.addMenu("&Help")
        helpMenu.addAction(self.helpContentAction)
        helpMenu.addAction(self.aboutAction)

    def _createActions(self):
        # Creating action using the first constructor
        self.newAction = QAction(self)
        self.newAction.setText("&New")
        # Creating actions using the second constructor
        self.openAction = QAction("&Open...", self)
        self.saveAction = QAction("&Save", self)
        self.exitAction = QAction("&Exit", self)
        self.copyAction = QAction("&Copy", self)
        self.pasteAction = QAction("&Paste", self)
        self.cutAction = QAction("C&ut", self)
        self.helpContentAction = QAction("&Help Content", self)
        self.aboutAction = QAction("&About", self)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWin()
    mainWin.show()

    sys.exit(app.exec_())

图片.png

3.通过第三个按钮来启动进度条
4.对任务进行实时控制

(持续更新中。。。)