1 表盘(QDial)


表盘 — QDial
python from PyQt6.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QApplication, QLCDNumber, QDial from PyQt6.QtCore import Qt import sys class QWindow(QMainWindow): def __init__(self): super().__init__() self.resize(180,150) self.initUI() def initUI(self): lcd = QLCDNumber(self) lcd.setDigitCount(3) # 创建表盘 dial = QDial() # 设置范围 dial.setRange(0,1000) # 设置是否显示刻度(缺省值为 False) dial.setNotchesVisible(True) # 设置是否可绕回 dial.setWrapping(False) # 设置刻度间隔(缺省值为 3.7 像素) dial.setNotchTarget(25) # 表盘转动信号 dial.valueChanged.connect(lcd.display) vbox = QVBoxLayout() vbox.addWidget(lcd) vbox.addWidget(dial) widget = QWidget() widget.setLayout(vbox) self.setCentralWidget(widget) self.show() def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()
DLC
[QDial Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qdial.html)

2 滑块(QSlider)


滑块 — QSlider
python from PyQt6.QtWidgets import QMainWindow, QApplication, QSlider, QLCDNumber from PyQt6.QtCore import Qt from PyQt6.QtGui import QPixmap import sys class QWindow(QMainWindow): def __init__(self): super().__init__() self.resize(180,150) self.initUI() def initUI(self): # 创建一个滑块 self.sld1 = QSlider(Qt.Orientation.Vertical,self) # Qt.Orientation.Vertical 创建垂直滑块 # Qt.Orientation.Horizontal 创建水平滑块 # 滑块位置大小 self.sld1.setGeometry(35,40,25,60) # 滑块最小值 self.sld1.setMinimum(0) # 滑块最大值 self.sld1.setMaximum(99) # 刻度位置 self.sld1.setTickPosition(QSlider.TickPosition.TicksLeft) # QSlider.TickPosition.NoTIcks 无刻度 # QSlider.TickPosition.TicksBothSides 两边都有刻度 # QSlider.TickPosition.TicksAbove 上方刻度 仅限水平滑块 # QSlider.TickPosition.TicksBelow 下方刻度 仅限水平滑块 # QSlider.TickPosition.TicksLeft 左侧刻度 仅限垂直滑块 # QSlider.TickPosition.TicksRight 右侧刻度 仅限垂直滑块 # 滑块信号 self.sld1.valueChanged[int].connect(self.changevalue) # sld.valueChanged 滑块值变化信号 # sld.sliderPressed 滑块按下信号 # sld.sliderReleased 滑块松开信号 # sld.sliderMoved 滑块拖动信号 self.sld2 = QSlider(Qt.Orientation.Horizontal,self) self.sld2.setGeometry(75,110,150,25) self.sld2.setMinimum(0) self.sld2.setMaximum(99) self.sld2.setTickPosition(QSlider.TickPosition.TicksBelow) self.sld2.valueChanged[int].connect(self.changevalue) self.lcd = QLCDNumber(self) self.lcd.setGeometry(70,40,150,60) self.resize(280,180) self.show() def changevalue(self,value): sender = self.sender() if sender == self.sld1: # 设置滑块数值 self.sld2.setValue(value) else: self.sld1.setValue(value) self.lcd.display(value) def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()
DLC
[QSlider Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qslider.html)

3 整数微调框(QSpinBox)


3.1 基本

