1 QComboBox
继承自QWidget,常用信号:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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)
#combox01
self.combox01 = QComboBox()
self.combox01.addItems(["星期一","星期二"])
self.combox01.addItem("星期三")
self.combox01.setCurrentIndex(1) #设置当前index 0开始计算
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSignal,Qt,QRegExp
from PyQt5.QtGui import QRegExpValidator
import sys
class QLineEditDemo(QWidget):
def __init__(self):
super(QLineEditDemo, self).__init__()
self.resize(400,400)
self.setWindowTitle("QLineEdit Demo")
self.mainlayout = QFormLayout(self)
# 1
self.lineedit_01 = QLineEdit("大天苍苍兮大地茫茫")
self.lineedit_01.setAlignment(Qt.AlignCenter) #设置文字居中显示
self.lineedit_01.setReadOnly(True) #设置不可编辑
self.lineedit_01.setStyleSheet('''
background-color: red ; /*- 背景颜色 */
color: white ; /*- 文字颜色 */
border-left: 0px ;
border-right: 0px ;
border-top: 0px ;
border-bottom: 1px solid black;''')
self.mainlayout.addRow("QLineEdit样式 ", self.lineedit_01)
# 2
self.lineedit_02 = QLineEdit("人各有志兮何可思量")
self.lineedit_02.setStyleSheet("background-color:#F0F0F0;color: black ;")
self.lineedit_02.setAlignment(Qt.AlignCenter)
self.mainlayout.addRow("QLineEdit样式 ", self.lineedit_02)
# 3 setTextMargins
self.lineedit_03 = QLineEdit("狐神鼠圣兮薄社依墙")
self.lineedit_03.setAlignment(Qt.AlignCenter)
self.lineedit_03.setTextMargins(0,0,0,0)
self.mainlayout.addRow(" QLineEdit文字:", QLabel(self.lineedit_03.text()))
# 4
self.lineedit_04 = QLineEdit("雷霆一发兮其孰敢当")
self.lineedit_04.setSelection(0, 9)
print(self.lineedit_04.selectedText()) #选择的文字
print(self.lineedit_04.text())
self.lineedit_04.insert("雷霆一发兮其孰敢当!!!")
self.lineedit_04.setAlignment(Qt.AlignCenter)
self.mainlayout.addRow(" TextMargins ", self.lineedit_04)
#密码
'''
QLineEdit.Normal
QLineEdit.NoEcho
QLineEdit.Password
QLineEdit.PasswordEchoOnEdit
'''
self.lineedit_05 = QLineEdit()
#self.lineedit_05.setEchoMode(QLineEdit.Password)
self.lineedit_05.setPlaceholderText("请输入数字")
self.mainlayout.addRow("输入数字: ", self.lineedit_05)
self.lineedit_06 = QLineEdit("123456")
self.lineedit_06.setEchoMode(QLineEdit.Password)
self.mainlayout.addRow("密码: ", self.lineedit_06)
#输入限制
self.lineedit_07 = QLineEdit("")
self.lineedit_07.setEchoMode(QLineEdit.Normal)
self.mainlayout.addRow("输入(最多6个数字)", self.lineedit_07)
reg = QRegExp('\d{0,6}')
pValidator = QRegExpValidator(self)
pValidator.setRegExp(reg)
#设置验证器
self.lineedit_07.setValidator(pValidator)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = QLineEditDemo()
win.show()
sys.exit(app.exec_())
效果如下:
3 QSpinBox
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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.mainlayout.addStretch()
#信号槽
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_())
效果如下: