PyQt5:颜色对话框中的 "No Fill"?
PyQt5: "No Fill" in Color Dialog?
我正在用 PyQt5 编写 GUI,我需要一个颜色选择器。
到目前为止,我使用 QColorDialog Class,它可以很好地用于 select 颜色 - 但我的问题是似乎没有办法 select "no color" (或"no fill",就像从 PowerPoint 或 Adobe Illustrator 中知道的那样)。
如何实现到select"no color"? (文档只提到了透明标志,但这对我没有帮助......)
如果您不介意使用非本机对话框,自定义它非常容易。
下面是一个非常基本的实现,展示了如何嵌入现有对话框,并在底部添加一个额外的 "No Color" 按钮。其余的实施留作 reader...
的练习
from PyQt5 import QtCore, QtWidgets
class ColorDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
widget = QtWidgets.QColorDialog()
widget.setWindowFlags(QtCore.Qt.Widget)
widget.setOptions(
QtWidgets.QColorDialog.DontUseNativeDialog |
QtWidgets.QColorDialog.NoButtons)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(widget)
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(QtWidgets.QPushButton('No Color'))
hbox.addWidget(QtWidgets.QPushButton('Cancel'))
hbox.addWidget(QtWidgets.QPushButton('Ok'))
layout.addLayout(hbox)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
dialog = ColorDialog()
dialog.show()
sys.exit(app.exec_())
我正在用 PyQt5 编写 GUI,我需要一个颜色选择器。 到目前为止,我使用 QColorDialog Class,它可以很好地用于 select 颜色 - 但我的问题是似乎没有办法 select "no color" (或"no fill",就像从 PowerPoint 或 Adobe Illustrator 中知道的那样)。
如何实现到select"no color"? (文档只提到了透明标志,但这对我没有帮助......)
如果您不介意使用非本机对话框,自定义它非常容易。
下面是一个非常基本的实现,展示了如何嵌入现有对话框,并在底部添加一个额外的 "No Color" 按钮。其余的实施留作 reader...
的练习from PyQt5 import QtCore, QtWidgets
class ColorDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
widget = QtWidgets.QColorDialog()
widget.setWindowFlags(QtCore.Qt.Widget)
widget.setOptions(
QtWidgets.QColorDialog.DontUseNativeDialog |
QtWidgets.QColorDialog.NoButtons)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(widget)
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(QtWidgets.QPushButton('No Color'))
hbox.addWidget(QtWidgets.QPushButton('Cancel'))
hbox.addWidget(QtWidgets.QPushButton('Ok'))
layout.addLayout(hbox)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
dialog = ColorDialog()
dialog.show()
sys.exit(app.exec_())