Created
November 25, 2024 16:18
-
-
Save fredeerock/2988189eaf7b3d0d84c2bdf4619e8330 to your computer and use it in GitHub Desktop.
A PyQT6 example of a WinUI looking accent button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt6.QtWidgets import QApplication, QPushButton, QMainWindow | |
from PyQt6.QtCore import QEvent | |
class WinUIButton(QPushButton): | |
def __init__(self, text, parent=None): | |
super().__init__(text, parent) | |
self.setMouseTracking(True) | |
self.installEventFilter(self) | |
self.setStyleSheet(""" | |
QPushButton { | |
padding: 7px 12px; | |
border-radius: 6px; | |
background-color: #0067C0; | |
color: white; | |
font-size: 11pt; | |
} | |
QPushButton:hover { | |
background-color: #1975C5; | |
} | |
QPushButton:pressed { | |
background-color: #3183CA; | |
color: #D1E8FF; | |
} | |
""") | |
self.adjustSize() | |
def eventFilter(self, obj, event): | |
if event.type() in {QEvent.Type.Enter, QEvent.Type.Leave, QEvent.Type.MouseButtonPress, QEvent.Type.MouseButtonRelease}: | |
self.update() | |
return super().eventFilter(obj, event) | |
class MainWindow(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
self.setWindowTitle("WinUI Styled Button") | |
self.setGeometry(100, 100, 400, 300) | |
button = WinUIButton("Bing Bang Boom", self) | |
button.adjustSize() | |
button.move((self.width() - button.width()) // 2, (self.height() - button.height()) // 2) | |
if __name__ == "__main__": | |
app = QApplication(sys.argv) | |
window = MainWindow() | |
window.show() | |
sys.exit(app.exec()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This can be run with
python main.py