PyQt5学习笔记(6) 资源文件的使用

1. 界面布局

2. 创建资源文件

新建文件或项目,选择Qt Resource File,

添加一个前缀,比如icons,前缀就是资源的分组,在添加一个文件,如下图

保存后在按钮的属性icon中选择资源文件后,即可显示图标。 

 

4. 使用Pyrcc 将res.qrc 转换为 res_rc.py

 

5.. 设计窗体业务逻辑,参考上一节

# -*- coding: utf-8 -*-
# @Time    : 2019/11/17 17:28
# @Author  : dailinqing
# @Email   : dailinqing@126.com
# @File    : res_test.py
# @Software: PyCharm

import  sys
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtGui import QPalette, QIcon
from PyQt5.QtCore import QObject,pyqtSlot,pyqtSignal
from PyQt5.QtCore import Qt
from widget import Ui_Widget


class Human(QObject):
    nameChanged = pyqtSignal(str)
    ageChanged = pyqtSignal([int],[str])


    def __init__(self,name ="mike",age=10,parent=None):
        super().__init__(parent)
        self.setAge(age)
        self.setName(name)

    def setAge(self,age):
        self.__age = age
        self.ageChanged.emit(self.__age)
        if age<18:
            ageInfo = "你是少年"
        elif 18<age<=35:
            ageInfo = "你是年轻人"
        elif 35<age<=80:
            ageInfo = "你是老人"
        else:
            ageInfo = "您是寿星"

        self.ageChanged[str].emit(ageInfo)

    def setName(self,name):
        self.__name = name
        self.nameChanged.emit(self.__name)

class QMyWidget(QWidget):
    def __init__(self,parent = None):
        super().__init__(parent)
        self.ui = Ui_Widget()
        self.ui.setupUi(self)

        self.boy = Human("boy",16)

        self.boy.nameChanged.connect(self.do_nameChange)

        self.boy.ageChanged.connect(self.do_ageChange_int)
        self.boy.ageChanged[str].connect(self.do_ageChange_str)

    def on_horizontalSlider_valueChanged(self,value):
        self.boy.setAge(value)

    def on_lineEdit_3_textChanged(self,name):
        self.boy.setName(self.ui.lineEdit_3.text())

    @pyqtSlot(int)
    def do_ageChange_int(self,age):
        self.ui.lineEdit.setText(str(age))

    @pyqtSlot(str)
    def do_ageChange_str(self,ageInfo):
        self.ui.lineEdit_2.setText(ageInfo)

    @pyqtSlot(str)
    def do_nameChange(self,name):
        self.ui.lineEdit_4.setText(name)

if __name__ == "__main__":

    app = QApplication(sys.argv)
    icon = QIcon(":/icons/chip.png")
    app.setWindowIcon(icon)
    form  = QMyWidget()
    form.show()
    sys.exit(app.exec_())

6. 运行结果

发布了62 篇原创文章 · 获赞 18 · 访问量 12万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览