最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Hover color appears when restoring window - Stack Overflow

programmeradmin5浏览0评论

I just started learning PyQt6 in Python. I have the following code: I click on the minimize button, the window disappears, if I click on the button in the taskbar it comes back but when it comes back, the minimize button has for a very short time the hover background color and not the initial one. Does anyone have any idea why or how I can solve this?

main.py file

import sys
from PyQt6.QtWidgets import (
    QApplication,
    QMainWindow,
    QWidget,
    QHBoxLayout,
    QVBoxLayout,
    QPushButton,
    QFrame,
    QLabel,
    QDialog
)
from PyQt6.QtGui import (
    QIcon,
    QCursor,
    QMouseEvent,
    QPixmap
)
from PyQt6.QtCore import (
    Qt,
    QPropertyAnimation,
    QEasingCurve,
    QEvent,
    QFile
)

class TitleBar(QWidget):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent
        self.setFixedHeight(35)
       
        TitleBar_Layout = QHBoxLayout(self)
        TitleBar_Layout.setContentsMargins(0, 0, 0, 0)
        TitleBar_Layout.setSpacing(0)

        icon_label = QLabel('')
        icon_label.setFixedSize(15, 15)

        title_label = QLabel('My Window')
        title_label.setFixedSize(200, 15)
        title_label.setObjectName('title_label')
        
        self.close_button = QPushButton('X')
        self.close_button.setFixedSize(35, 35)
        self.close_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
        self.close_button.setFocusPolicy(Qt.FocusPolicy.NoFocus)
        self.close_button.clicked.connect(self._close_Window)
        self.close_button.setObjectName('close_button')

        self.minimize_button = QPushButton('-')
        self.minimize_button.setFixedSize(35, 35)
        self.minimize_button.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
        self.minimize_button.setFocusPolicy(Qt.FocusPolicy.NoFocus)
        self.minimize_button.clicked.connect(self._minimize_window)
        self.minimize_button.setObjectName('min_button')
    
        TitleBar_Layout.addSpacing(10)
        TitleBar_Layout.addWidget(icon_label)
        TitleBar_Layout.addSpacing(5)
        TitleBar_Layout.addWidget(title_label)
        TitleBar_Layout.addStretch()
        TitleBar_Layout.addWidget(self.minimize_button)
        TitleBar_Layout.addWidget(self.close_button)

        self.start_position = None
    
    def mousePressEvent(self, event: QMouseEvent):
        if event.button() == Qt.MouseButton.LeftButton:
            self.start_position = event.globalPosition().toPoint() -     self.parent.frameGeometry().topLeft()
            event.accept()
    
    def mouseMoveEvent(self, event: QMouseEvent):
        if event.buttons() == Qt.MouseButton.LeftButton and self.start_position:
            self.parent.move(event.globalPosition().toPoint() - self.start_position)
            event.accept()
    
    def _close_Window(self):
        self.parent.close()
    
    def _minimize_window(self):
        self.parent.showMinimized()       

class ChildWindow(QDialog):
    def __init__(self, parent):
        super().__init__()
        self.setFixedSize(200, 200)
        self.setWindowIcon(QIcon('icons/sun.png'))
        self.setWindowTitle('Modal Window')
        
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(Qt.WindowType.FramelessWindowHint)
        self.setWindowTitle('My Window')
        self.setWindowIcon(QIcon('icons/sun.png'))
        self.setFixedSize(400, 500)
        
        self.centerWindow()

        WindowContainer = QWidget(self)
        self.setCentralWidget(WindowContainer)

        self.TitleBar = TitleBar(self)
        self.setMenuWidget(self.TitleBar)


        WindowGrid = QVBoxLayout()
        WindowGrid.setAlignment(Qt.AlignmentFlag.AlignCenter)

        frame = QFrame()
        frame.setFixedSize(250, 400)
        frame.setObjectName('frame')
        

        show_dialog = QPushButton('Show Dialog', frame)
        show_dialog.clicked.connect(self.showDialog)
        show_dialog.setObjectName('open_child_window')


        WindowGrid.addWidget(show_dialog)

        WindowContainer.setLayout(WindowGrid)
    
    def showDialog(self):
        self.dg = ChildWindow(self)
        self.dg.exec()

    def centerWindow(self):
        primary_screen = QApplication.primaryScreen()
        primary_screen_geometry = primary_screen.availableGeometry()

        center_x = (primary_screen_geometry.width() - self.width()) // 2
        center_y = (primary_screen_geometry.height() - self.height()) // 2

        self.move(center_x, center_y)

  

if __name__ == '__main__':
    app = QApplication([])
    window = Window()
    with open('style.qss', 'r') as f:
        style = f.read()
        # Set the stylesheet of the application
        app.setStyleSheet(style)
    window.show()
    sys.exit(app.exec())

style.qss file

* {
    background-color: #222222;
}

#close_button {
    border: 0px;
    background-color: #222222;;
    margin: 0px;
    padding: 0px;
}
            
#close_button:hover {
    background-color: #ff0000;                            
}

#min_button {
    border: 0px;
    background-color: #222222;
    margin: 0px;
    padding: 0px;
 }

#min_button:hover {
    background-color: #464646;
 }

#frame {
    background-color: #c0c0c0;
}

#open_child_window {
    background-color: #0066cc;
    color: #ffffff;
    border: 0px;
    width: 150;
     height: 30;
    border-radius: 5px;
    }

#open_child_window:hover {
    background-color: #1172d4;
}

#title_label {
    color: #bababa;
    font-size: 12px;
}

I also asked ChatGPT, but the solutions it gave had no effect. I hope I don't have any errors in the code during copy-paste. It works, but aesthetically I'm bothered by that hover color that appears when restoring the window.

发布评论

评论列表(0)

  1. 暂无评论