Skip to content

Instantly share code, notes, and snippets.

@fredeerock
Created November 25, 2024 16:18
Show Gist options
  • Select an option

  • Save fredeerock/2988189eaf7b3d0d84c2bdf4619e8330 to your computer and use it in GitHub Desktop.

Select an option

Save fredeerock/2988189eaf7b3d0d84c2bdf4619e8330 to your computer and use it in GitHub Desktop.
A PyQT6 example of a WinUI looking accent button
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())
@fredeerock
Copy link
Copy Markdown
Author

This can be run with python main.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment