pyside6界面开发笔记(05)

程序入口模板:pyside6界面开发笔记——模板框架

标签控件

显示文本

  • 示例1
class Window(QWidget):

    def __init__(self):
        super(Window,self).__init__()

        label=QLabel('我喜欢非常pyside6,你呢?也很喜欢吧。')
        label.setWordWrap(True) # 开启自动换行

        # 使用水平布局
        h_layout=QHBoxLayout()
        h_layout.addWidget(label)

        #将水平布局方式设置为窗口的整体布局
        self.setLayout(h_layout)
  • 示例2:文本纵向显示
class Window(QWidget):

    def __init__(self):
        super(Window,self).__init__()

        label=QLabel()
        text='我 喜欢 pyside'
        words=text.split(' ')
        label.setText('\n'.join(words))

        # 使用水平布局
        h_layout=QHBoxLayout()
        h_layout.addWidget(label)

        #将水平布局方式设置为窗口的整体布局
        self.setLayout(h_layout)

显示图片

class Window(QWidget):

    def __init__(self):
        super(Window,self).__init__()

        label=QLabel()
        pixmap=QPixmap('logo.png') # 实例化QPixmap
        label.setPixmap(pixmap)    # 调用setPixmap()显示

        # 使用水平布局
        h_layout=QHBoxLayout()
        h_layout.addWidget(label)

        #将水平布局方式设置为窗口的整体布局
        self.setLayout(h_layout)
  • 完善自适应拉伸
class Window(QWidget):

    def __init__(self):
        super(Window,self).__init__()

        label=QLabel(self)
        pixmap=QPixmap('logo.png') 
        label.setPixmap(pixmap)
        label.setScaledContents(True) # 实现自适应

        # 使用水平布局
        h_layout=QHBoxLayout()
        h_layout.addWidget(label)

        #将水平布局方式设置为窗口的整体布局
        self.setLayout(h_layout)

显示动图

class Window(QWidget):

    def __init__(self):
        super(Window,self).__init__()

        self.movie=QMovie()
        self.movie.setFileName('./test.gif')
        self.movie.jumpToFrame(0)
        """
        jumpToFrame:设置当前要显示的帧
        """
        self.label=QLabel()
        self.label.setMovie(self.movie)
        self.label.setAlignment(Qt.AlignCenter)
        """
        setAlignment(Qt.AlignCenter):让QLabel居中显示
        """
        self.start_btn=QPushButton('开始')
        self.pause_btn=QPushButton('暂停')
        self.stop_btn=QPushButton('停止')
        self.speed_up_btn=QPushButton('加速')
        self.speed_down_btn=QPushButton('减速')

        self.start_btn.clicked.connect(self.control)
        self.pause_btn.clicked.connect(self.control)
        self.stop_btn.clicked.connect(self.control)
        self.speed_up_btn.clicked.connect(self.control)
        self.speed_down_btn.clicked.connect(self.control)

        h_layout=QHBoxLayout()
        v_layout=QVBoxLayout()

        h_layout.addWidget(self.start_btn)
        h_layout.addWidget(self.pause_btn)
        h_layout.addWidget(self.stop_btn)
        h_layout.addWidget(self.speed_up_btn)
        h_layout.addWidget(self.speed_down_btn)

        v_layout.addWidget(self.label)
        v_layout.addLayout(h_layout)

        self.setLayout(v_layout)

    def control(self):
        if self.sender()==self.start_btn:
            self.movie.start()
        elif self.sender()==self.pause_btn:
            if self.pause_btn.text()=='暂停':
                self.movie.setPaused(True)
                self.pause_btn.setText('继续')
            else:
                self.movie.setPaused(False)
                self.pause_btn.setText('暂停')
        elif self.sender()==self.stop_btn:
            self.movie.stop()
            self.movie.jumpToFrame(0)
        elif self.sender()==self.speed_up_btn:
            speed=self.movie.speed()
            self.movie.setSpeed(speed*2)
        elif self.sender()==self.speed_down_btn:
            speed=self.movie.speed()
            self.movie.setSpeed(speed/2)

笔记笔者根据《Pyqt编程快速上手》将其中pyqt改为pyside整理而成,所有代码均经过验证

  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值