一文掌握PyQt5的基础控件(内含代码哦!)

370 阅读2分钟

前言

PyQt5的基础控件有标签控件、消息框控件、文本框控件、按钮控件、定时器控件等,今天使用简单的编程代码来将其一网打尽!


基础控件介绍

  • 标签控件(QLabel):用于显示文本或图片。
  • 消息框控件(QMessageBox):用于显示标准的消息对话框。可以包含文本、图片、按钮等。
  • 文本框控件(QLineEdit 或者 QTextEdit):QLineEdit 用于单行文本输入。QTextEdit 用于多行文本编辑,支持富文本格式。
  • 按钮控件(QPushButton):用于创建可点击的按钮。按钮可以连接信号和槽,实现点击事件的处理。
  • 定时器控件(QTimer):用于实现定时任务,比如每隔一定时间执行某个操作。

代码介绍

拆分介绍

  • 标签控件(QLabel)
#显示文本
self.lable = QLabel('Hello World!')

#显示图片
self.lable_image = QLabel('lable 1', self)
self.pixmap = QPixmap(r'图片地址')
self.pixmap.scaled(100,100)
self.lable_image.setPixmap(self.pixmap)

这里在标签控件中先是简单的显示文本,结合QPixmap进行图片显示,通过scaled对图片进行尺寸处理。

  • 按钮控件(QPushButton)消息框控件(QMessageBox)
#消息框 结合open_movie
self.btn=QPushButton('打开动图')
self.btn.clicked.connect(self.open_movie)

def open_movie(self):
        answers=QMessageBox.question(self,"Tips",'open the movie?',QMessageBox.Yes|QMessageBox.No)
        if answers==QMessageBox.Yes:
            self.movie.start()
        if answers==QMessageBox.No:
            self.movie.stop()

通过按钮选择进行消息框弹出,如选择打开,则打开动图播放,否则就关闭动图播放。

  • 文本框控件(QLineEdit 或者 QTextEdit):
 #单行文本框
self.line_text=QLineEdit()
self.line_text.setEchoMode(QLineEdit.Password)

#文本编辑框
self.edit=QTextEdit()
self.edit.setLineWrapMode(QTextEdit.NoWrap)
self.edit.textChanged.connect(self.input_word)

文本框使用了单行、多行显示,在多行编辑中还增加了文本改变的事件,可以用于后续的扩展。

  • 定时器控件(QTimer)
 #定时器
self.time=QTimer()
self.time.start(10)
self.time.timeout.connect(self.time_out)

这里的定时器其实就是使用QTimer,然后定义一个时间,如果达到这个时间则会执行self.time_out。我在这里执行了时间显示,一开始没有显示秒钟,可能就看不出来刷新。也因为刷新频率过快,在前面使用输入数据更新显示框,会很快变化。

整体分享

import sys
from PyQt5.QtCore import Qt, QSize, QTime, QTimer, QDateTime
from PyQt5.QtGui import QPixmap, QMovie

from PyQt5.QtWidgets import *



class Win(QWidget):
    def __init__(self):
        super(Win,self).__init__()

        #显示文本
        self.lable = QLabel('Hello World!')

        #显示图片
        self.lable_image = QLabel('lable 1', self)
        self.pixmap = QPixmap(r'图片地址')
        self.pixmap.scaled(100,100)
        self.lable_image.setPixmap(self.pixmap)


        #显示动图
        self.lable_movie = QLabel('lable 2', self)
        self.movie=QMovie()
        self.movie.setFileName("动图地址")
        self.movie.jumpToFrame(0)
        self.lable_movie.setMovie(self.movie)


        #消息框 结合open_movie
        self.btn=QPushButton('打开动图')
        self.btn.clicked.connect(self.open_movie)

        #单行文本框
        self.line_text=QLineEdit()
        self.line_text.setEchoMode(QLineEdit.Password)

        #文本编辑框
        self.edit=QTextEdit()
        self.edit.setLineWrapMode(QTextEdit.NoWrap)
        self.edit.textChanged.connect(self.input_word)

        #文本浏览框
        self.text_bro=QTextBrowser()

        #单选框
        self.radio_open=QRadioButton("开")
        self.radio_close = QRadioButton("关")

        #复选框
        self.check_open = QCheckBox("开")
        self.check_close = QCheckBox("关")

        #下拉框
        self.combo=QComboBox()
        self.combo.addItem("开")
        self.combo.addItem("关")

        #定时器
        self.time=QTimer()
        self.time.start(10)
        self.time.timeout.connect(self.time_out)

        #布局
        h_layout=QHBoxLayout()
        h_layout.addWidget(self.lable)
        h_layout.addWidget(self.lable_image)
        h_layout.addWidget(self.lable_movie)
        h_layout.addWidget(self.line_text)
        h_layout.addWidget(self.btn)
        h_layout.addWidget(self.edit)
        h_layout.addWidget(self.text_bro)


        h1_layout=QHBoxLayout()
        h1_layout.addWidget(self.radio_open)
        h1_layout.addWidget(self.radio_close)
        h1_layout.addWidget(self.check_open)
        h1_layout.addWidget(self.check_close)
        h1_layout.addWidget(self.combo)


        v_layout=QVBoxLayout()
        v_layout.addLayout(h_layout)
        v_layout.addLayout(h1_layout)
        self.setLayout(v_layout)

    def open_movie(self):
        answers=QMessageBox.question(self,"Tips",'open the movie?',QMessageBox.Yes|QMessageBox.No)
        if answers==QMessageBox.Yes:
            self.movie.start()
        if answers==QMessageBox.No:
            self.movie.stop()

    def input_word(self):
        self.text_bro.append(self.edit.toPlainText())

    def time_out(self):
        self.text_bro.clear()
        self.text_bro.setText(QDateTime.currentDateTime().toString('yyyy-M-d hh:mm:dd:ss'))

        pass
if __name__=='__main__':
    app=QApplication([])
    window=Win()
    window.show()

    sys.exit(app.exec())

实现效果

image.png


总结

以上就是今天要讲的内容,本文仅仅简单介绍了PyQt5的基础控件使用,大家可以根据的需要进行功能扩展!