1 QComboBox
继承自QWidget,常用信号:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import sys
class DemoQComboBox(QWidget):
def __init__(self):
super(DemoQComboBox, self).__init__()
self.setWindowTitle("DemoQComboBox")
self.resize(400,400)
self.mainlayout = QVBoxLayout(self)
self.combox01 = QComboBox()
self.combox01.addItems(["星期一","星期二"])
self.combox01.addItem("星期三")
self.combox01.setCurrentIndex(1)
print("combox01 count: ",self.combox01.count() )
print("combox01 当前index: ",self.combox01.currentIndex(), "text: ", self.combox01.currentText() )
self.mainlayout.addWidget(self.combox01)
self.combox01.currentIndexChanged.connect(self.combox01_currentIndexChanged_slot)
self.combox01.currentIndexChanged[str].connect(self.combox01_indexchange_str)
def combox01_currentIndexChanged_slot(self, index):
print("index: ", index)
def combox01_indexchange_str(self, text):
print(text)
if __name__ == '__main__':
app = QApplication(sys.argv)
test = DemoQComboBox()
test.show()
sys.exit(app.exec_())
显示如下:
2 QLineEdit
3 QSpinBox
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import sys
class DemoSpinBox(QWidget):
def __init__(self):
super(DemoSpinBox, self).__init__()
self.setWindowTitle("QSpinBox测试")
self.resize(300,200)
self.mainlayout = QVBoxLayout(self)
layout01 = QGridLayout()
self.mainlayout.addLayout(layout01)
layout01.addWidget(QLabel("数量"),0,0,1,1)
self.num = QSpinBox()
self.num.setValue(2)
self.num.setRange(0,100)
self.num.setSuffix(" kg")
layout01.addWidget(self.num, 0, 1, 1, 1)
layout01.addWidget(QLabel("单价"), 0, 2, 1, 1)
self.price = QDoubleSpinBox()
self.price.setValue(2.1)
self.price.setPrefix("$ ")
layout01.addWidget(self.price, 0, 3, 1, 1)
self.btn = QPushButton("计算")
layout01.addWidget(self.btn, 1, 0, 1, 2)
layout01.addWidget(QLabel("总价"), 1, 2, 1, 1)
self.totalprice = QDoubleSpinBox()
self.totalprice.setValue(0)
self.totalprice.setPrefix("$ ")
layout01.addWidget(self.totalprice, 1, 3, 1, 1)
line= QFrame()
line.setFrameStyle(QFrame.HLine|QFrame.Plain)
self.mainlayout.setSpacing(0)
self.mainlayout.addWidget(line)
layout02 = QGridLayout()
layout02.addWidget(QLabel("二进制显示"),0,0)
self.bin_spin = QSpinBox()
self.bin_spin.setPrefix("bin ")
self.bin_spin.setValue(8)
self.bin_spin.setRange(0,0xFF)
self.bin_spin.setDisplayIntegerBase(2)
layout02.addWidget(self.bin_spin, 0, 1)
self.mainlayout.addLayout(layout02)
self.btn.clicked.connect(self.on_btn_clicked)
def on_btn_clicked(self):
value = self.price.value()*self.num.value()
self.totalprice.setValue(value)
if __name__ == '__main__':
app = QApplication(sys.argv)
test = DemoSpinBox()
test.show()
sys.exit(app.exec_())
效果如下:
|