PyQT6: It is enough to read this article

foreword

This article comes from the PyQT6 learning tutorial at station B, which can be used as study notes. Of course, the content has been deleted, so the following link is recommended.

[Udemy Paid Course] Using PyQt6 and Qt Designer for Python GUI Development (Chinese and English Subtitles )

1. Build the Win10 environment of PyQT6

1. IDE environment

Pycharm+python3.10+PyQT6 and its components

2. Create a project through Pycharm (using a virtual environment)

3. Install PyQT6 through the Pycharm terminal

pip install pyqt6

4. Install PyQT6 related components (install when using)

5. Design a Demo verification environment

from PyQt6.QtWidgets import QApplication, QWidget, QMainWindow, QDialog
import sys

# 一、应用程序对象,只能有一个
# 1、该类管理GUI应用程序控制流和主要设置,专门用于QWidget所需的一些功能
# 2、不使用命令行或提示符程序(cmd),则为空列表 []
# 3、如果使用命令行或提示符程序(cmd),则为sys.argv
app = QApplication(sys.argv)
# 二、从窗口类型中可以创建三种窗口对象
# 1、QWidget
# 2、QMainWindow
# 3、QDialog
window = QWidget()
# 三、QWidget显示窗口
window.show()
# 四、执行程序
sys.exit(app.exec())

2. The use of three window types

1、QWidget

Base class for all user interface objects with rich widgets (e.g. buttons, input boxes)


2、QMainWindow

The base class of the main window, which provides a main application window, a framework for building the application user interface, has its own layout, and can access QStatusBar, QToolbar, etc.

(1) Sample program

from PyQt6.QtWidgets import QApplication, QWidget, QMainWindow, QDialog
import sys

app = QApplication(sys.argv)
window = QMainWindow()

window.statusBar().showMessage("欢迎来到PyQT6课程")  # 只有QMainWindow才能创建
window.menuBar().addMenu("File")  # 只有QMainWindow才能创建

window.show()
sys.exit(app.exec())


3、QDialog 

Dialog window base class, mainly used for top-level window interaction for short-term tasks

3. Property setting of QWidget window

from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtGui import QIcon  # 导入引用图标的函数
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("我的第一个程序的标题")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标
        self.setStyleSheet('background-color:green')  # 设置窗口内背景颜色
        self.setWindowOpacity(0.5)  # 设置窗口透明度
        # self.setFixedWidth(700)  # 不生效,被禁用了
        # self.setFixedHeight(400)  # 不生效,被禁用了



app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Fourth, the use of Qt Designer

1. Installation

pip install pyqt6-tools

(1)有个错误:  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

(2) Solution:

①The python 3.10 version may be too high, and the virtual environment needs to be replaced (here is the Anaconda replacement method)

②Download Anaconda first

Anaconda | The World's Most Popular Data Science Platform https://www.anaconda.com/ ③ Install Anaconda with one click, and add the condabin directory to the system environment variable

④Change the python version through pycharm

 ⑤Delete the original virtual environment or create a new project, uninstall pyqt6, and reinstall pyqt6

pip install pyqt6
pip install pyqt6-tools

2. use

(1) After installing pyqt6-tools, find the path of the qt application and double-click to open it

 (2) How to use

① You can convert ui files to py files for use

② Directly load the ui file

3. Convert ui file to py file

(1) Save the UI file first

(2) Convert again

pyuic6 -x .\ui\WinUI.ui -o ./WinUI.py

 

 4. Load the ui file directly

# Form implementation generated from reading ui file '.\ui\WinUI.ui'
#
# Created by: PyQt6 UI code generator 6.1.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets, uic


