### Install PyQt-Frameless-Window Source: https://pyqt-frameless-window.readthedocs.io/en/latest/quick-start.html Use these commands to install the library for your specific GUI framework. ```bash pip install PyQt5-Frameless-Window ``` ```bash pip install PyQt6-Frameless-Window ``` ```bash pip install PySide2-Frameless-Window ``` ```bash pip install PySideSix-Frameless-Window ``` -------------------------------- ### Setup UI for Frameless Window with Qt Designer Source: https://pyqt-frameless-window.readthedocs.io/en/latest/usage.html This code defines the UI structure for a PyQt window, intended for use with a frameless window implementation. It includes layout setup and widget definitions, with a specific margin to accommodate a title bar. ```python class Ui_Form(object): def setupUi(self, Form): Form.resize(400, 423) self.verticalLayout_2 = QVBoxLayout(Form) self.verticalLayout_2.setContentsMargins(-1, 32, -1, -1) self.frame_2 = QFrame(Form) self.verticalLayout = QVBoxLayout(self.frame_2) self.horizontalLayout = QHBoxLayout() self.pushButton = QPushButton(self.frame_2) self.horizontalLayout.addWidget(self.pushButton) self.lineEdit = QLineEdit(self.frame_2) self.horizontalLayout.addWidget(self.lineEdit) self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QHBoxLayout() self.pushButton_2 = QPushButton(self.frame_2) self.horizontalLayout_2.addWidget(self.pushButton_2) self.lineEdit_2 = QLineEdit(self.frame_2) self.horizontalLayout_2.addWidget(self.lineEdit_2) self.verticalLayout.addLayout(self.horizontalLayout_2) self.verticalLayout_2.addWidget(self.frame_2) self.frame = QFrame(Form) self.horizontalLayout_3 = QHBoxLayout(self.frame) self.tableView = QTableView(self.frame) self.horizontalLayout_3.addWidget(self.tableView) self.verticalLayout_2.addWidget(self.frame) self.retranslateUi(Form) def retranslateUi(self, Form): _translate = QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "选择文件")) self.pushButton_2.setText(_translate("Form", "创建路径")) ``` -------------------------------- ### Inherit FramelessWindow for Custom Native Event Handling Source: https://pyqt-frameless-window.readthedocs.io/en/latest/snap-layout.html Example of inheriting FramelessWindow to rewrite the nativeEvent method for custom Windows message handling. This is useful for customizing window behavior like snap layouts and title bar interactions. ```python import sys if sys.platform != "win32": from qframelesswindow import FramelessWindow else: from ctypes.wintypes import MSG import win32con from PyQt5.QtCore import QPoint, QEvent, Qt from PyQt5.QtGui import QCursor, QMouseEvent from PyQt5.QtWidgets import QApplication from qframelesswindow import FramelessWindow as Window from qframelesswindow.titlebar.title_bar_buttons import TitleBarButtonState class FramelessWindow(Window): """ Frameless window """ def nativeEvent(self, eventType, message): """ Handle the Windows message """ ``` -------------------------------- ### Implement a minimal frameless window Source: https://pyqt-frameless-window.readthedocs.io/en/latest/usage.html Inherit from FramelessWindow to create a basic frameless application window. ```python import sys from PyQt5.QtWidgets import QApplication from qframelesswindow import FramelessWindow class Window(FramelessWindow): def __init__(self, parent=None): super().__init__(parent=parent) self.setWindowTitle("PyQt-Frameless-Window") self.titleBar.raise_() if __name__ == '__main__': app = QApplication(sys.argv) demo = Window() demo.show() sys.exit(app.exec_()) ``` -------------------------------- ### Use StandardTitleBar with icon and title Source: https://pyqt-frameless-window.readthedocs.io/en/latest/usage.html Replace the default title bar with StandardTitleBar to include window icons and titles. ```python from qframelesswindow import FramelessWindow, StandardTitleBar class Window(FramelessWindow): def __init__(self, parent=None): super().__init__(parent=parent) # replace the default title bar with StandardTitleBar self.setTitleBar(StandardTitleBar(self)) self.setWindowIcon(QIcon("screenshot/logo.png")) self.setWindowTitle("PyQt-Frameless-Window") # don't forget to put the title bar at the top self.titleBar.raise_() ``` -------------------------------- ### Integrate UI with FramelessWindow Class Source: https://pyqt-frameless-window.readthedocs.io/en/latest/usage.html This Python code demonstrates how to inherit from both a generated UI class (Ui_Form) and a custom FramelessWindow class to create a functional frameless window. Ensure the FramelessWindow class is defined elsewhere. ```python class Window(FramelessWindow, Ui_Form): def __init__(self, parent=None): super().__init__(parent) self.setupUi(self) ``` -------------------------------- ### Create an Acrylic Window Source: https://pyqt-frameless-window.readthedocs.io/en/latest/window-effect.html Use the AcrylicWindow class to create a window with an acrylic blur effect. Ensure the title bar is raised if customizing effects. ```python from qframelesswindow import AcrylicWindow class Window(AcrylicWindow): def __init__(self, parent=None): super().__init__(parent=parent) self.setWindowTitle("Acrylic Window") self.titleBar.raise_() # customize acrylic effect # self.windowEffect.setAcrylicEffect(self.winId(), "106EBE99") # you can also enable mica effect on Win11 # self.windowEffect.setMicaEffect(self.winId(), False) ``` -------------------------------- ### Enable Snap Layout in Frameless Window Source: https://pyqt-frameless-window.readthedocs.io/en/latest/snap-layout.html Override the nativeEvent method to handle Windows messages for snap layout and custom title bar button interactions. This code should replace the existing nativeEvent in qframelesswindow/windows/__init__.py. ```python from ..titlebar.title_bar_buttons import TitleBarButtonState def nativeEvent(self, eventType, message): """ Handle the Windows message """ msg = MSG.from_address(message.__int__()) if not msg.hWnd: return super().nativeEvent(eventType, message) if msg.message == win32con.WM_NCHITTEST and self._isResizeEnabled: pos = QCursor.pos() xPos = pos.x() - self.x() yPos = pos.y() - self.y() w, h = self.width(), self.height() lx = xPos < self.BORDER_WIDTH rx = xPos > w - self.BORDER_WIDTH ty = yPos < self.BORDER_WIDTH by = yPos > h - self.BORDER_WIDTH if lx and ty: return True, win32con.HTTOPLEFT elif rx and by: return True, win32con.HTBOTTOMRIGHT elif rx and ty: return True, win32con.HTTOPRIGHT elif lx and by: return True, win32con.HTBOTTOMLEFT elif ty: return True, win32con.HTTOP elif by: return True, win32con.HTBOTTOM elif lx: return True, win32con.HTLEFT elif rx: return True, win32con.HTRIGHT #--------------------------------------- ADDED CODE --------------------------------------# elif self.titleBar.childAt(pos-self.geometry().topLeft()) is self.titleBar.maxBtn: self.titleBar.maxBtn.setState(TitleBarButtonState.HOVER) return True, win32con.HTMAXBUTTON elif msg.message in [0x2A2, win32con.WM_MOUSELEAVE]: self.titleBar.maxBtn.setState(TitleBarButtonState.NORMAL) elif msg.message in [win32con.WM_NCLBUTTONDOWN, win32con.WM_NCLBUTTONDBLCLK]: if self.titleBar.childAt(QCursor.pos()-self.geometry().topLeft()) is self.titleBar.maxBtn: QApplication.sendEvent(self.titleBar.maxBtn, QMouseEvent( QEvent.MouseButtonPress, QPoint(), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)) return True, 0 elif msg.message in [win32con.WM_NCLBUTTONUP, win32con.WM_NCRBUTTONUP]: if self.titleBar.childAt(QCursor.pos()-self.geometry().topLeft()) is self.titleBar.maxBtn: QApplication.sendEvent(self.titleBar.maxBtn, QMouseEvent( QEvent.MouseButtonRelease, QPoint(), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)) #------------------------------------------------------------------------------------------# elif msg.message == win32con.WM_NCCALCSIZE: if msg.wParam: rect = cast(msg.lParam, LPNCCALCSIZE_PARAMS).contents.rgrc[0] else: rect = cast(msg.lParam, LPRECT).contents isMax = win_utils.isMaximized(msg.hWnd) isFull = win_utils.isFullScreen(msg.hWnd) # adjust the size of client rect if isMax and not isFull: thickness = win_utils.getResizeBorderThickness(msg.hWnd) rect.top += thickness rect.left += thickness rect.right -= thickness rect.bottom -= thickness # handle the situation that an auto-hide taskbar is enabled if (isMax or isFull) and Taskbar.isAutoHide(): position = Taskbar.getPosition(msg.hWnd) if position == Taskbar.LEFT: rect.top += Taskbar.AUTO_HIDE_THICKNESS elif position == Taskbar.BOTTOM: rect.bottom -= Taskbar.AUTO_HIDE_THICKNESS elif position == Taskbar.LEFT: rect.left += Taskbar.AUTO_HIDE_THICKNESS elif position == Taskbar.RIGHT: rect.right -= Taskbar.AUTO_HIDE_THICKNESS result = 0 if not msg.wParam else win32con.WVR_REDRAW return True, result return super().nativeEvent(eventType, message) ``` -------------------------------- ### Customize the title bar appearance Source: https://pyqt-frameless-window.readthedocs.io/en/latest/usage.html Extend TitleBar to modify button colors or apply custom QSS styles to title bar components. ```python from qframelesswindow import FramelessWindow, TitleBar class CustomTitleBar(TitleBar): """ Custom title bar """ def __init__(self, parent): super().__init__(parent) # customize the style of title bar button self.minBtn.setHoverColor(Qt.white) self.minBtn.setHoverBackgroundColor(QColor(0, 100, 182)) self.minBtn.setPressedColor(Qt.white) self.minBtn.setPressedBackgroundColor(QColor(54, 57, 65)) # use qss to customize title bar button self.maxBtn.setStyleSheet(""" TitleBarButton { qproperty-normalColor: black; qproperty-normalBackgroundColor: transparent; qproperty-hoverColor: white; qproperty-hoverBackgroundColor: rgb(0, 100, 182); qproperty-pressedColor: white; qproperty-pressedBackgroundColor: rgb(54, 57, 65); } """) class Window(FramelessWindow): def __init__(self, parent=None): super().__init__(parent=parent) # change the default title self.setTitleBar(CustomTitleBar(self)) ``` -------------------------------- ### Toggle Acrylic Effect Source: https://pyqt-frameless-window.readthedocs.io/en/latest/window-effect.html This method enables or disables the acrylic effect and applies a background color or transparency. It also conditionally adds a shadow effect based on the operating system version. ```python def setAcrylicEffectEnabled(self, enable: bool): """ set acrylic effect enabled """ self.setStyleSheet(f"background:{'transparent' if enable else '#F2F2F2'}") if enable: self.windowEffect.setAcrylicEffect(self.winId(), "F2F2F299") if QOperatingSystemVersion.current() != QOperatingSystemVersion.Windows10: self.windowEffect.addShadowEffect(self.winId()) else: self.windowEffect.addShadowEffect(self.winId()) self.windowEffect.removeBackgroundEffect(self.winId()) ``` -------------------------------- ### Intercepting Native Windows Events Source: https://pyqt-frameless-window.readthedocs.io/en/latest/snap-layout.html Overrides nativeEvent to handle window hit testing and mouse interactions for custom title bar buttons. Requires win32con and PyQt5/6 event handling. ```python msg = MSG.from_address(message.__int__()) if not msg.hWnd: return super().nativeEvent(eventType, message) if msg.message == win32con.WM_NCHITTEST and self._isResizeEnabled: if self._isHoverMaxBtn(): self.titleBar.maxBtn.setState(TitleBarButtonState.HOVER) return True, win32con.HTMAXBUTTON elif msg.message in [0x2A2, win32con.WM_MOUSELEAVE]: self.titleBar.maxBtn.setState(TitleBarButtonState.NORMAL) elif msg.message in [win32con.WM_NCLBUTTONDOWN, win32con.WM_NCLBUTTONDBLCLK] and self._isHoverMaxBtn(): e = QMouseEvent(QEvent.MouseButtonPress, QPoint(), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) QApplication.sendEvent(self.titleBar.maxBtn, e) return True, 0 elif msg.message in [win32con.WM_NCLBUTTONUP, win32con.WM_NCRBUTTONUP] and self._isHoverMaxBtn(): e = QMouseEvent(QEvent.MouseButtonRelease, QPoint(), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) QApplication.sendEvent(self.titleBar.maxBtn, e) return super().nativeEvent(eventType, message) def _isHoverMaxBtn(self): pos = QCursor.pos() - self.geometry().topLeft() - self.titleBar.pos() return self.titleBar.childAt(pos) is self.titleBar.maxBtn ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.