-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f41fc34
commit 0bea62c
Showing
6 changed files
with
215 additions
and
76 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,153 @@ | ||
from PySide6.QtCore import Qt | ||
from PySide6.QtGui import QPixmap, QIcon | ||
from PySide6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout | ||
from tool import resource_path | ||
from PySide6.QtCore import QSize, Qt, QEvent | ||
from PySide6.QtGui import QPalette | ||
from PySide6.QtWidgets import ( | ||
QApplication, | ||
QHBoxLayout, | ||
QLabel, | ||
QMainWindow, | ||
QStyle, | ||
QToolButton, | ||
QVBoxLayout, | ||
QWidget, | ||
) | ||
|
||
|
||
class Example(QWidget): | ||
class CustomTitleBar(QWidget): | ||
def __init__(self, parent): | ||
super().__init__(parent) | ||
self.setAutoFillBackground(True) | ||
self.setBackgroundRole(QPalette.ColorRole.Highlight) | ||
self.initial_pos = None | ||
title_bar_layout = QHBoxLayout(self) | ||
title_bar_layout.setContentsMargins(1, 1, 1, 1) | ||
title_bar_layout.setSpacing(2) | ||
|
||
self.title = QLabel(f"{self.__class__.__name__}", self) | ||
self.title.setStyleSheet( | ||
"""font-weight: bold; | ||
border: 2px solid black; | ||
border-radius: 12px; | ||
margin: 2px; | ||
""" | ||
) | ||
self.title.setAlignment(Qt.AlignmentFlag.AlignCenter) | ||
if title := parent.windowTitle(): | ||
self.title.setText(title) | ||
title_bar_layout.addWidget(self.title) | ||
# Min button | ||
self.min_button = QToolButton(self) | ||
min_icon = self.style().standardIcon( | ||
QStyle.StandardPixmap.SP_TitleBarMinButton | ||
) | ||
self.min_button.setIcon(min_icon) | ||
self.min_button.clicked.connect(self.window().showMinimized) | ||
|
||
# Max button | ||
self.max_button = QToolButton(self) | ||
max_icon = self.style().standardIcon( | ||
QStyle.StandardPixmap.SP_TitleBarMaxButton | ||
) | ||
self.max_button.setIcon(max_icon) | ||
self.max_button.clicked.connect(self.window().showMaximized) | ||
|
||
# Close button | ||
self.close_button = QToolButton(self) | ||
close_icon = self.style().standardIcon( | ||
QStyle.StandardPixmap.SP_TitleBarCloseButton | ||
) | ||
self.close_button.setIcon(close_icon) | ||
self.close_button.clicked.connect(self.window().close) | ||
|
||
# Normal button | ||
self.normal_button = QToolButton(self) | ||
normal_icon = self.style().standardIcon( | ||
QStyle.StandardPixmap.SP_TitleBarNormalButton | ||
) | ||
self.normal_button.setIcon(normal_icon) | ||
self.normal_button.clicked.connect(self.window().showNormal) | ||
self.normal_button.setVisible(False) | ||
# Add buttons | ||
buttons = [ | ||
self.min_button, | ||
self.normal_button, | ||
self.max_button, | ||
self.close_button, | ||
] | ||
for button in buttons: | ||
button.setFocusPolicy(Qt.FocusPolicy.NoFocus) | ||
button.setFixedSize(QSize(28, 28)) | ||
button.setStyleSheet( | ||
"""QToolButton { border: 2px solid white; | ||
border-radius: 12px; | ||
} | ||
""" | ||
) | ||
title_bar_layout.addWidget(button) | ||
|
||
def window_state_changed(self, state): | ||
if state == Qt.WindowState.WindowMaximized: | ||
self.normal_button.setVisible(True) | ||
self.max_button.setVisible(False) | ||
else: | ||
self.normal_button.setVisible(False) | ||
self.max_button.setVisible(True) | ||
|
||
|
||
class MainWindow(QMainWindow): | ||
def __init__(self): | ||
super().__init__() | ||
self.setStyleSheet(""" | ||
QWidget { | ||
background: white; | ||
} | ||
""") | ||
self.label = None | ||
# 创建QLabel实例 | ||
self.label = QLabel(self) | ||
# icon = QIcon() | ||
# icon.addPixmap(QPixmap(resource_path("icon.png")), QIcon.Normal, QIcon.On) | ||
# self.setWindowIcon(icon) | ||
self.setWindowIcon(QIcon(resource_path("../icon.ico"))) | ||
# 加载图片 | ||
pixmap = QPixmap(resource_path(resource_path("../instructions/instruction.png"))) # 替换为你的图片路径 | ||
|
||
self.label.setPixmap(pixmap) | ||
self.setFixedSize(pixmap.size()) | ||
# self.setWindowIcon(QIcon()) | ||
# 创建布局并添加QLabel | ||
layout = QVBoxLayout() | ||
layout.addWidget(self.label) | ||
self.setLayout(layout) | ||
|
||
# 设置窗口标题和大小 | ||
self.setWindowTitle('Parameters') | ||
self.setWindowFlags(Qt.WindowSystemMenuHint | Qt.WindowCloseButtonHint | Qt.WindowStaysOnTopHint) | ||
self.show() | ||
|
||
if __name__ == '__main__': | ||
self.setWindowTitle("Custom Title Bar") | ||
self.resize(400, 200) | ||
self.setWindowFlags(Qt.WindowType.FramelessWindowHint) | ||
central_widget = QWidget() | ||
self.title_bar = CustomTitleBar(self) | ||
|
||
work_space_layout = QVBoxLayout() | ||
work_space_layout.setContentsMargins(11, 11, 11, 11) | ||
work_space_layout.addWidget(QLabel("Hello, World!", self)) | ||
|
||
centra_widget_layout = QVBoxLayout() | ||
centra_widget_layout.setContentsMargins(0, 0, 0, 0) | ||
centra_widget_layout.setAlignment(Qt.AlignmentFlag.AlignTop) | ||
centra_widget_layout.addWidget(self.title_bar) | ||
centra_widget_layout.addLayout(work_space_layout) | ||
|
||
central_widget.setLayout(centra_widget_layout) | ||
self.setCentralWidget(central_widget) | ||
|
||
def changeEvent(self, event): | ||
if event.type() == QEvent.Type.WindowStateChange: | ||
self.title_bar.window_state_changed(self.windowState()) | ||
super().changeEvent(event) | ||
event.accept() | ||
|
||
def window_state_changed(self, state): | ||
self.normal_button.setVisible(state == Qt.WindowState.WindowMaximized) | ||
self.max_button.setVisible(state != Qt.WindowState.WindowMaximized) | ||
|
||
def mousePressEvent(self, event): | ||
if event.button() == Qt.MouseButton.LeftButton: | ||
self.initial_pos = event.position().toPoint() | ||
super().mousePressEvent(event) | ||
event.accept() | ||
|
||
def mouseMoveEvent(self, event): | ||
if self.initial_pos is not None: | ||
delta = event.position().toPoint() - self.initial_pos | ||
self.window().move( | ||
self.window().x() + delta.x(), | ||
self.window().y() + delta.y(), | ||
) | ||
super().mouseMoveEvent(event) | ||
event.accept() | ||
|
||
def mouseReleaseEvent(self, event): | ||
self.initial_pos = None | ||
super().mouseReleaseEvent(event) | ||
event.accept() | ||
|
||
if __name__ == "__main__": | ||
app = QApplication([]) | ||
ex = Example() | ||
window = MainWindow() | ||
window.show() | ||
app.exec() |
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
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
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