class Ui_Form(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        uic.loadUi("./ui/WinUI.ui", self)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Ui_Form()
    window.show()
    sys.exit(app.exec())

Five, the use of QLabel

A label object, which can be used as a text label or an image label.

from PyQt6.QtWidgets import QApplication, QWidget, QLabel
from PyQt6.QtGui import QIcon, QFont, QPixmap, QMovie  # 导入引用图标的函数
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("我的第一个程序的标题")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        '''文字标签
        label = QLabel("我的第一个文字标签", self)  # 创建标签对象
        label.setText("设置文本")  # 文本赋值
        label.move(100, 100)  # 移动文本
        label.setFont(QFont("Sanserif", 15))  # 改变字体大小,因为跟QFont有关,需要引入QFont
        label.setStyleSheet("color:red")  # 设置红色

        label.setNum(16)  # 设置数字
        label.clear()  # 清理标签
        '''

        '''静态图像标签
        label = QLabel(self)
        pixmap = QPixmap("./bilibili.jpg")  # 转化成像素图对象
        label.setPixmap(pixmap)  # 将标签设置为像素图对象
        '''

        '''git图像标签'''
        label = QLabel(self)
        movie = QMovie("./动态图.gif")  # 只能加载没有声音的图像
        movie.setSpeed(500)  # 设置动态图播放速度
        label.setMovie(movie)  # 将标签设置为像素图对象
        movie.start()




app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Six, the use of QPushButton

The button object, here shows the basic operation.

from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMenu
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("按钮相关的学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        self.create_button()

    def create_button(self):
        btn = QPushButton("click", self)  # 按钮对象
        btn.setGeometry(100, 100, 130, 130)  # 设置大小与位置
        btn.setFont(QFont("Times", 14, QFont.Weight.ExtraBold))  # 设置字体(Times为时间类型, 14为字体大小,QFont.Weight.ExtraBold为粗体)
        btn.setIcon(QIcon("./bilibili.jpg"))  # 加按钮图标
        btn.setIconSize(QSize(36, 36))  # 设置图标大小

        # 菜单选项
        menu = QMenu()  # 创建菜单选项
        menu.setFont(QFont("Times", 14, QFont.Weight.ExtraBold))  # 设置字体大小
        menu.setStyleSheet("background-color:green")  # 设置背景
        menu.addAction("Copy")  # 添加菜单选项
        menu.addAction("Cut")

        btn.setMenu(menu)  # 引用菜单选项



app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Seven, the use of QLineEdit

Input box object, used to input text.

from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("按钮相关的学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        line_edit = QLineEdit(self)
        line_edit.setFont(QFont("Sanserif", 15))  # 设置编辑框的文字大小
        # line_edit.setText("设置默认文本")
        # line_edit.setPlaceholderText("请输入文本")  # 设置提示语
        # line_edit.setEnabled(False)  # 置灰,禁止输入框输入
        line_edit.setEchoMode(QLineEdit.EchoMode.Password)  # 输入时不会以明文展示


app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Eight, the use of QHBoxLayout

Align objects horizontally, for horizontal alignment.

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("按钮相关的学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        hbox = QHBoxLayout()

        btn1 = QPushButton("click1")  # 添加按钮
        btn2 = QPushButton("click2")
        btn3 = QPushButton("click3")
        btn4 = QPushButton("click4")
        btn5 = QPushButton("click5")

        hbox.addWidget(btn1)
        hbox.addWidget(btn2)
        hbox.addWidget(btn3)
        hbox.addWidget(btn4)
        hbox.addWidget(btn5)

        # hbox.addSpacing(100)  # 添加间距的方式一
        hbox.addStretch(5)  # 添加间距的方式二

        self.setLayout(hbox)



app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Nine, the use of QVBoxLayout

Align objects vertically, for vertical alignment.

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("按钮相关的学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        hbox = QVBoxLayout()

        btn1 = QPushButton("click1")  # 添加按钮
        btn2 = QPushButton("click2")
        btn3 = QPushButton("click3")
        btn4 = QPushButton("click4")
        btn5 = QPushButton("click5")

        hbox.addWidget(btn1)
        hbox.addWidget(btn2)
        hbox.addWidget(btn3)
        hbox.addWidget(btn4)
        hbox.addWidget(btn5)

        hbox.addSpacing(100)  # 添加间距的方式一
        # hbox.addStretch(5)  # 添加间距的方式二

        self.setLayout(hbox)  # 显示对象



app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Ten, the use of QGridLayout

Table alignment object for vertical and horizontal alignment

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("按钮相关的学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        grid = QGridLayout()

        btn1 = QPushButton("click1")  # 添加按钮
        btn2 = QPushButton("click2")
        btn3 = QPushButton("click3")
        btn4 = QPushButton("click4")
        btn5 = QPushButton("click5")

        grid.addWidget(btn1, 0, 0)
        grid.addWidget(btn2, 0, 1)
        grid.addWidget(btn3, 0, 2)
        grid.addWidget(btn4, 1, 0)
        grid.addWidget(btn5, 1, 2)

        self.setLayout(grid)  # 显示对象



app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

11. The use of button signal slots

After pressing the button, change the specified text

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout, QLabel
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("事件学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        self.create_widget()

    def create_widget(self):
        hbox = QHBoxLayout()
        btn = QPushButton("Change Text")
        btn.clicked.connect(self.clicked_btn)  # 连接信号槽,当点击后执行clicked_btn函数

        self.label = QLabel("默认文本")

        hbox.addWidget(btn)
        hbox.addWidget(self.label)

        self.setLayout(hbox)

    def clicked_btn(self):
        self.label.setText("按下按钮后显示")  # 当执行该函数后,设置标签为某个文本
        self.label.setFont(QFont("Times", 15))
        self.label.setStyleSheet("color:red")


app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

12. Use of QRadioButton

Single-select an object, and display the text of the object after selection.

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout, QLabel, QRadioButton
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("QRadioButton学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        self.create_radio()

    def create_radio(self):
        hbox = QHBoxLayout()

        rad1 = QRadioButton("Python")
        rad1.setIcon(QIcon("./bilibili.jpg"))
        rad1.setIconSize(QSize(40, 40))
        rad1.setFont(QFont("Times", 18))
        rad1.toggled.connect(self.radio_selected)  # 选中后,切换连接

        rad2 = QRadioButton("GO")
        rad2.setIcon(QIcon("./img.png"))
        rad2.setIconSize(QSize(40, 40))
        rad2.setFont(QFont("Times", 18))
        rad2.toggled.connect(self.radio_selected)

        self.label = QLabel("Hello")
        self.label.setFont(QFont("Sanserif", 15))

        vbox = QVBoxLayout()
        vbox.addWidget(self.label)
        vbox.addLayout(hbox)  # 将垂直布局添加到现在的水平布局中


        hbox.addWidget(rad1)
        hbox.addWidget(rad2)

        # self.setLayout(hbox)
        self.setLayout(vbox)

    def radio_selected(self):
        radio_btn = self.sender()  # 对象本身,如点击了按钮A,则为按钮A本身
        if radio_btn.isChecked():  # 检查是否选中了选项
            self.label.setText("你已经选中 {}".format(radio_btn.text()))

app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Thirteen, the use of QCheckBox

A check box object, displaying the selected object text.

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout, QLabel, QRadioButton, QCheckBox
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("QRadioButton学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        hbox = QHBoxLayout()

        self.check1 = QCheckBox("python")
        self.check1.setIcon(QIcon("./img.png"))
        self.check1.setIconSize(QSize(40, 40))
        self.check1.setFont(QFont("Sanserif", 13))
        self.check1.stateChanged.connect(self.item_selected)

        self.check2 = QCheckBox("go")
        self.check2.setIcon(QIcon("./img.png"))
        self.check2.setIconSize(QSize(40, 40))
        self.check2.setFont(QFont("Sanserif", 13))
        self.check2.stateChanged.connect(self.item_selected)

        self.check3 = QCheckBox("java")
        self.check3.setIcon(QIcon("./img.png"))
        self.check3.setIconSize(QSize(40, 40))
        self.check3.setFont(QFont("Sanserif", 13))
        self.check3.stateChanged.connect(self.item_selected)

        self.label = QLabel("Hello")
        self.label.setFont(QFont("Sanserif", 15))

        vbox = QVBoxLayout()
        vbox.addWidget(self.label)

        hbox.addWidget(self.check1)
        hbox.addWidget(self.check2)
        hbox.addWidget(self.check3)

        vbox.addLayout(hbox)

        self.setLayout(vbox)
        # self.setLayout(hbox)

    def item_selected(self):
        value = ""

        if self.check1.isChecked():
            value = self.check1.text()
        if self.check2.isChecked():
            value = self.check2.text()
        if self.check3.isChecked():
            value = self.check3.text()

        self.label.setText("你的选择是 " + value)

app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Fourteen, the use of QSpinBox

Number input box object, you can manually enter the value or add or subtract the input value

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout, QLabel, \
    QRadioButton, QCheckBox, QSpinBox, QLineEdit
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("学习QSpinBox")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        hbox = QHBoxLayout()

        label = QLabel("Price : ")
        label.setFont(QFont("Times", 15))

        self.lineedit = QLineEdit()  # 添加输入框

        self.spinbox = QSpinBox()
        self.spinbox.valueChanged.connect(self.spin_selected)

        self.total_result = QLineEdit()

        hbox.addWidget(label)
        hbox.addWidget(self.lineedit)
        hbox.addWidget(self.spinbox)
        hbox.addWidget(self.total_result)

        self.setLayout(hbox)

    def spin_selected(self):
        if self.lineedit.text() != 0:
            price = int(self.lineedit.text())
            totalPrice = self.spinbox.value() * price
            self.total_result.setText(str(totalPrice))

        else:
            print("wrong value")




app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

15. Learn the use of QDoubleSpinBox by loading the UI

Number input box object (difference from QSpinBox: double precision), you can manually enter the value or add or subtract the input value

1. Through "four", we already know the simple use of Qt Designer. There are two programming methods: we can realize our business functions by translating into py files and adding business logic; or by loading, and then through components Name to access the UI, and then add business logic to achieve our business functions.

2. Design pages and programs similar to "fourteen" through Qt Designer, and use QDoubleSpinBox instead of QSpinBox

 

 

 

 

 3. Load the ui and design the program

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout, QLabel, \
    QRadioButton, QCheckBox, QSpinBox, QLineEdit, QDoubleSpinBox
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
from PyQt6 import uic
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        uic.loadUi("./ui/QDoubleSpinBox.ui", self)

        self.lineedit = self.findChild(QLineEdit, "lineEdit001")
        self.spinbox = self.findChild(QDoubleSpinBox, "doubleSpinBox001")
        self.total_result = self.findChild(QLineEdit, "lineEdit002")

        self.spinbox.valueChanged.connect(self.spin_selected)

    def spin_selected(self):
        if self.lineedit.text() != 0:
            price = int(self.lineedit.text())
            totalPrice = self.spinbox.value() * price
            self.total_result.setText(str(totalPrice))

        else:
            print("wrong value")




app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Sixteen, the use of QLCDNumber

LCD display object, used to display numbers.

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout, QLabel, \
    QRadioButton, QCheckBox, QSpinBox, QLineEdit, QDoubleSpinBox, QLCDNumber
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize, QTime, QTimer
from PyQt6 import uic
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        uic.loadUi("./ui/QDoubleSpinBox.ui", self)

        self.lineedit = self.findChild(QLineEdit, "lineEdit001")
        self.spinbox = self.findChild(QDoubleSpinBox, "doubleSpinBox001")
        self.total_result = self.findChild(QLineEdit, "lineEdit002")

        timer = QTimer()
        timer.timeout.connect(self.showLCD)

        timer.start(1000)

        self.showLCD()

    def showLCD(self):
        vbox = QVBoxLayout()

        lcd = QLCDNumber()

        lcd.setStyleSheet("background:red")

        vbox.addWidget(lcd)

        time = QTime.currentTime()
        text = time.toString("hh:mm")

        lcd.display(text)

        self.setLayout(vbox)




app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Seventeen, the use of QComboBox

A select box object, providing options

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout, QLabel, \
    QRadioButton, QCheckBox, QSpinBox, QLineEdit, QDoubleSpinBox, QLCDNumber, QComboBox
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize, QTime, QTimer
from PyQt6 import uic
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("QComboBox学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        self.create_combo()
        self.combo.currentIndexChanged.connect(self.combo_changed)

    def create_combo(self):
        hbox = QHBoxLayout()

        label = QLabel("选择账号类型:")
        label.setFont(QFont("Times", 15))
        self.combo = QComboBox()
        self.combo.addItem("当前账号")
        self.combo.addItem("未来账号")
        self.combo.addItem("任意账号")

        hbox.addWidget(label)
        hbox.addWidget(self.combo)

        vbox = QVBoxLayout()
        self.label_result = QLabel("Hello")
        self.label_result.setFont(QFont("Times", 15))
        vbox.addWidget(self.label_result)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

    def combo_changed(self):
        item =self.combo.currentText()
        self.label_result.setText("你的账号选择了: " + item)


app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Eighteen, the use of QSlider

Slider object, you can slide the slider to change the value

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout, QLabel, \
    QRadioButton, QCheckBox, QSpinBox, QLineEdit, QDoubleSpinBox, QLCDNumber, QComboBox, QSlider
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize, QTime, QTimer, Qt
from PyQt6 import uic
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("QSlider学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        hbox = QHBoxLayout()

        self.slider = QSlider()
        self.slider.setOrientation(Qt.Orientation.Horizontal)
        self.slider.setTickPosition(QSlider.TickPosition.TicksLeft)
        # self.slider.setTickPosition(QSlider.TickPosition.TicksAbove)
        self.slider.setTickInterval(5)

        self.slider.setMinimum(0)
        self.slider.setMaximum(100)
        self.slider.valueChanged.connect(self.changed_slider)

        self.label = QLabel("")
        self.label.setFont(QFont("Times", 15))

        hbox.addWidget(self.slider)
        hbox.addWidget(self.label)

        self.setLayout(hbox)

    def changed_slider(self):
        value = self.slider.value()
        self.label.setText(str(value))


app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Nineteen, the use of QListWidget

list object, ordered list

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton , QVBoxLayout, QGridLayout, QLabel, \
    QRadioButton, QCheckBox, QSpinBox, QLineEdit, QDoubleSpinBox, QLCDNumber, QComboBox, QSlider, QListWidget
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize, QTime, QTimer, Qt
from PyQt6 import uic
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()  # 用于访问父类的方法和属性

        self.setGeometry(200, 200, 700, 400)  # 设置初始位置与窗口大小
        self.setWindowTitle("QListWidget学习")  # 设置标题
        self.setWindowIcon(QIcon("./bilibili.jpg"))  # 加载窗口图标

        hbox = QVBoxLayout()

        self.list_widget = QListWidget()

        self.list_widget.insertItem(0, "Python")
        self.list_widget.insertItem(1, "java")
        self.list_widget.insertItem(2, "c++")
        self.list_widget.insertItem(3, "c#")
        self.list_widget.insertItem(1, "go")
        self.list_widget.setFont(QFont("Times", 15))
        self.list_widget.setStyleSheet("background-color:gray")

        self.list_widget.clicked.connect(self.item_clicked)

        self.label = QLabel("")

        hbox.addWidget(self.list_widget)
        hbox.addWidget(self.label)

        self.setLayout(hbox)

    def item_clicked(self):
        item = self.list_widget.currentItem()
        self.label.setText("你选择了:" + str(item.text()))


app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Twenty, to be continued. . .

。。。

Guess you like

Origin blog.csdn.net/weixin_43431593/article/details/125577712
Recommended