码迷,mamicode.com
首页 > 其他好文 > 详细

pyqt5gui例子

时间:2019-02-01 23:51:29      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:for   margin   exp   gif   bool   创建对象   创建   nal   cal   

自定义信号:

  ui.py.

技术分享图片
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file ‘class_ts.ui‘
#
# Created by: PyQt5 UI code generator 5.11.3
#
# 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(537, 509)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setMinimumSize(QtCore.QSize(150, 30))
        self.lineEdit.setMaximumSize(QtCore.QSize(150, 30))
        self.lineEdit.setObjectName("lineEdit")
        self.horizontalLayout.addWidget(self.lineEdit)
        spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem2)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem3)
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setMinimumSize(QtCore.QSize(150, 30))
        self.lineEdit_2.setMaximumSize(QtCore.QSize(150, 30))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.horizontalLayout_2.addWidget(self.lineEdit_2)
        spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem4)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setMinimumSize(QtCore.QSize(200, 50))
        self.pushButton.setMaximumSize(QtCore.QSize(200, 50))
        self.pushButton.setStyleSheet("background-color: rgb(85, 170, 0);")
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton, 0, QtCore.Qt.AlignHCenter)
        spacerItem5 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem5)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 537, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "帐号"))
        self.label_2.setText(_translate("MainWindow", "密码"))
        self.pushButton.setText(_translate("MainWindow", "登录"))
View Code

 

1.装饰器信号

技术分享图片
# -*- coding:utf-8 -*-

from PyQt5.Qt import *


from class_ts import Ui_MainWindow


class Window(QMainWindow, Ui_MainWindow):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("装饰器连接信号")
        self.resize(500,500)
        self.setupUi(self)


    @pyqtSlot(bool)
    def on_pushButton_clicked(self,val):  # on_btn_clicked固定的函数名
        print("按纽被点击了",val)

    @pyqtSlot()
    def on_pushButton_pressed(self):  # on_btn_clicked固定的函数名
        print("按纽被按下了")



if __name__=="__main__":
    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
View Code

 2.自定义信号传送数据

技术分享图片
# -*- coding:utf-8 -*-

from PyQt5.Qt import *
# 定义一个类,重写QPushButton鼠标点击事件
class Btn(QPushButton):
    # 自定义一个信号
    # rightClicked = pyqtSignal(str)
    rightClicked = pyqtSignal([str],[int])
    def mousePressEvent(self,evt):
        super().mousePressEvent(evt)
        print(evt)
        print(evt.button())
        # QMouseEvent
        # 判断某个键点击了
        if evt.button() == Qt.RichText: # RichText = 1 是左键, RightButton = 2 是右键
            print("应该发射左击信号了")
            # self.rightClicked.emit(self.text())
            self.rightClicked[str].emit(self.text())
            self.rightClicked[int].emit(888)

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("ui学习")
        self.resize(500,500)
        # self.setupUi(self)
        self.setup_ui()


    def setup_ui(self):
        # 创建对象
        btn = Btn("xxxx",self)
        # rightClicked是自定义的信号
        # content接收数据
        btn.rightClicked[int].connect(lambda content:print("按纽被点击了",content))


if __name__=="__main__":
    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
View Code

 

pyqt5gui例子

标签:for   margin   exp   gif   bool   创建对象   创建   nal   cal   

原文地址:https://www.cnblogs.com/xaiobong/p/10347292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!