Python 尝试简单的QPushButton背景色更改

Python 尝试简单的QPushButton背景色更改,python,user-interface,pyqt5,qpushbutton,Python,User Interface,Pyqt5,Qpushbutton,我需要帮助创建一个简单的“闪烁”效果改变背景颜色的Q按钮。我想如果我能足够快地在两种颜色之间改变背景色,我就能产生这种闪烁效果。但是,虽然我可以将背景颜色设置为一种颜色,但我不知道如何快速在两种颜色之间切换。我尝试使用循环,但我的输出GUI只保留一种颜色。我是这类东西的初学者,所以也许我错过了一个简单的解决方案 我有所有必要的软件包和一切,所以为了简单起见,我只包括了处理背景颜色的按钮部分,这就是我认为我的问题所在 self.powerup_button = QtWidgets.QPushBut

我需要帮助创建一个简单的“闪烁”效果改变背景颜色的Q按钮。我想如果我能足够快地在两种颜色之间改变背景色,我就能产生这种闪烁效果。但是,虽然我可以将背景颜色设置为一种颜色,但我不知道如何快速在两种颜色之间切换。我尝试使用循环,但我的输出GUI只保留一种颜色。我是这类东西的初学者,所以也许我错过了一个简单的解决方案

我有所有必要的软件包和一切,所以为了简单起见,我只包括了处理背景颜色的按钮部分,这就是我认为我的问题所在

self.powerup_button = QtWidgets.QPushButton(self.centralwidget)

count = 0

while count < 100:


   self.powerup_button.setStyleSheet("background-color: none")
   count = count + 1

   self.powerup_button.setStyleSheet("background-color: green")
   count = count + 1
self.powerup\u button=qtwidts.QPushButton(self.centralwidget)
计数=0
当计数小于100时:
self.powerup_按钮.设置样式表(“背景色:无”)
计数=计数+1
self.powerup_按钮设置样式表(“背景色:绿色”)
计数=计数+1
我以为while循环会使按钮在两种颜色之间切换,产生我想要的闪烁效果,但我错了。

试试:

import sys
from PyQt5 import QtWidgets, QtCore

class MyWindow(QtWidgets.QMainWindow): 
    def __init__(self):
        super().__init__()

        self.flag = True

        self.powerup_button = QtWidgets.QPushButton("Button")
        self.setCentralWidget(self.powerup_button)

        timer = QtCore.QTimer(self, interval=1000)
        timer.timeout.connect(self.update_background)
        timer.start()  

    def update_background(self):
        if self.flag:
            self.powerup_button.setStyleSheet("background-color: none;")
        else:
            self.powerup_button.setStyleSheet("background-color: green;")  
        self.flag = not self.flag            


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    app.exec_()       

更改颜色后,您是否尝试过使用self.powerup\u按钮.repaint()或self.powerup\u按钮.update()?谢谢!这很有效。我似乎无法将其附加到我的设计师生成的文件thoug,它没有错误,但不会显示闪烁,我不知道为什么。