背景不会改变颜色

时间:2015-12-26 20:42:18

标签: python pyqt

我在使用pyqt的python上使用gui-application时遇到了一些麻烦。

我需要更改setStyleSheet("QPlainTextEdit{background-color:rgb(0,0,0)}")中的背景颜色值,这些值会因移动滑块而发生变化。

import sys
from PyQt4 import QtGui, QtCore
try:  
    from PyQt4.QtCore import QString  
except ImportError:  
    # we are using Python3 so QString is not defined  
    QString = str 


class ColorViewer(QtGui.QWidget):       
    def __init__(self):
        QtGui.QWidget.__init__(self)

        red=0
        green=0
        blue=0

        self.resize(700, 200)
        self.setWindowTitle(self.trUtf8('Colorviewer'))

        self.square = QtGui.QPlainTextEdit(self)
        self.square.setGeometry(455, 20, 225, 160)
        self.square.setStyleSheet("QPlainTextEdit{background-color:rgb(0,0,0)}")
        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('cleanlooks'))

        self.redtext = QtGui.QPushButton(self.trUtf8('Красный'), self)
        self.redtext.setCheckable(False)
        self.redtext.move(10, 20)
        self.redspinbox = QtGui.QSpinBox(self)
        self.redspinbox.setEnabled(False)
        self.redspinbox.setMinimum(0)
        self.redspinbox.setMaximum(255)
        self.redspinbox.move(110, 22)
        self.redspinbox.setValue(0)
        self.redslider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        self.redslider.setMinimum(0)
        self.redslider.setMaximum(255)  
        self.redslider.setGeometry(160, 26, 255, 14)
        self.redspinbox.valueChanged.connect(self.redspinbox.setValue)
        self.connect(self.redslider,QtCore.SIGNAL('sliderMoved(int)'),self.redspinbox,QtCore.SLOT('setValue(int)'))

        self.greentext = QtGui.QPushButton(self.trUtf8('Зеленый'), self)
        self.greentext.setCheckable(False)
        self.greentext.move(10, 90)
        self.greenspinbox = QtGui.QSpinBox(self)
        self.greenspinbox.setEnabled(False)
        self.greenspinbox.setMinimum(0)
        self.greenspinbox.setMaximum(255)
        self.greenspinbox.move(110, 162)
        self.greenspinbox.setValue(0)
        self.greenslider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        self.greenslider.setMinimum(0)
        self.greenslider.setMaximum(255)
        self.greenslider.setGeometry(160, 166, 255, 14)        
        self.connect(self.greenslider,QtCore.SIGNAL('sliderMoved(int)'),self.greenspinbox,QtCore.SLOT('setValue(int)'))

        self.bluetext = QtGui.QPushButton(self.trUtf8('Синий'), self)
        self.bluetext.setCheckable(False)
        self.bluetext.move(10, 160)
        self.bluespinbox = QtGui.QSpinBox(self)
        self.bluespinbox.setEnabled(False)
        self.bluespinbox.setMinimum(0)
        self.bluespinbox.setMaximum(255)
        self.bluespinbox.move(110, 92)
        self.bluespinbox.setValue(0)
        self.blueslider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        self.blueslider.setMinimum(0)
        self.blueslider.setMaximum(255)
        self.blueslider.setGeometry(160, 96, 255, 14)
        self.connect(self.blueslider,QtCore.SIGNAL('sliderMoved(int)'),self.bluespinbox,QtCore.SLOT('setValue(int)'))

        self.connect(self.redslider,QtCore.SIGNAL('sliderMoved(int)'), self.ColorChanged(int))



    def ColorChanged(self):
        r=self.redslider.value()
        g=self.greenslider.value()
        b=self.blueslider.value()
        str_col = str
        str_col+="QPlainTextEdit{background-color:rgb("+str.r+","+str.g+","+str.b+")}"+")}"
        self.square.setStyleSheet(str_col) 




app = QtGui.QApplication(sys.argv)
cv = ColorViewer()
cv.show()
app.exec_()

我有以下错误:

Traceback (most recent call last):
  File "C:\Python34\colorwidget.py", line 91, in <module>
    cv = ColorViewer()
  File "C:\Python34\colorwidget.py", line 75, in __init__
    self.connect(self.redslider,QtCore.SIGNAL('sliderMoved(int)'), self.ColorChanged(int))
TypeError: ColorChanged() takes 1 positional argument but 2 were given

我尝试了更多方法,但我认为,这更容易解决我的任务。 如何解决?

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。

我们正在改变

self.connect(self.redslider,QtCore.SIGNAL('sliderMoved(int)'), self.ColorChanged(int))

self.connect(self.redslider,QtCore.SIGNAL('sliderMoved(int)'), self.ColorChanged)

str_col+="QPlainTextEdit{background-color:rgb("+str.r+","+str.g+","+str.b+")}"+")}"

str_col=("QPlainTextEdit{background-color:rgb(" + str(r) + "," + str(g) + "," + str(b) + ")}")