0. why pyQt5

Cross-platform, easy to build, if I write Windows code, now want to run on Linux, MAC, first of all, C# cannot do, because the essence of C# is several Windows core libraries, so the development of Windows applications may be a sword, but cross-platform capabilities are difficult to do. Windows DLLS are not available on other operating systems.

(Right off the bat, not all right, but I think close to correct)

1. Import all dependent libraries via PIP (bring them here)

PyQt is built on Qt, so what you download here are actually Qt dependencies that use Qt functionality

This is the PyQt5 library

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

This is a PyQt5 edit package, it should be able to drag the UI visually, but I don’t use it, I think it’s cooler to design the UI directly in Python

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

2. An example of a UI with nothing

from PyQt5 import QtWidgets if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) w = QtWidgets.QWidget() w.resize(400, 200) W.setwindowTitle (" Mission Control Platform ") W.Show () exit(app.exec_())Copy the code

Resize: length * width setWindowTitle: title text show() : instantiate window object exit(app.exec_()) : provides a built-in cross click event to terminate the program thread

2. A UI example of something

import sys from PyQt5.QtWidgets import QMainWindow, QApplication class MainWin(QMainWindow): def __init__(self): Super (MainWin, self).__init__() self.setwindowTitle (" grab ") self.resize(400, 300) self.status = self.statusbar () self.status.showmessage (' fetch ') if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWin() mainWin.show() sys.exit(app.exec_())Copy the code

3. Examples of UI interfaces with titles in the middle

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 (" task Control platform ") self.resize(400, 300) self.status = self.statusbar () self.status.showMessage(' currently no task ') self.centralWidget = QLabel(" Currently no device available ") 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_())Copy the code

4. At the beginning of the event, add a toolbar

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 (" task Control platform ") self.resize(400, 300) self.status = self.statusbar () self._createToolbars () self._createmenubar () self.statusbar () self.statusBar() self.statusBar() Self. CentralWidget = QLabel (currently no use "plant") 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_())Copy the code

5. Add the drop-down list

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 (" task Control platform ") self.resize(400, 300) self.status = self.statusBar() self._createActions() self._createToolBars() self._createMenuBar() Self. Status. ShowMessage (' current no task) self. CentralWidget = QLabel (currently no use "plant") 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_())Copy the code

6. Add a button to close the event through the signal slot

import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import * class MainWin(QMainWindow): def __init__(self): Super (MainWin, self).__init__() self.setwindowTitle (" task Control platform ") self.resize(400, 300) self.status = self.statusBar() self._createActions() self._createToolBars() self._createMenuBar() Self.status. showMessage(' currently no task ') #self.centralWidget = QLabel(" Currently no device available ") #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 (' laptop 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_())Copy the code

7. Let the button fly for a while

import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import * class MainWin(QMainWindow): def __init__(self): Super (MainWin, self).__init__() self.setwindowTitle (" task Control platform ") self.resize(400, 300) self.status = self.statusBar() self._createActions() self._createToolBars() self._createMenuBar() Self.status. showMessage(' currently no task ') #self.centralWidget = QLabel(" Currently no device available ") #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 (' laptop 1 ') btn.clicked. Connect (self.close) Btn2 = QPushButton(self) btn2.move(50,100) btn2.resize(200,30) btn2.setText(' laptop device 1(click close)') Resize (200,30). Clicked. Connect (self.close) btn2 = QPushButton(self) 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_())Copy the code

8. Open the game through events

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 (" task Control platform ") self.resize(400, 300) self.status = self.statusBar() self._createActions() self._createToolBars() self._createMenuBar() Self.status. showMessage(' currently no task ') #self.centralWidget = QLabel(" Currently no device available ") #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 (' laptop device 1 ') Btn.clicked. Connect (self.button1_clicked) btn2 = QPushButton(self) btn2.move(50,100) btn2.resize(200,30) Clicked. Connect (self.close) btn2 = QPushButton(self) btn2.move(50,150) Btn2.resize (200,30) btn2.settext (' 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_())Copy the code
Import sys import OS if __name__ == "__main__": print(" OK ") os.system(r' your absolute path \ etg.exe ')Copy the code

Played the game for four years, except lich King (need four characters true ending set to play) failed to beat almost recited every boss and pawn’s move sheet, unlocked the ultimate role

The store sold the last item short, and the original 10 star coin item went up to 200, so it got squeezed