我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用PyQt5.QtGui.QPalette()。
def checkBoxToggled(self): checkBox = self.sender() window = self.mainWindowForCheckBoxMap.get(checkBox) if not window: translator = QTranslator() translator.load(self.qmFileForCheckBoxMap[checkBox]) QApplication.installTranslator(translator) # Because we will be installing an event filter for the main window # it is important that this instance isn't garbage collected before # the main window when the program terminates. We ensure this by # making the main window a child of this one. window = MainWindow(self) window.setPalette(QPalette(self.colorForLanguage(checkBox.text()))) window.installEventFilter(self) self.mainWindowForCheckBoxMap[checkBox] = window window.setVisible(checkBox.isChecked())
def set_mainwindow_palette(fanart, first_time=None): logger.info('\n{0}: mainwindow background\n'.format(fanart)) if fanart.endswith('default.jpg'): default_dir, default_img = os.path.split(fanart) default_fit = os.path.join(default_dir, 'default_fit.jpg') if not os.path.exists(default_fit): ui.image_fit_option(fanart, default_fit, fit_size=1) fanart = default_fit if not os.path.isfile(fanart) or ui.keep_background_constant: fanart = ui.default_background if os.path.isfile(fanart): if not ui.keep_background_constant or first_time: palette = QtGui.QPalette() palette.setBrush(QtGui.QPalette.Background, QtGui.QBrush(QtGui.QPixmap(fanart))) MainWindow.setPalette(palette) ui.current_background = fanart
def __init__(self, parent=None): super(AnnotatedProgressBar, self).__init__(parent) self.value = 0 self._ticks = 1 self._update_tick_labels() self._transitions = (0, 1) self.setMinimumHeight(40) self.setMinimumWidth(300) self.palette = QtGui.QPalette()
def setColor(self): color = QColorDialog.getColor(Qt.green, self) if color.isValid(): self.colorLabel.setText(color.name()) self.colorLabel.setPalette(QPalette(color)) self.colorLabel.setAutoFillBackground(True)
def apply_palette(self, palette_name): palette_def = self._api.opt.general['palettes'][palette_name] palette = QtGui.QPalette() for color_type, color_value in palette_def.items(): if '+' in color_type: group_name, role_name = color_type.split('+') else: group_name = '' role_name = color_type target_group = getattr(QtGui.QPalette, group_name, None) target_role = getattr(QtGui.QPalette, role_name, None) if target_group is not None and target_role is not None: palette.setColor( target_group, target_role, QtGui.QColor(*color_value)) elif target_role is not None: palette.setColor(target_role, QtGui.QColor(*color_value)) self.setPalette(palette) self.update()
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.centralWidget = QWidget() self.setCentralWidget(self.centralWidget) self.createGroupBox() listWidget = QListWidget() for le in MainWindow.listEntries: listWidget.addItem(self.tr(le)) mainLayout = QVBoxLayout() mainLayout.addWidget(self.groupBox) mainLayout.addWidget(listWidget) self.centralWidget.setLayout(mainLayout) exitAction = QAction(self.tr("E&xit"), self, triggered=QApplication.instance().quit) fileMenu = self.menuBar().addMenu(self.tr("&File")) fileMenu.setPalette(QPalette(Qt.red)) fileMenu.addAction(exitAction) self.setWindowTitle(self.tr("Language: %s") % self.tr("English")) self.statusBar().showMessage(self.tr("Internationalization Example")) if self.tr("LTR") == "RTL": self.setLayoutDirection(Qt.RightToLeft)
def draw_ticks(self, painter): w = self.width() h = self.height() tick_step = int(round(w / self._ticks)) f1 = int(((w / float(self._ticks*1000)) * self._transitions[0]*1000)) f2 = int(((w / float(self._ticks*1000)) * self._transitions[1]*1000)) # the contract indicator window painter.setPen(QtGui.QColor(0, 0, 0, 0)) painter.setBrush(QtGui.QColor(180, 80, 80, 140)) painter.drawRect(f1, 0, f2-f1, h) painter.setPen(self.palette.color(QtGui.QPalette.Text)) painter.setFont(self.font()) # draw the "contact" and "rest" text for t, l in zip(self._transitions, ['contract', 'rest']): x = int(((w / float(self._ticks*1000)) * (t*1000.))) metrics = painter.fontMetrics() fw = metrics.width(l) painter.drawText(x-fw/2, h/2, l) # draw the ticks marking each second j = 0 for i in range(tick_step, self._ticks*tick_step, tick_step): painter.drawLine(i, h-5, i, h) metrics = painter.fontMetrics() fw = metrics.width(self.tick_labels[j]) painter.drawText(i-fw/2, h-7, self.tick_labels[j]) j += 1
def __init__(self, parent=None, interval=50): QWidget.__init__(self, parent) palette = QPalette(self.palette()) palette.setColor(palette.Background, Qt.transparent) self.setPalette(palette) self.counter = 0 self.interval = interval
def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.setEnabled(True) MainWindow.resize(640, 480) MainWindow.setMinimumSize(QtCore.QSize(640, 480)) MainWindow.setMaximumSize(QtCore.QSize(640, 480)) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.video_holder = QtWidgets.QLabel(self.centralwidget) self.video_holder.setGeometry(QtCore.QRect(0, 10, 640, 480)) self.video_holder.setMinimumSize(QtCore.QSize(640, 480)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(252, 252, 252)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(241, 84, 87)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(252, 252, 252)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(241, 84, 87)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(241, 84, 87)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(241, 84, 87)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) self.video_holder.setPalette(palette) self.video_holder.setAutoFillBackground(True) self.video_holder.setObjectName("video_holder") MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow)
def set_bg(self, _bg="jhin.png"): """set widget background to given jpg/png/color""" palette = QtGui.QPalette() if "." in _bg: if "\\" in _bg: img = imgGetter.getimg("fullpath", _bg) # elif _bg in os.listdir(Data.BACKGROUNDPATH): else: img = imgGetter.getimg("bg", _bg) # rescale for window if img.width() == 0 or img.height() == 0: img = QtGui.QColor("#123456") else: if self.width() / self.height() >= img.width() / img.height(): img = img.scaledToWidth(self.width()) else: img = img.scaledToHeight(self.height()) else: img = QtGui.QColor(_bg) palette.setBrush(QtGui.QPalette.Background, QtGui.QBrush(img)) self.setPalette(palette) # Keybinding
def set_mainwindow_palette(fanart,first_time=None): if not os.path.isfile(fanart) or ui.keep_background_constant: fanart = ui.default_background if os.path.isfile(fanart): if not ui.keep_background_constant or first_time: palette = QtGui.QPalette() palette.setBrush(QtGui.QPalette.Background, QtGui.QBrush(QtGui.QPixmap(fanart))) MainWindow.setPalette(palette) ui.current_background = fanart
def main(): sys.excepthook = excepthook settings = read_settings() if settings["Appearance"]["style"]: QApplication.setStyle(settings["Appearance"]["style"]) app = QApplication(sys.argv) if settings["Appearance"]["palette"]: app.setPalette(QPalette(QColor(settings["Appearance"]["palette"]))) IntroWindow() sys.exit(app.exec_())
def addToLibrary(self): global home #self.LibraryDialog.show() self.LibraryDialog = QtWidgets.QDialog() self.LibraryDialog.setObjectName(_fromUtf8("Dialog")) self.LibraryDialog.resize(582, 254) self.listLibrary = QtWidgets.QListWidget(self.LibraryDialog) self.listLibrary.setGeometry(QtCore.QRect(20, 20, 341, 192)) self.listLibrary.setObjectName(_fromUtf8("listLibrary")) self.AddLibraryFolder = QtWidgets.QPushButton(self.LibraryDialog) self.AddLibraryFolder.setGeometry(QtCore.QRect(420, 50, 94, 27)) self.AddLibraryFolder.setObjectName(_fromUtf8("AddLibraryFolder")) self.RemoveLibraryFolder = QtWidgets.QPushButton(self.LibraryDialog) self.RemoveLibraryFolder.setGeometry(QtCore.QRect(420, 90, 94, 27)) self.RemoveLibraryFolder.setObjectName(_fromUtf8("RemoveLibraryFolder")) self.LibraryClose = QtWidgets.QPushButton(self.LibraryDialog) self.LibraryClose.setGeometry(QtCore.QRect(420, 130, 94, 27)) self.LibraryClose.setObjectName(_fromUtf8("LibraryClose")) self.LibraryDialog.setWindowTitle(_translate("Dialog", "Library Setting", None)) self.AddLibraryFolder.setText(_translate("Dialog", "ADD", None)) self.RemoveLibraryFolder.setText(_translate("Dialog", "Remove", None)) self.LibraryClose.setText(_translate("Dialog", "Close", None)) self.LibraryDialog.show() file_name = os.path.join(home,'local.txt') if os.path.exists(file_name): lines = open_files(file_name,True) self.listLibrary.clear() for i in lines: i = i.replace('\n','') self.listLibrary.addItem(i) self.AddLibraryFolder.clicked.connect(self.addFolderLibrary) self.RemoveLibraryFolder.clicked.connect(self.removeFolderLibrary) self.LibraryClose.clicked.connect(self.LibraryDialog.close) self.LibraryClose.setStyleSheet("font: bold 12px;color:white;background:rgba(0,0,0,30%);border:rgba(0,0,0,30%);border-radius: 3px;") self.RemoveLibraryFolder.setStyleSheet("font: bold 12px;color:white;background:rgba(0,0,0,30%);border:rgba(0,0,0,30%);border-radius: 3px;") self.AddLibraryFolder.setStyleSheet("font: bold 12px;color:white;background:rgba(0,0,0,30%);border:rgba(0,0,0,30%);border-radius: 3px;") self.listLibrary.setStyleSheet("""QListWidget{ font: bold 12px;color:white;background:rgba(0,0,0,30%); border:rgba(0,0,0,30%);border-radius: 3px; } QListWidget:item:selected:active { background:rgba(0,0,0,20%); color: violet; } QListWidget:item:selected:inactive { border:rgba(0,0,0,30%); } QMenu{ font: bold 12px;color:black;background-image:url('1.png'); } """) picn = self.default_background palette = QtGui.QPalette() palette.setBrush(QtGui.QPalette.Background,QtGui.QBrush(QtGui.QPixmap(picn))) self.LibraryDialog.setPalette(palette)