PyQt5基础 QSpinBox 计数器

360 阅读2分钟
  •        Python : 3.8.13
  •          OS : Windows 21H1
  •       Conda : 4.12.0
  •     PyCharm : 2022.1 (Community Edition)

UI界面

qspinBox-1.png

文件结构

C:\Users\admin\PycharmProjects\pythonProject1>tree /F
文件夹 PATH 列表
卷序列号为 B0D5-61C1
C:.
│  demo.py
│  main.py
│  untitled.py
│  untitled.ui
│
├─.idea
│  │  .gitignore
│  │  misc.xml
│  │  modules.xml
│  │  pythonProject1.iml
│  │  workspace.xml
│  │
│  └─inspectionProfiles
│          profiles_settings.xml
│
└─__pycache__
        untitled.cpython-38.pyc

代码

# -*- coding: utf-8 -*-

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


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(466, 417)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
        self.spinBox.setGeometry(QtCore.QRect(140, 140, 42, 22))
        self.spinBox.setMinimum(-50)
        self.spinBox.setMaximum(50)
        self.spinBox.setSingleStep(2)
        self.spinBox.setObjectName("spinBox")
        self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox.setGeometry(QtCore.QRect(220, 140, 62, 22))

        # 小数位
        self.doubleSpinBox.setDecimals(3)
        # 最小值
        self.doubleSpinBox.setMinimum(-10.0)
        # 最大值
        self.doubleSpinBox.setMaximum(10.0)
        # 调节时的单步步长
        self.doubleSpinBox.setSingleStep(0.05)


        self.doubleSpinBox.setObjectName("doubleSpinBox")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(150, 120, 54, 12))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(220, 120, 71, 16))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(140, 210, 54, 12))
        self.label_3.setObjectName("label_3")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(220, 200, 75, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 466, 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.label_3.setText(_translate("MainWindow", "TextLabel"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

import untitled


def cal_and_show(spinBox, doubleSpinBox, label):
    print(spinBox.value())
    print(doubleSpinBox.value())
    result = spinBox.value() + doubleSpinBox.value()
    print(result)
    label.setText(str(result))


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = QMainWindow()

    ui = untitled.Ui_MainWindow()
    ui.setupUi(window)

    ui.pushButton.clicked.connect(lambda: cal_and_show(ui.spinBox,
                                                       ui.doubleSpinBox,
                                                       ui.label_3))

    window.show()

    sys.exit(app.exec_())

效果

D:\Develop\Anaconda3\envs\pyqtenv\python.exe C:/Users/admin/PycharmProjects/pythonProject1/main.py
12
0.151
12.151

Process finished with exit code 0

qspinBox-2.png

学习推荐


Python具有开源、跨平台、解释型和交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
GUI可以选择Tkinter、PySide2、wxPython、PyGObject、wxWidgets等进行创作。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。