How in pyqt change text in QPlainTextEdit() if slider's value is 1, and don't change if the value is 0?

后端 未结 1 1061
遥遥无期
遥遥无期 2021-01-17 06:52
message = QtGui.QPlainTextEdit()
slider = QtGui.QSlider()
def slider_func():
    if slider.value() == 0:
       \'\'\'what to write here ?\'\'\'

    if slider.value         


        
相关标签:
1条回答
  • 2021-01-17 07:13

    Here is an example of how to watch the value of the slider, and modify a QPlainText when the value is at certain values.

    from PyQt4.QtGui import *
    from PyQt4.Qt import *
    import sys
    
    
    class Window(QWidget):
        def __init__(self, *args, **kwargs):
            super(Window, self).__init__(*args, **kwargs)
    
            layout = QVBoxLayout()
            self.setLayout(layout)
    
            self.text = "Please enter some text"
            self.edit = QPlainTextEdit(self.text)
            layout.addWidget(self.edit)
    
            slider = QSlider(Qt.Horizontal)
            layout.addWidget(slider)
            slider.valueChanged.connect(self.change_value)
    
        def change_value(self, value):
            if value == 0:
                self.edit.setPlainText(self.text)
            elif value == 1:
                self.edit.setPlainText(self.text[::-1])
            elif value > 1:
                self.edit.setPlainText("Slider value is above 1, value: {0}".format(value))
    
    
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())
    

    When the QSlider.valueChanged signal is emitted, the new value is carried with it. So in my slot Window.change_value(self, value) the value positional argument will always contain the new QSlider value; which allows you to test against it.

    0 讨论(0)
提交回复
热议问题