我尝试创建一个圆形按钮,但实际上 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_())