如果滑块的值为1,pyqt如何更改QPlainTextEdit()中的文本,如果值为0,则不会更改?

时间:2016-06-04 17:34:53

标签: python pyqt pyqt4

message = QtGui.QPlainTextEdit()
slider = QtGui.QSlider()
def slider_func():
    if slider.value() == 0:
       '''what to write here ?'''

    if slider.value() == 1:
        m = str(message.toPlainText())
        translated = ''
        i = len(m) - 1
        while i >= 0:
            translated = translated + m[i]
            i = i - 1
        message.setPlainText(translated)
QtCore.QObject.connect(slider, QtCore.SIGNAL('valueChanged(int)'),
                    slider_func)

当我将滑块设置为第一个刻度时,文本会反转。但是,当我把它设置为开始时,文本仍然是逆转的。我知道问题是,一旦文本被更改,它就会设置文本编辑行。任何想法如何解决? Example1Example2

1 个答案:

答案 0 :(得分:0)

以下是如何观看滑块值的示例,并在值为特定值时修改QPlainText

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_())

当发出QSlider.valueChanged信号时,新值随之携带。因此,在我的广告位Window.change_value(self, value)中,value位置参数将始终包含新的QSlider值;它允许你测试它。