Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使一个圆圈每秒钟改变一次颜色?_Python_Pyqt5 - Fatal编程技术网

Python 如何使一个圆圈每秒钟改变一次颜色?

Python 如何使一个圆圈每秒钟改变一次颜色?,python,pyqt5,Python,Pyqt5,我最近制作了一个停车灯程序(gui),但我不知道是哪一个错误我不知道如何包括QTimer或任何延迟功能来改变颜色,我尝试了show功能,结果得到了两个程序我真的不知道如何修复我的代码,你能帮我吗 import PyQt5, sys, time,os from os import system,name from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QPoint,QTimerEvent,QTimer,Qt f

我最近制作了一个停车灯程序(gui),但我不知道是哪一个错误我不知道如何包括QTimer或任何延迟功能来改变颜色,我尝试了show功能,结果得到了两个程序我真的不知道如何修复我的代码,你能帮我吗

import PyQt5, sys, time,os
from os import system,name
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QPoint,QTimerEvent,QTimer,Qt
from PyQt5.QtWidgets import QWidget,QApplication,QMainWindow
from PyQt5.QtGui import QPainter
class Stoplight(QMainWindow):
    def __init__(self,parent = None):
        QWidget.__init__(self,parent)
        self.setWindowTitle("Stoplight")
        self.setGeometry(500,500,250,510)
    def paintEvent(self,event):
        radx = 50
        rady = 50
        center = QPoint(125,125)
        p = QPainter()
        p.begin(self)
        p.setBrush(Qt.white)
        p.drawRect(event.rect())
        p.end()

        p1 = QPainter()
        p1.begin(self)
        p1.setBrush(Qt.red)
        p1.setPen(Qt.black)
        p1.drawEllipse(center,radx,rady)
        p1.end()
class Stoplight1(Stoplight):
    def __init__(self,parent = None):
        QWidget.__init__(self,parent)
        self.setWindowTitle("Stoplight")
        self.setGeometry(500,500,250,510)
    def paintEvent(self,event):
        radx = 50
        rady = 50
        center = QPoint(125,125)
        p = QPainter()
        p.begin(self)
        p.setBrush(Qt.white)
        p.drawRect(event.rect())
        p.end()

        p1 = QPainter()
        p1.begin(self)
        p1.setBrush(Qt.green)
        p1.setPen(Qt.black)
        p1.drawEllipse(center,radx,rady)
        p1.end()
if __name__ == "__main__":
    application = QApplication(sys.argv)
    stoplight1 = Stoplight()
    stoplight2 = Stoplight1()
    time.sleep(1)
    stoplight1.show()
    time.sleep(1)
    stoplight2.show()
sys.exit(application.exec_())

这将是实现它的一个暗示。您需要定义一些函数或对象来处理循环的不同位置。此外,使用函数
self.update()
调用
paintEvent
application.processEvents()
使更改在gui中可见。您可以在执行部分使用代码,并从中生成更复杂的函数

编辑:使用字典尝试这种方法,因为它包含的代码较少

import PyQt5, sys, time,os
from os import system,name
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QPoint,QTimerEvent,QTimer,Qt
from PyQt5.QtWidgets import QWidget,QApplication,QMainWindow
from PyQt5.QtGui import QPainter
class Stoplight(QMainWindow):
    def __init__(self,parent = None):
        QWidget.__init__(self,parent)
        self.setWindowTitle("Stoplight")
        self.setGeometry(500,500,250,510)
        self.radx = 50
        self.rady = 50
        self.traffic_light_dic = {"red":{"QColor":Qt.red,
                                         "center":QPoint(125,125)},
                                  "yellow": {"QColor": Qt.yellow,
                                          "center": QPoint(125, 250)},
                                  "green": {"QColor": Qt.green,
                                          "center": QPoint(125, 375)},
                                  }
    def switch_to_color(self, color):
        self.center = self.traffic_light_dic[color]["center"]
        self.color = self.traffic_light_dic[color]["QColor"]
        self.update()
    def paintEvent(self, event):
        p = QPainter()
        p.begin(self)
        p.setBrush(self.color)
        p.setPen(Qt.black)
        p.drawEllipse(self.center, self.radx,self.rady)
        p.end()

if __name__ == "__main__":
    application = QApplication(sys.argv)
    stoplight1 = Stoplight()
    stoplight1.show()
    time.sleep(2)
    stoplight1.switch_to_color("red")
    application.processEvents()
    time.sleep(2)
    stoplight1.switch_to_color("yellow")
    application.processEvents()
    time.sleep(2)
    stoplight1.switch_to_color("green")
    application.processEvents()
    sys.exit(application.exec_())
虽然works的响应显然是不正确的,因为您不应该在GUI线程中使用
time.sleep()
,因为它会冻结应用程序,例如,在运行
time.sleep()
时尝试调整窗口大小

相反,您应该使用
QTimer
,正如您所说,该计时器必须连接到更改颜色的函数,并调用间接调用
paintEvent()
事件的
update()
方法。由于希望颜色循环更改颜色,因此必须创建循环迭代器

from itertools import cycle
from PyQt5 import QtCore, QtGui, QtWidgets

class TrafficLight(QtWidgets.QMainWindow):
    def __init__(self,parent = None):
        super(TrafficLight, self).__init__(parent)
        self.setWindowTitle("TrafficLight ")
        self.traffic_light_colors = cycle([
            QtGui.QColor('red'),
            QtGui.QColor('yellow'),
            QtGui.QColor('green')
        ])
        self._current_color = next(self.traffic_light_colors)
        timer = QtCore.QTimer(
            self, 
            interval=2000, 
            timeout=self.change_color
        )
        timer.start()
        self.resize(200, 400)

    @QtCore.pyqtSlot()
    def change_color(self):
        self._current_color = next(self.traffic_light_colors)
        self.update()

    def paintEvent(self, event):
        p = QtGui.QPainter(self)
        p.setBrush(self._current_color)
        p.setPen(QtCore.Qt.black)
        p.drawEllipse(self.rect().center(), 50, 50)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = TrafficLight()
    w.show() 
    sys.exit(app.exec_())

为什么要出现两个窗口?目前,在一个固定的延迟后,您只需打开两个stoplight GUI。你想在一个gui窗口中更改颜色吗?我只需要在一个gui窗口中更改颜色,从红色开始,绿色和黄色代表3圈我只是试了一下顺便说一句,我不知道如何重置颜色,所以我尝试了显示功能对不起对不起,先生,我如何在颜色中设置不同的间隔,例如红色10秒,黄色5秒,绿色10秒?@JansenLloydMacabangun如果间隔不同,逻辑会变得更复杂,先生,我想你不需要更新,我几分钟前就知道了,谢谢你,先生。我使用另一个变量来处理不同的时间延迟,我启动x=0,然后使用if-else语句,我可以更改时间延迟的值。非常感谢,先生,我的gui现在没有落后,顺便问一下,sir@QtCore.pyqtSlot()这个函数有什么用?@JansenLloydMacabangun read