本文通过Python3+PyQt5实现自定义部件–分数滑块。它既能支持键盘也支持鼠标,使用物理(视口)坐标通过绘制方式显示。
#!/usr/bin/env python3
import platform
from PyQt5.QtCore import (QPointF, QRectF, QSize, Qt,pyqtSignal)
from PyQt5.QtWidgets import (QApplication, QDialog,QSizePolicy,
QGridLayout, QLCDNumber, QLabel,
QSpinBox, QWidget)
from PyQt5.QtGui import QColor,QFont,QPainter,QFontMetricsF,QPalette, QPolygonF
X11 = True
try:
from PyQt5.QtGui import qt_x11_wait_for_window_manager
except ImportError:
X11 = False
class FractionSlider(QWidget):
XMARGIN = 12.0
YMARGIN = 5.0
WSTRING = "999"
valueChanged = pyqtSignal(int,int)
def __init__(self, numerator=0, denominator=10, parent=None):
super(FractionSlider, self).__init__(parent)
self.__numerator = numerator
self.__denominator = denominator
self.setFocusPolicy(Qt.WheelFocus)
self.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding,
QSizePolicy.Fixed))
def decimal(self):
return self.__numerator / float(self.__denominator)
def fraction(self):
return self.__numerator, self.__denominator
def sizeHint(self):
return self.minimumSizeHint()
def minimumSizeHint(self):