1.QLabel组件
QLabel用于显示文本或图像,不提供用户交互功能,标签的视觉外观可以以各种方式配置。
举个例子吧
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")#设置窗口的名称
widget=QLabel("Hello")
font =widget.font()
font.setPointSize(30)
widget.setFont(font)
widget.setAlignment(
Qt.AlignmentFlag.AlignHCenter
| Qt.AlignmentFlag.AlignHCenter#设置文字在窗口的中间位置
)
self.setCentralWidget(widget)
app=QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec()
实现效果:
2.复选框QCheckBox:常用作表单选项
演示代码如下:
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QCheckBox, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
widget=QCheckBox("This is a checkBox")
widget.setCheckState(Qt.CheckState.Checked)
widget.stateChanged.connect(self.show_state)
self.setCentralWidget(widget)
def show_state(self, s):
print(Qt.CheckState(s)==Qt.CheckState.Checked)
print(s)
app = QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec()
实现效果
0 是 Unchecked,2 是 Checked,1 是部分 checked
True 表示当前是选中的状态,Qt.CheckState.Checked = 2,所以底下显示 2
False 表示当前是未选中的状态,Qt.CheckState.Unchecked = 0,所以底下显示 0
![](https://i-blog.csdnimg.cn/blog_migrate/b876be9fefd2407cdebfa19ddd331adb.png)
下拉列表框 QComboBox:显示下拉框选项
演示代码如下:
import sys
from PyQt6.QtWidgets import QApplication, QComboBox, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
widget=QComboBox()
widget.addItems(["One", "Two", "Three"])
widget.currentIndexChanged.connect(self.index_changed)
widget.currentTextChanged.connect(self.text_changed)
self.setCentralWidget(widget)
def index_changed(self, i):
print(i)
def text_changed(self, s):
print(s)
app=QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec()
实现效果
One的索引是0,Two的索引是1,Three的索引是2
列表框QListWidget
演示代码如下:
import sys
from PyQt6.QtWidgets import QApplication, QListWidget, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
widget=QListWidget()
widget.addItems(["One", "Two", "Three"])
widget.currentItemChanged.connect(self.index_changed)
widget.currentTextChanged.connect(self.text_changed)
self.setCentralWidget(widget)
def index_changed(self, i):
print(i.text())
def text_changed(self, s):
print(s)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
实现效果
单行输入框QLineEdit
演示代码如下:
import sys
from PyQt6.QtWidgets import QApplication, QLineEdit, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
widget=QLineEdit()
widget.setMaxLength(10)
widget.setPlaceholderText("Enter your text")
widget.returnPressed.connect(self.return_pressed)
widget.selectionChanged.connect(self.selection_changed)
widget.textChanged.connect(self.text_changed)
widget.textEdited.connect(self.text_edited)
self.setCentralWidget(widget)
def return_pressed(self):
print("Return pressed!")
self.centralWidget().setText("BOOM!")
def selection_changed(self):
print("Selection changed")
print(self.centralWidget().selectedText())
def text_changed(self, s):
print("Text changed...")
print(s)
def text_edited(self, s):
print("Text edited...")
print(s)
app = QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec()
实现效果:
随着输入的不同,显示效果不同
数值调整框QSpinBox
演示代码如下:
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QSpinBox
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
widget = QSpinBox()
#or:widget = QDoubleSpinBox()
widget.setMinimum(1)
widget.setMaximum(31)
#or:widget.setRange(1,31)
widget.setPrefix("1月")
widget.setSuffix("日")
widget.setSingleStep(1) #or e.g. 0.5 for QDoubleSpinBox
widget.valueChanged.connect(self.value_changed)
widget.textChanged.connect(self.value_changed_str)
self.setCentralWidget(widget)
def value_changed(self, i):
print(i)
def value_changed_str(self, s):
print(s)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
实现效果:上下键调整数值,范围在(1,31)
滑动条QSlider
演示代码如下
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QSlider
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
widget = QSlider()
widget.setMinimum(-10)
widget.setMaximum(3)
#or:widget ArithmeticError(-10,3)
widget.setSingleStep(3)
widget.valueChanged.connect(self.value_changed)
widget.sliderMoved.connect(self.slider_position)
widget.sliderPressed.connect(self.slider_pressed)
widget.sliderReleased.connect(self.slider_released)
self.setCentralWidget(widget)
def value_changed(self, i):
print(i)
def slider_position(self, p):
print("position", p)
def slider_pressed(self):
print("Pressed!")
def slider_released(self):
print("Released")
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
实现效果
拖动滑动条于不同位置,数字显示不同
旋转组件QSpin
演示代码如下:
import sys
from PyQt6.QtWidgets import QApplication, QDial, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
widget = QDial()
widget.setRange(-10, 100)
widget.setSingleStep(5)
widget.valueChanged.connect(self.value_changed)
widget.sliderMoved.connect(self.slider_position)
widget.sliderPressed.connect(self.slider_pressed)
widget.sliderReleased.connect(self.slider_released)
self.setCentralWidget(widget)
def value_changed(self, i):
print(i)
def slider_position(self, p):
print("position", p)
def slider_pressed(self):
print("Pressed!")
def slider_released(self):
print("Released")
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
显示效果:
拖动圆球显示不同位置(position)