整数微调框 — QSpinBox
python from PyQt6.QtWidgets import QMainWindow, QWidget, QApplication, QFormLayout, QSpinBox, QSlider from PyQt6.QtCore import Qt import sys class QWindow(QMainWindow): def __init__(self): super().__init__() self.resize(180,150) self.initUI() def initUI(self): # 创建整数微调框 self.spb = QSpinBox(self) # 设置整数微调框的区间(可拆写) self.spb.setRange(20, 200) # self.spb.setMinimum(20) # self.spb.setMaximum(200) # 设置整数微调框的调节单位 self.spb.setSingleStep(20) # 设置整数微调框可否循环调整 self.spb.setWrapping(False) # 缺省值就是 False # 最大值本身也同时成为最小值,元最小值成为第二小值(懂?) # 更改整数微调框值(缺省值为大于等于0的最小值) self.spb.setValue(20) # 整数微调框数值变化信号连接 self.spb.valueChanged.connect(self.spbvaluechanged) self.sld = QSlider(Qt.Orientation.Horizontal,self) self.sld.setRange(20, 200) self.sld.setValue(20) self.sld.valueChanged.connect(self.sldvaluechanged) formlayout = QFormLayout() formlayout.addRow('微调框',self.spb) formlayout.addRow('滑块',self.sld) widget = QWidget() widget.setLayout(formlayout) self.setCentralWidget(widget) self.show() def spbvaluechanged(self,value): self.sld.setValue(value) def sldvaluechanged(self,value): self.spb.setValue(value) def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()
DLC
[QSpinBox Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qspinbox.html)

3.2 显示信息

整数微调框 — QSpinBox
python from PyQt6.QtWidgets import QMainWindow, QWidget, QApplication, QFormLayout, QSpinBox, QSlider from PyQt6.QtCore import Qt import sys # 自定义整数微调框 class Diyspb(QSpinBox): # 提取整数微调框信息 def valueFromText(self,str): regExp = QRegExp('(\\d+)(\\s*[xx]\\s*\\d+)?') if regExp.exactMatch(str): return int(regExp.cap(1)) else: return 0 # 将信息处理并返回 def textFromValue(self,num): if num == 200: return '' elif num % 10 == 0: return str(int(num/10)) + ' x 10' else: return str(num) class QWindow(QMainWindow): def __init__(self): super().__init__() self.resize(250,100) self.initUI() def initUI(self): # 创建自定义整数微调框 self.dspb = Diyspb(self) self.dspb.setRange(0, 200) self.dspb.setSingleStep(20) self.dspb.setWrapping(False) self.dspb.valueChanged.connect(self.dspbvaluechanged) # 整数微调框最小值时显示字符串 self.dspb.setSpecialValueText('选择使用道具数') self.sld = QSlider(Qt.Orientation.Horizontal,self) self.sld.setRange(0, 200) self.sld.setValue(0) self.sld.valueChanged.connect(self.sldvaluechanged) formlayout = QFormLayout() formlayout.addRow('微调框',self.dspb) formlayout.addRow('滑块',self.sld) widget = QWidget() widget.setLayout(formlayout) self.setCentralWidget(widget) self.show() def dspbvaluechanged(self,value): self.sld.setValue(value) # 判断数值区间 if self.dspb.value() == self.dspb.maximum(): # 整数微调框前缀 self.dspb.setPrefix('使用全部道具') # 整数微调框后缀 self.dspb.setSuffix('') elif self.dspb.minimum() < self.dspb.value() < self.dspb.maximum(): self.dspb.setPrefix('使用 ') self.dspb.setSuffix(' 个道具') def sldvaluechanged(self,value): self.dspb.setValue(value) def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()

4 小数微调框(QDoubleSpinBox)


  • 使用方式基本与整数微调框一致;
整数微调框 — QSpinBox
python from PyQt6.QtWidgets import QMainWindow, QWidget, QApplication, QFormLayout, QLabel, QDoubleSpinBox, QSlider from PyQt6.QtCore import Qt import sys class QWindow(QMainWindow): def __init__(self): super().__init__() self.resize(180,150) self.initUI() def initUI(self): self.dspb = QDoubleSpinBox(self) self.dspb.setRange(0,10) self.dspb.setSingleStep(0.2) self.dspb.setWrapping(False) self.dspb.setValue(0) # 设置小数微调框小数位数(缺省值为2) self.dspb.setDecimals(1) self.dspb.valueChanged.connect(self.dspbvaluechanged) self.sld1 = QSlider(Qt.Orientation.Horizontal,self) self.sld1.setRange(0, 10) self.sld1.setValue(0) self.sld1.valueChanged.connect(self.sld1valuechanged) self.sld2 = QSlider(Qt.Orientation.Horizontal,self) self.sld2.setRange(1, 10) self.sld2.setValue(1) self.sld2.valueChanged.connect(self.sld2valuechanged) self.lb = QLabel('精度值-1') formlayout = QFormLayout() formlayout.addRow('小数调整',self.dspb) formlayout.addRow('整数调整',self.sld1) formlayout.addRow(self.lb,self.sld2) widget = QWidget() widget.setLayout(formlayout) self.setCentralWidget(widget) self.show() def dspbvaluechanged(self,value): self.sld1.setValue(value) def sld1valuechanged(self,value): self.dspb.setValue(value) def sld2valuechanged(self,value): self.dspb.setDecimals(value) self.lb.setText(f'精度值-{value}') def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()
DLC
[QDoubleSpinBox Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qdoublespinbox.html)

