[PyQt6] PartⅡ. QSpinBox

Creating QSpinBox

重点代码

self.spinbox.valueChanged.connect(self.spin_selected) # 当值发生变化的时候,调用是spin_selected函数

完整代码

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QRadioButton, QLabel, QVBoxLayout, 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("Python QCheckBox")
        self.setWindowIcon(QIcon('python.png')) # 设置图片,没有的话不显示

        hbox = QHBoxLayout()

        label = QLabel("Laptop 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("error")

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

当失去焦点时,再调用函数

self.doubleSpinBox.editingFinished.connect("self.second_result") 
posted @ 2022-05-23 20:42  deadright  阅读(72)  评论(0)    收藏  举报