Pythonic方式分配多个滑块回调(PyQt)?

时间:2015-07-28 18:15:23

标签: python pyqt4

我正在寻找一种更加pythonic的方法来将回调函数连接到我正在编写的基于PyQt的GUI上的多个滑块。必须有一个更好的方法来做到这一点!在相关的说明中,有没有办法使用字典迭代调用对象上的方法?假如我想迭代获取每个滑块的值?提前致谢。

我要压缩的代码(和pythonify!):

    self.ui.seg1Slider.valueChanged.connect(lambda value,segment='1': self.sliderMoved(value,segment))
    self.ui.seg2Slider.valueChanged.connect(lambda value,segment='2': self.sliderMoved(value,segment))
    self.ui.seg3Slider.valueChanged.connect(lambda value,segment='3': self.sliderMoved(value,segment))
    self.ui.seg4Slider.valueChanged.connect(lambda value,segment='4': self.sliderMoved(value,segment))
    self.ui.seg5Slider.valueChanged.connect(lambda value,segment='5': self.sliderMoved(value,segment))
    self.ui.seg6Slider.valueChanged.connect(lambda value,segment='6': self.sliderMoved(value,segment))
    self.ui.seg7Slider.valueChanged.connect(lambda value,segment='7': self.sliderMoved(value,segment))
    self.ui.seg8Slider.valueChanged.connect(lambda value,segment='8': self.sliderMoved(value,segment))
    self.ui.seg10Slider.valueChanged.connect(lambda value,segment='10': self.sliderMoved(value,segment))
    self.ui.seg11Slider.valueChanged.connect(lambda value,segment='11': self.sliderMoved(value,segment))
    self.ui.seg12Slider.valueChanged.connect(lambda value,segment='12': self.sliderMoved(value,segment))
    self.ui.seg13Slider.valueChanged.connect(lambda value,segment='13': self.sliderMoved(value,segment))
    self.ui.seg14Slider.valueChanged.connect(lambda value,segment='14': self.sliderMoved(value,segment))
    self.ui.seg15Slider.valueChanged.connect(lambda value,segment='15': self.sliderMoved(value,segment))
    self.ui.seg17Slider.valueChanged.connect(lambda value,segment='17': self.sliderMoved(value,segment))
    self.ui.seg18Slider.valueChanged.connect(lambda value,segment='18': self.sliderMoved(value,segment))
    self.ui.seg19Slider.valueChanged.connect(lambda value,segment='19': self.sliderMoved(value,segment))
    self.ui.seg20Slider.valueChanged.connect(lambda value,segment='20': self.sliderMoved(value,segment))
    self.ui.seg21Slider.valueChanged.connect(lambda value,segment='21': self.sliderMoved(value,segment))
    self.ui.seg22Slider.valueChanged.connect(lambda value,segment='22': self.sliderMoved(value,segment))
    self.ui.seg23Slider.valueChanged.connect(lambda value,segment='23': self.sliderMoved(value,segment))
    self.ui.seg24Slider.valueChanged.connect(lambda value,segment='24': self.sliderMoved(value,segment))
    self.ui.seg25Slider.valueChanged.connect(lambda value,segment='25': self.sliderMoved(value,segment))
    self.ui.seg26Slider.valueChanged.connect(lambda value,segment='26': self.sliderMoved(value,segment))
    self.ui.seg27Slider.valueChanged.connect(lambda value,segment='27': self.sliderMoved(value,segment))
    self.ui.seg28Slider.valueChanged.connect(lambda value,segment='28': self.sliderMoved(value,segment))
    self.ui.seg29Slider.valueChanged.connect(lambda value,segment='29': self.sliderMoved(value,segment))
    self.ui.seg30Slider.valueChanged.connect(lambda value,segment='30': self.sliderMoved(value,segment))
    self.ui.seg31Slider.valueChanged.connect(lambda value,segment='31': self.sliderMoved(value,segment))
    self.ui.seg32Slider.valueChanged.connect(lambda value,segment='32': self.sliderMoved(value,segment))

1 个答案:

答案 0 :(得分:1)

A loop with getattr will work, but it seems you don't have a complete range of numbers to work with (9 and 16 are missing), so you need to take account of that as well:

for segment in range(1, 33):
    if segment == 9 or segment == 16:
        continue
    slider = getattr(self.ui, 'seg%dSlider' % segment)
    slider.valueChanged.connect(
        lambda value, segment=str(segment): self.sliderMoved(value, segment))

However, a somewhat cleaner solution may be possible, since it looks like you are using Qt Designer to create the GUI. This means that the sliders will all get object names that match their attribute names. If you also make sure that all the sliders are put inside a container widget, you will then be able to do something like this:

for slider in self.ui.sliderContainer.findChildren(QtGui.QSlider):
    segment = slider.objectName()[3:][:-6]
    slider.valueChanged.connect(
        lambda value, segment=segment: self.sliderMoved(value, segment))

This is much more robust than the previous solution, because as long as you use the same naming format, you can safely add or remove sliders in Qt Designer without needing to update this code as well.

(PS: It's also possible to find children with object-names that match a given QRegExp).