我不知道我是否正确理解了您的问题,但这是一个使用自定义类的版本。可以根据需要进行修改。部分代码来自这里
import sys
from PyQt4 import QtCore, QtGui
class HexButton(QtGui.QPushButton):
size = 100
x = (3**0.5 / 2)
font = QtGui.QFont('Arial', size*0.08)
font.setWeight(500)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
hexaPoints = [QtCore.QPoint(size/4,0),
QtCore.QPoint(size/4 + size/2,0),
QtCore.QPoint(size,size*0.5*x),
QtCore.QPoint(size/4 + size/2,size*x),
QtCore.QPoint(size/4,size*x),
QtCore.QPoint(0,size*0.5*x)]
hexaPointsF = [QtCore.QPointF(size/4,0),
QtCore.QPointF(size/4 + size/2,0),
QtCore.QPointF(size,size*0.5*x),
QtCore.QPointF(size/4 + size/2,size*x),
QtCore.QPointF(size/4,size*x),
QtCore.QPointF(0,size*0.5*x)]
hexa = QtGui.QPolygon(hexaPoints)
hexaF = QtGui.QPolygonF(hexaPointsF)
def __init__(self, parent=None):
QtGui.QPushButton.__init__(self)
self.setMinimumSize(HexButton.size + 10, HexButton.size + 10)
self.text = None
self.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
self.color = QtGui.QColor(241, 186, 82)
self.setFlat(True)
def setText(self, text):
self.text = text
self.update()
def setColor(self, r,g,b):
self.color = QtGui.QColor(r,g,b)
self.update()
def paintEvent(self, event):
qp = QtGui.QPainter()
qp.begin(self)
opt = QtGui.QStyleOptionButton()
self.initStyleOption(opt)
self.style().drawControl(QtGui.QStyle.CE_PushButton, opt, qp, self)
qp.end()
painter = QtGui.QPainter()
painter.begin(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(self.color)
basePoly = QtGui.QPolygonF(HexButton.hexaF)
ss = 0.98
basePoly.translate(QtCore.QPointF(HexButton.size*(1.03-ss)*0.8, HexButton.size*(1.19-ss)*0.6*HexButton.x))
painter.drawPolygon(basePoly)
plist = HexButton.hexaPointsF + [HexButton.hexaPointsF[0]]
s = 0.95
painter.translate(QtCore.QPointF(HexButton.size*(1.03-s)*0.8, HexButton.size*(1.2-s)*0.6*HexButton.x))
painter.scale(s, s)
painter.setPen(QtGui.QPen(QtGui.QColor(0, 0, 0), (HexButton.size*0.016)*(1/s)))
painter.drawPolyline(*plist)
painter.resetTransform()
if self.text:
pen_text = QtGui.QPen()
pen_text.setBrush(QtGui.QColor(0,0,0))
painter.setPen(pen_text)
painter.setFont(HexButton.font)
painter.drawText(0, 0, HexButton.size+10, HexButton.size*HexButton.x+20, QtCore.Qt.AlignCenter, self.text)
painter.end()
class MainDialog(QtGui.QMainWindow):
def __init__(self):
super(self.__class__, self).__init__()
centralwidget = QtGui.QWidget(self)
self.layout = QtGui.QHBoxLayout(centralwidget)
self.button = HexButton()
self.button.setText("Foooooo")
self.anotherButton = HexButton()
self.anotherButton.setText("Barrr")
self.anotherButton.setColor(255, 102, 102)
self.layout.addWidget(self.button)
self.layout.addWidget(self.anotherButton)
self.setCentralWidget(centralwidget)
def main():
app = QtGui.QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()
if __name__ == '__main__':
main()