Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在QToolButton上显示双色字体_Python_Pyqt4_Qtoolbutton - Fatal编程技术网

Python 在QToolButton上显示双色字体

Python 在QToolButton上显示双色字体,python,pyqt4,qtoolbutton,Python,Pyqt4,Qtoolbutton,我想要一个按钮,显示两种颜色的文本。我尝试了以下方法: import sys from PyQt4 import QtCore,QtGui class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def clicked(self,name): self.drop_btns_hide

我想要一个按钮,显示两种颜色的文本。我尝试了以下方法:

import sys
from PyQt4 import QtCore,QtGui


class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()


    def clicked(self,name):
        self.drop_btns_hide(self.dropdown)
        print("Clicked on {}".format(name))        
        
        
    def initUI(self):
        
        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
        self.phslst = ['abc','def','ghi','jkl']
        
        self.setToolTip('This is a <b>QWidget</b> widget')
         
        btn = QtGui.QToolButton(self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')        
        btn.resize(btn.sizeHint())
        btn.move(50, 50)       
        text = QtGui.QTextDocument()
        text.setHtml('<b>'
                     '<font color="#0085C8" size="6" face="Avenir">'
                     'Select Phase'
                     '</font>'
                     '<font color="#d8d8d8" size="6">'
                     '&#9660;'
                     '</font>'
                     '</b>')
        pixmap = QtGui.QPixmap(text.size().width(), text.size().height())
        pixmap.fill(QtCore.Qt.transparent)
        painter = QtGui.QPainter(pixmap)
        text.drawContents(painter, QtCore.QRectF(pixmap.rect()))
        icon = QtGui.QIcon(pixmap)
        self.select_icon = icon
        btn.setIcon(icon)
        btn.setIconSize(pixmap.size())        
        
        btnA = QtGui.QPushButton('Test', self)
        btnA.setToolTip('This is a <b>QPushButton</b> widget')
        btnA.resize(btnA.sizeHint())
        btnA.move(10, 100)
        
        self.setGeometry(300, 300, 250, 250)
        self.setWindowTitle('Tooltips')    
        self.show()

        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

我不明白为什么。

由于效率原因,QPaint方法所做的是累积指令,因此不会立即执行。这就是在您的案例中发生的情况,因为QPainter仍然处于活动状态,并且具有QPixmap的属性,这就是为什么会出现错误,因此在本例中,解决方案是调用end()方法:

text=QtGui.QTextDocument()
text.setHtml(“”
''
“选择阶段”
''
''
'▼'
''
'')
pixmap=QtGui.QPixmap(text.size().toSize())
pixmap.fill(QtCore.Qt.transparent)
painter=QtGui.QPainter(pixmap)
text.drawContents(painter,QtCore.QRectF(pixmap.rect()))
(完)#
QPaintDevice: Cannot destroy paint device that is being painted
fish: “python QToolButton.py” terminated by signal SIGSEGV (Address boundary error)