编程语言
首页 > 编程语言> > Python在pyqt5中创建一个真实的圆圈按钮

Python在pyqt5中创建一个真实的圆圈按钮

作者:互联网

我尝试创建一个圆形按钮,但实际上pyqt仍然创建了一个方形按钮.找到的所有示例都创建了方形按钮并在其中放置了圆形图像,但是当我尝试隐藏按钮的背景时仍然失败,我也尝试添加一些悬停功能,但是应该可以用后缀,所以您可以跳过它.我的代码在这里:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QAction
from PyQt5.QtGui import QPalette
# from video import VideoWindow
import sys

class HoverButton(QtWidgets.QToolButton):

    def __init__(self, parent=None):
        super(HoverButton, self).__init__(parent)
        self.setMouseTracking(True)

    def enterEvent(self,event):
        # print("Enter")
        self.setStyleSheet('''   
                                 border-image: url("images/exit.jpg") 10 10 2 2;
                                 border-top: 10px transparent;
                                 border-bottom: 10px transparent;
                                 border-right: 2px transparent;
                                 border-left: 2px transparent''')
        self.setGeometry(QtCore.QRect(1100, 550, 160, 161))

    def leaveEvent(self,event):
        self.setStyleSheet('''  
                                 border-image: url("images/exit.jpg") 10 10 2 2;
                                 border-top: 10px transparent;
                                 border-bottom: 10px transparent;
                                 border-right: 2px transparent;
                                 border-left: 2px transparent''')
        self.setGeometry(QtCore.QRect(1100, 550, 140, 141))

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1371, 924)
        MainWindow.setAcceptDrops(True)
        self.button =  HoverButton(self)
        self.button.setGeometry(QtCore.QRect(1100, 550, 140, 141)) 
        self.button.setStyleSheet('''background: transparent;
                                 border-image: url("images/exit.jpg") 3 10 3 10;
                                 border-top: 3px transparent;
                                 border-bottom: 3px transparent;
                                 border-right: 10px transparent;
                                 border-left: 10px transparent;
                                 ''')
        self.button.setObjectName('button')
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

输出:
enter image description here

解决方法:

一种可能的解决方案是使用setMask(),此实现的优点是click事件将仅针对区域内的点:

class HoverButton(QtWidgets.QToolButton):
    def __init__(self, parent=None):
        super(HoverButton, self).__init__(parent)
        self.setStyleSheet('''border-image: url("imagen.jpg")''')

    def resizeEvent(self, event):
        self.setMask(QtGui.QRegion(self.rect(), QtGui.QRegion.Ellipse))
        QtWidgets.QToolButton.resizeEvent(self, event)

之前:

enter image description here

后:

enter image description here

在您的情况下,传递给QRegion的QRect()必须适合您图像的大小,因为圆形元素比您的图像小,一种更简单的方法是编辑图像,使圆完美地刻在图像中图像是.

标签:pyqt5,python,pyqt,qtoolbutton
来源: https://codeday.me/bug/20191009/1878194.html