更改 Python 中 Widget Window 的样式和背景颜色
Change the style and background color of Widget Window in Python
def color_picker(self):
color = QtGui.QColorDialog.getColor()
self.setStyleSheet("QWidget { background-color: %s}" % color.name())
global selectedcolor
selectedcolor=color.name()
print(selectedcolor)
global RGBcolorfromcolorpicker
RGBcolorfromcolorpicker=selectedcolor.lstrip('#')
#This line doesn't work.
self.QColorDialog.setStyleSheet('QTabBar::tab{background-color: red;}')
self.send_rgb_color()
您可以使用 class 的对象,而不是只使用静态方法,在这种情况下,我创建了一个 class,它已经实现了更改颜色的功能:
from PyQt4 import QtCore, QtGui
class ColorDialog(QtGui.QColorDialog):
def __init__(self, initial=QtGui.QColor(), parent=None):
super(ColorDialog, self).__init__(parent)
self.setOption(QtGui.QColorDialog.DontUseNativeDialog)
self.currentColorChanged.connect(self.onCurrentColorChanged)
self.onCurrentColorChanged(self.currentColor())
@QtCore.pyqtSlot(QtGui.QColor)
def onCurrentColorChanged(self, color):
self.setStyleSheet("QColorDialog { background-color: %s}" % color.name())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = ColorDialog()
if w.exec_() == QtGui.QDialog.Accepted:
print(w.currentColor())
def color_picker(self):
color = QtGui.QColorDialog.getColor()
self.setStyleSheet("QWidget { background-color: %s}" % color.name())
global selectedcolor
selectedcolor=color.name()
print(selectedcolor)
global RGBcolorfromcolorpicker
RGBcolorfromcolorpicker=selectedcolor.lstrip('#')
#This line doesn't work.
self.QColorDialog.setStyleSheet('QTabBar::tab{background-color: red;}')
self.send_rgb_color()
您可以使用 class 的对象,而不是只使用静态方法,在这种情况下,我创建了一个 class,它已经实现了更改颜色的功能:
from PyQt4 import QtCore, QtGui
class ColorDialog(QtGui.QColorDialog):
def __init__(self, initial=QtGui.QColor(), parent=None):
super(ColorDialog, self).__init__(parent)
self.setOption(QtGui.QColorDialog.DontUseNativeDialog)
self.currentColorChanged.connect(self.onCurrentColorChanged)
self.onCurrentColorChanged(self.currentColor())
@QtCore.pyqtSlot(QtGui.QColor)
def onCurrentColorChanged(self, color):
self.setStyleSheet("QColorDialog { background-color: %s}" % color.name())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = ColorDialog()
if w.exec_() == QtGui.QDialog.Accepted:
print(w.currentColor())