我最近制作了一个交通信号灯程序(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_())
答案 0 :(得分:1)
尽管@f.wue的响应显然是不正确的,因为您不应该在GUI线程中使用time.sleep()
,因为它会冻结应用程序,例如,尝试在运行{{1 }}。
相反,应该使用time.sleep()
来表示,该计时器必须连接到更改颜色的函数并调用间接调用QTimer
事件的update()
方法。由于您希望颜色周期性地更改颜色,因此必须创建一个循环迭代器。
paintEvent()
答案 1 :(得分:0)
这将是完成它的提示。您需要定义一些函数或对象来处理循环的不同位置。此外,使用函数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_())