Python PyQt5.QtGui 模块,QPalette() 实例源码
我们从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 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 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 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 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 __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))
painter.setBrush(QtGui.QColor(180, 80, 140))
painter.drawRect(f1, 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,
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 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 __init__(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 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))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, 87))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, 87))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.disabled, 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 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 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, 27))
self.RemoveLibraryFolder.setobjectName(_fromUtf8("RemoveLibraryFolder"))
self.LibraryClose = QtWidgets.QPushButton(self.LibraryDialog)
self.LibraryClose.setGeometry(QtCore.QRect(420, 130, 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,30%);border:rgba(0,30%);border-radius: 3px;")
self.RemoveLibraryFolder.setStyleSheet("font: bold 12px;color:white;background:rgba(0,30%);border-radius: 3px;")
self.AddLibraryFolder.setStyleSheet("font: bold 12px;color:white;background:rgba(0,30%);border-radius: 3px;")
self.listLibrary.setStyleSheet("""QListWidget{
font: bold 12px;color:white;background:rgba(0,30%);
border:rgba(0,30%);border-radius: 3px;
}
QListWidget:item:selected:active {
background:rgba(0,20%);
color: violet;
}
QListWidget:item:selected:inactive {
border:rgba(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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。