5 日历(QCalendarWidget)


日历 — QCalendarWidget
python from PyQt6.QtWidgets import QMainWindow, QWidget, QApplication, QVBoxLayout, QLabel, QCalendarWidget from PyQt6.QtCore import Qt, QDate import sys class QWindow(QMainWindow): def __init__(self): super().__init__() self.resize(180,150) self.initUI() def initUI(self): # 创建日历 self.cld = QCalendarWidget() # 设置日期范围 self.cld.setDateRange(QDate(2020, 1, 1),QDate(2025, 12, 31)) # self.cld.setMinimumDate(QDate(2020, 1, 1)) 缺省值为 QDate(-4714, 11, 25) # self.cld.setMaximumDate(QDate(2025, 12, 31)) 缺省值为 QDate(9999, 12, 31) # 设置日历的选择方式 self.cld.setSelectionMode(QCalendarWidget.SelectionMode.SingleSelection) # QCalendarWidget.SelectionMode.NoSelection 不可选择日期 # QCalendarWidget.SelectionMode.SingleSelection 可选择单个日期 # 设置每周第一天周几(缺省值为周一) self.cld.setFirstDayOfWeek(Qt.DayOfWeek.Sunday) # 选择日期 today = QDate.currentDate().toJulianDay() yesterday = QDate.currentDate().fromJulianDay(today-1) self.cld.setSelectedDate(yesterday) # 选择页面(和选择日期,择一使用) self.cld.setCurrentPage(2021,11) # 列标题(月份)显示方式 self.cld.setHorizontalHeaderFormat(QCalendarWidget.HorizontalHeaderFormat.LongDayNames) # QCalendarWidget.HorizontalHeaderFormat.SingleLetterDayNames 一 M # QCalendarWidget.HorizontalHeaderFormat.ShortDayNames 周一 Mon 缺省值 # QCalendarWidget.HorizontalHeaderFormat.LongDayNames 星期一 Monday # QCalendarWidget.HorizontalHeaderFormat.NoHorizontalHeader 不显示 # 行标题(周数)显示方式 self.cld.setVerticalHeaderFormat(QCalendarWidget.VerticalHeaderFormat.NoVerticalHeader) # QCalendarWidget.VerticalHeaderFormat.NoVerticalHeader 隐藏标题 # QCalendarWidget.VerticalHeaderFormat.ISOWeekNumbers 显示ISO周数 缺省值 # 设置日历网格 self.cld.setGridVisible(True) # 日历点击信号传递 self.cld.clicked.connect(self.cldclicked) self.lb = QLabel('请选择日期') vbl = QVBoxLayout() vbl.addWidget(self.cld) vbl.addWidget(self.lb) widget = QWidget() widget.setLayout(vbl) self.setCentralWidget(widget) self.show() def cldclicked(self,date): # 返回年份月份 self.lb.setText(date.toString('yyyy-MM-dd dddd') + '\n' + str(self.cld.yearShown()) + '年' + str(self.cld.monthShown()) + '月') def main(): app = QApplication(sys.argv) qw = QWindow() sys.exit(app.exec()) if __name__ == '__main__': main()
DLC
【pyqt5学习】——日历控件calendarWidget设置单元格格式(前景、背景)、日历控件属性编辑_有情怀的机械男的博客-CSDN博客_pyqt 日历 [QCalendarWidget Class Qt Widgets 6.3.2](https://doc.qt.io/qt-6/qcalendarwidget.html)