counter
import sys
import ctypes
from PySide6.QtCore import Qt, QPoint
from PySide6.QtWidgets import (
QApplication,
QWidget,
QLabel,
QPushButton,
QVBoxLayout,
QHBoxLayout,
QFrame,
)
=========================================================
Windows Mica
=========================================================
def enable_mica(window):
"""Windows 11のMica背景を有効にする。"""
hwnd = int(window.winId())
DWMWA_SYSTEMBACKDROP_TYPE = 38
# 2 = Mica
backdrop_type = ctypes.c_int(2)
ctypes.windll.dwmapi.DwmSetWindowAttribute(
ctypes.c_void_p(hwnd),
DWMWA_SYSTEMBACKDROP_TYPE,
ctypes.byref(backdrop_type),
ctypes.sizeof(backdrop_type),
)=========================================================
Main Window
=========================================================
class CounterApp(QWidget):
def __init__(self):
super().__init__()
self.count = 0
self.drag_position = QPoint()
self.setWindowTitle("Counter")
self.resize(900, 600)
self.setMinimumSize(750, 500)
# フレームレスウィンドウ
self.setWindowFlags(
Qt.FramelessWindowHint |
Qt.Window
)
# 背景を透明にする
self.setAttribute(
Qt.WA_TranslucentBackground
)
self.setup_ui()
# =====================================================
# UI
# =====================================================
def setup_ui(self):
# -------------------------------------------------
# Root
# -------------------------------------------------
root = QFrame()
root.setObjectName("root")
root_layout = QVBoxLayout(root)
root_layout.setContentsMargins(0, 0, 0, 0)
root_layout.setSpacing(0)
# -------------------------------------------------
# Title bar
# -------------------------------------------------
self.title_bar = QFrame()
self.title_bar.setObjectName("titleBar")
title_layout = QHBoxLayout(
self.title_bar
)
title_layout.setContentsMargins(
18, 0, 8, 0
)
title_layout.setSpacing(0)
title = QLabel("My App")
title.setObjectName("windowTitle")
title_layout.addWidget(title)
title_layout.addStretch()
# Window buttons
minimize = QPushButton("—")
maximize = QPushButton("□")
close = QPushButton("×")
minimize.setObjectName(
"windowButton"
)
maximize.setObjectName(
"windowButton"
)
close.setObjectName(
"closeButton"
)
minimize.clicked.connect(
self.showMinimized
)
maximize.clicked.connect(
self.toggle_maximize
)
close.clicked.connect(
self.close
)
for button in (
minimize,
maximize,
close,
):
button.setFixedSize(46, 32)
title_layout.addWidget(button)
root_layout.addWidget(
self.title_bar
)
# -------------------------------------------------
# Body
# -------------------------------------------------
body = QFrame()
body.setObjectName("body")
body_layout = QHBoxLayout(body)
body_layout.setContentsMargins(
0, 0, 0, 0
)
body_layout.setSpacing(0)
# -------------------------------------------------
# Navigation
# -------------------------------------------------
self.nav = QFrame()
self.nav.setObjectName(
"navigation"
)
nav_layout = QVBoxLayout(
self.nav
)
nav_layout.setContentsMargins(
12, 20, 12, 20
)
nav_layout.setSpacing(6)
app_name = QLabel("Counter")
app_name.setObjectName("appName")
nav_layout.addWidget(
app_name
)
nav_layout.addSpacing(20)
self.counter_nav = QPushButton(
"⌁ Counter"
)
self.about_nav = QPushButton(
"ⓘ About"
)
self.counter_nav.setObjectName(
"navButton"
)
self.about_nav.setObjectName(
"navButton"
)
self.counter_nav.clicked.connect(
self.show_counter
)
self.about_nav.clicked.connect(
self.show_about
)
nav_layout.addWidget(
self.counter_nav
)
nav_layout.addWidget(
self.about_nav
)
nav_layout.addStretch()
version = QLabel("v1.0.0")
version.setObjectName("version")
nav_layout.addWidget(
version
)
body_layout.addWidget(
self.nav
)
# -------------------------------------------------
# Content
# -------------------------------------------------
self.content = QFrame()
self.content.setObjectName(
"content"
)
self.content_layout = QVBoxLayout(
self.content
)
self.content_layout.setContentsMargins(
50, 40, 50, 40
)
self.content_layout.setSpacing(0)
body_layout.addWidget(
self.content
)
root_layout.addWidget(
body
)
# -------------------------------------------------
# Main layout
# -------------------------------------------------
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(
8, 8, 8, 8
)
main_layout.addWidget(
root
)
# 初期ページ
self.show_counter()
# =====================================================
# Counter page
# =====================================================
def show_counter(self):
self.clear_content()
self.set_selected(
self.counter_nav,
self.about_nav,
)
# Header
title = QLabel("Counter")
title.setObjectName(
"pageTitle"
)
subtitle = QLabel(
"A simple Fluent-style counter."
)
subtitle.setObjectName(
"pageSubtitle"
)
self.content_layout.addWidget(
title
)
self.content_layout.addWidget(
subtitle
)
self.content_layout.addSpacing(
30
)
# -------------------------------------------------
# Counter card
# -------------------------------------------------
card = QFrame()
card.setObjectName("card")
card_layout = QVBoxLayout(card)
card_layout.setContentsMargins(
40, 35, 40, 35
)
self.counter_label = QLabel()
self.counter_label.setObjectName(
"counter"
)
self.counter_label.setAlignment(
Qt.AlignCenter
)
card_layout.addWidget(
self.counter_label
)
self.content_layout.addWidget(
card
)
self.content_layout.addSpacing(
20
)
# -------------------------------------------------
# + / - buttons
# -------------------------------------------------
minus = QPushButton("−")
plus = QPushButton("+")
minus.setObjectName(
"secondaryButton"
)
plus.setObjectName(
"primaryButton"
)
minus.setFixedSize(
110, 60
)
plus.setFixedSize(
110, 60
)
minus.clicked.connect(
self.decrement
)
plus.clicked.connect(
self.increment
)
buttons = QHBoxLayout()
buttons.setSpacing(12)
buttons.addStretch()
buttons.addWidget(minus)
buttons.addWidget(plus)
buttons.addStretch()
self.content_layout.addLayout(
buttons
)
self.content_layout.addSpacing(
10
)
# -------------------------------------------------
# Reset
# -------------------------------------------------
reset = QPushButton("Reset")
reset.setObjectName(
"resetButton"
)
reset.setFixedHeight(42)
reset.clicked.connect(
self.reset
)
self.content_layout.addWidget(
reset
)
self.content_layout.addStretch()
self.update_display()
# =====================================================
# About page
# =====================================================
def show_about(self):
self.clear_content()
self.set_selected(
self.about_nav,
self.counter_nav,
)
# Header
title = QLabel("About")
title.setObjectName(
"pageTitle"
)
subtitle = QLabel(
"About this application."
)
subtitle.setObjectName(
"pageSubtitle"
)
self.content_layout.addWidget(
title
)
self.content_layout.addWidget(
subtitle
)
self.content_layout.addSpacing(
30
)
# -------------------------------------------------
# Application card
# -------------------------------------------------
app_card = QFrame()
app_card.setObjectName("card")
app_layout = QVBoxLayout(
app_card
)
app_layout.setContentsMargins(
28, 24, 28, 24
)
app_layout.setSpacing(12)
app_name = QLabel(
"Counter"
)
app_name.setObjectName(
"cardTitle"
)
description = QLabel(
"A small desktop application "
"built with Python and PySide6.\n\n"
"Designed with a Windows "
"Fluent-inspired interface."
)
description.setObjectName(
"info"
)
description.setWordWrap(
True
)
app_layout.addWidget(
app_name
)
app_layout.addWidget(
description
)
self.content_layout.addWidget(
app_card
)
self.content_layout.addSpacing(
16
)
# -------------------------------------------------
# Information card
# -------------------------------------------------
info_card = QFrame()
info_card.setObjectName("card")
info_layout = QVBoxLayout(
info_card
)
info_layout.setContentsMargins(
28, 24, 28, 24
)
info_layout.setSpacing(14)
version = QLabel(
"Version 1.0.0"
)
framework = QLabel(
"Framework PySide6"
)
platform = QLabel(
"Platform Windows"
)
for label in (
version,
framework,
platform,
):
label.setObjectName(
"info"
)
info_layout.addWidget(
label
)
self.content_layout.addWidget(
info_card
)
self.content_layout.addStretch()
# =====================================================
# Utility
# =====================================================
def clear_content(self):
while self.content_layout.count():
item = (
self.content_layout.takeAt(0)
)
widget = item.widget()
if widget:
widget.deleteLater()
def set_selected(
self,
selected,
unselected,
):
selected.setProperty(
"selected",
True
)
unselected.setProperty(
"selected",
False
)
for button in (
selected,
unselected,
):
button.style().unpolish(
button
)
button.style().polish(
button
)
button.update()
# =====================================================
# Counter logic
# =====================================================
def update_display(self):
self.counter_label.setText(
str(self.count)
)
def increment(self):
self.count += 1
self.update_display()
def decrement(self):
self.count -= 1
self.update_display()
def reset(self):
self.count = 0
self.update_display()
# =====================================================
# Window
# =====================================================
def toggle_maximize(self):
if self.isMaximized():
self.showNormal()
else:
self.showMaximized()
# -----------------------------------------------------
# Window dragging
# -----------------------------------------------------
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
if event.position().y() < 45:
self.drag_position = (
event.globalPosition().toPoint()
- self.frameGeometry().topLeft()
)
event.accept()
return
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
if self.drag_position:
self.move(
event.globalPosition().toPoint()
- self.drag_position
)
event.accept()
return
super().mouseMoveEvent(event)=========================================================
Style
=========================================================
STYLE = """
/* =======================================================
Root
======================================================= */
QFrame#root {
background-color:
rgba(245, 245, 245, 210);
border: 1px solid
rgba(255, 255, 255, 100);
border-radius: 12px;}
/* =======================================================
Title bar
======================================================= */
QFrame#titleBar {
background-color:
rgba(255, 255, 255, 35);
border-top-left-radius: 12px;
border-top-right-radius: 12px;}
QLabel#windowTitle {
font-size: 13px;
color: #404040;}
QPushButton#windowButton {
background-color: transparent;
border: none;
color: #444444;
font-size: 14px;}
QPushButton#windowButton:hover {
background-color:
rgba(0, 0, 0, 15);
border-radius: 4px;}
QPushButton#closeButton {
background-color: transparent;
border: none;
color: #444444;
font-size: 18px;}
QPushButton#closeButton:hover {
background-color: #C42B1C;
color: white;}
/* =======================================================
Navigation
======================================================= */
QFrame#navigation {
background-color:
rgba(255, 255, 255, 75);
border-right: 1px solid
rgba(0, 0, 0, 15);}
QLabel#appName {
font-size: 20px;
font-weight: 600;
padding-left: 12px;
color: #202020;}
QLabel#version {
font-size: 12px;
color: #777777;
padding-left: 12px;}
QPushButton#navButton {
text-align: left;
padding: 11px 14px;
border: none;
border-radius: 6px;
background-color: transparent;
color: #3B3B3B;
font-size: 14px;}
QPushButton#navButton:hover {
background-color:
rgba(0, 0, 0, 20);}
QPushButton#navButton[selected="true"] {
background-color:
rgba(0, 103, 192, 25);
color: #005FB8;
font-weight: 600;}
/* =======================================================
Content
======================================================= */
QFrame#content {
background-color:
rgba(255, 255, 255, 25);}
QLabel#pageTitle {
font-size: 32px;
font-weight: 600;
color: #202020;}
QLabel#pageSubtitle {
font-size: 14px;
color: #666666;}
/* =======================================================
Cards
======================================================= */
QFrame#card {
background-color:
rgba(255, 255, 255, 205);
border: 1px solid
rgba(0, 0, 0, 18);
border-radius: 12px;}
QLabel#cardTitle {
font-size: 20px;
font-weight: 600;
color: #202020;}
QLabel#counter {
background-color: transparent;
font-size: 96px;
font-weight: 600;
color: #202020;}
QLabel#info {
font-size: 15px;
color: #404040;}
/* =======================================================
Counter buttons
======================================================= */
QPushButton#primaryButton {
background-color: #0067C0;
color: white;
border: none;
border-radius: 8px;
font-size: 28px;
font-weight: 600;}
QPushButton#primaryButton:hover {
background-color: #0078D4;}
QPushButton#primaryButton:pressed {
background-color: #005A9E;}
QPushButton#secondaryButton {
background-color:
rgba(255, 255, 255, 220);
color: #202020;
border: 1px solid #D1D1D1;
border-radius: 8px;
font-size: 28px;}
QPushButton#secondaryButton:hover {
background-color:
rgba(255, 255, 255, 245);}
QPushButton#secondaryButton:pressed {
background-color: #E5E5E5;}
QPushButton#resetButton {
background-color: transparent;
color: #0067C0;
border: none;
border-radius: 6px;
font-size: 14px;}
QPushButton#resetButton:hover {
background-color:
rgba(0, 103, 192, 20);}
"""
=========================================================
Main
=========================================================
def main():
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
app.setStyleSheet(STYLE)
window = CounterApp()
window.show()
# Windows 11 Mica
enable_mica(window)
sys.exit(app.exec())if name == "main":
main()