我在 PyQt5 中有一个旋转框,如下所示。我怎样才能逆转选择?即,如果我单击向下箭头,值就会上升,反之亦然。
最佳答案
一个可能的解决方案是覆盖 stepBy()
和 stepEnabled()
方法:
import sys
from PyQt5 import QtWidgets
class ReverseSpinBox(QtWidgets.QSpinBox):
def stepEnabled(self):
if self.wrapping() or self.isReadOnly():
return super().stepEnabled()
ret = QtWidgets.QAbstractSpinBox.StepNone
if self.value() > self.minimum():
ret |= QtWidgets.QAbstractSpinBox.StepUpEnabled
if self.value() < self.maximum():
ret |= QtWidgets.QAbstractSpinBox.StepDownEnabled
return ret
def stepBy(self, steps):
return super().stepBy(-steps)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = ReverseSpinBox()
w.resize(320, 20)
w.show()
sys.exit(app.exec_())
关于python - 如何在 PyQt5 中反转旋转框选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62821244/