PyQt5+eric6之旅(四) - 信号与槽的绑定

Leon

如果编辑了按钮, 那么接下来就是通过按钮触发某个动作

  • 首先右击窗体,生成对话框代码

clipboard.png

  • 绑定按钮

clipboard.png

  • 接着在代码栏就会看到生成的py文件

clipboard.png

  • 在qt designer中创建slot 绑定

clipboard.png

  • 再次编译 打开py文件发现已经绑定

clipboard.png

  • code ui_0726.py

        # -*- coding: utf-8 -*-
       
       # Form implementation generated from reading ui file 'D:\python\PyQt5\0726\0726.ui'
       #
       # Created by: PyQt5 UI code generator 5.10.1
       #
       # WARNING! All changes made in this file will be lost!
       
       from PyQt5 import QtCore, QtGui, QtWidgets
       
       class Ui_MainWindow(object):
           def setupUi(self, MainWindow):
               MainWindow.setObjectName("MainWindow")
               MainWindow.resize(531, 648)
               icon = QtGui.QIcon()
               icon.addPixmap(QtGui.QPixmap("C:/Users/aabgiilln/Desktop/服务日志.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
               MainWindow.setWindowIcon(icon)
               self.centralWidget = QtWidgets.QWidget(MainWindow)
               self.centralWidget.setObjectName("centralWidget")
               self.gridLayout = QtWidgets.QGridLayout(self.centralWidget)
               self.gridLayout.setObjectName("gridLayout")
               self.label = QtWidgets.QLabel(self.centralWidget)
               self.label.setObjectName("label")
               self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
               self.lineEdit = QtWidgets.QLineEdit(self.centralWidget)
               self.lineEdit.setObjectName("lineEdit")
               self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
               self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
               self.pushButton_2.setObjectName("pushButton_2")
               self.gridLayout.addWidget(self.pushButton_2, 0, 2, 1, 1)
               self.label_2 = QtWidgets.QLabel(self.centralWidget)
               self.label_2.setObjectName("label_2")
               self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
               self.lineEdit_2 = QtWidgets.QLineEdit(self.centralWidget)
               self.lineEdit_2.setObjectName("lineEdit_2")
               self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
               self.pushButton = QtWidgets.QPushButton(self.centralWidget)
               self.pushButton.setObjectName("pushButton")
               self.gridLayout.addWidget(self.pushButton, 1, 2, 1, 1)
               self.textEdit = QtWidgets.QTextEdit(self.centralWidget)
               self.textEdit.setObjectName("textEdit")
               self.gridLayout.addWidget(self.textEdit, 3, 0, 1, 3)
               self.lineEdit_3 = QtWidgets.QLineEdit(self.centralWidget)
               self.lineEdit_3.setObjectName("lineEdit_3")
               self.gridLayout.addWidget(self.lineEdit_3, 2, 1, 1, 2)
               self.label_3 = QtWidgets.QLabel(self.centralWidget)
               self.label_3.setObjectName("label_3")
               self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
               MainWindow.setCentralWidget(self.centralWidget)
               self.toolBar = QtWidgets.QToolBar(MainWindow)
               self.toolBar.setObjectName("toolBar")
               MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
               self.actionRun = QtWidgets.QAction(MainWindow)
               icon1 = QtGui.QIcon()
               icon1.addPixmap(QtGui.QPixmap("C:/Users/aabgiilln/Desktop/运行中.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
               self.actionRun.setIcon(icon1)
               self.actionRun.setObjectName("actionRun")
               self.actionPause = QtWidgets.QAction(MainWindow)
               icon2 = QtGui.QIcon()
               icon2.addPixmap(QtGui.QPixmap("C:/Users/aabgiilln/Desktop/播放-暂停.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
               self.actionPause.setIcon(icon2)
               self.actionPause.setObjectName("actionPause")
               self.actionQuit = QtWidgets.QAction(MainWindow)
               icon3 = QtGui.QIcon()
               icon3.addPixmap(QtGui.QPixmap("C:/Users/aabgiilln/Desktop/退出 (1).png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
               self.actionQuit.setIcon(icon3)
               self.actionQuit.setObjectName("actionQuit")
               self.toolBar.addAction(self.actionRun)
               self.toolBar.addAction(self.actionPause)
               self.toolBar.addAction(self.actionQuit)
       
               self.retranslateUi(MainWindow)
               self.pushButton.clicked.connect(MainWindow.close)
               self.pushButton_2.clicked.connect(MainWindow.on_pushButton_clicked)
               QtCore.QMetaObject.connectSlotsByName(MainWindow)
       
           def retranslateUi(self, MainWindow):
               _translate = QtCore.QCoreApplication.translate
               MainWindow.setWindowTitle(_translate("MainWindow", "实时日志"))
               self.label.setText(_translate("MainWindow", "服务器IP"))
               self.pushButton_2.setText(_translate("MainWindow", "开始"))
               self.label_2.setText(_translate("MainWindow", "服务密码"))
               self.pushButton.setText(_translate("MainWindow", "退出"))
               self.label_3.setText(_translate("MainWindow", "状态信息"))
               self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
               self.actionRun.setText(_translate("MainWindow", "Run"))
               self.actionPause.setText(_translate("MainWindow", "Pause"))
               self.actionQuit.setText(_translate("MainWindow", "Quit"))
       
       
       if __name__ == "__main__":
           import sys
           app = QtWidgets.QApplication(sys.argv)
           MainWindow = QtWidgets.QMainWindow()
           ui = Ui_MainWindow()
           ui.setupUi(MainWindow)
           MainWindow.show()
           sys.exit(app.exec_())
    
    • code start.py
        # -*- coding: utf-8 -*-
       
       """
       Module implementing Start.
       """
       
       from PyQt5.QtCore import pyqtSlot
       from PyQt5.QtWidgets import QMainWindow
       
       from Ui_0726 import Ui_MainWindow
       
       
       class Start(QMainWindow, Ui_MainWindow):
           """
           Class documentation goes here.
           """
           def __init__(self, parent=None):
               """
               Constructor
               
               @param parent reference to the parent widget
               @type QWidget
               """
               super(Start, self).__init__(parent)
               self.setupUi(self)
           
           @pyqtSlot()
           def on_pushButton_clicked(self):
               """
               Slot documentation goes here.
               """
               # TODO: not implemented yet
               self.lineEdit_3.setText('start')
       
       if __name__ == '__main__':
          import sys
          from PyQt5.QtWidgets import  QApplication
          app = QApplication(sys.argv)
          window = Start() ## 类名,注意要和自己定义的类名一致。
          window.show()
          sys.exit(app.exec_())
    • result

clipboard.png

阅读 1.4k

黑客与音乐家
音乐爱好者的计算机之旅

正确的方向比努力更重要

548 声望
16 粉丝
0 条评论

正确的方向比努力更重要

548 声望
16 粉丝
宣传栏