【问题标题】:Make a rectangle blink in PyQt在 PyQt 中使矩形闪烁
【发布时间】:2017-02-27 18:55:37
【问题描述】:

这段代码的目的是让一个正方形出现在画布上一段时间,然后被擦除。我知道所有的绘画事件都必须由重载函数paintEvent来处理。

但是,首先,没有绘制方块,我相信应该绘制和擦除方块的时间也没有得到尊重。我的猜测是由于事件出现的频率而发生这种情况。

我已经尝试在 drawApple 和 eraseApple 函数下调用 QPaintEvent。我错过了什么?

import sys, random
import numpy as np
import math
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QTimer
from PyQt4.QtCore import QRect
from PyQt4.QtGui import QPaintEvent

class Game(QtGui.QMainWindow):

    def __init__(self):
        super(Game, self).__init__()
        self.initUI()

    def initUI(self):
        palette = QtGui.QPalette()
        palette.setColor(QtGui.QPalette.Background, QtCore.Qt.white)
        self.setPalette(palette)
        self.tboard = Board(self)
        self.setCentralWidget(self.tboard)
        self.resize(400, 400)
        self.center()
        self.setWindowTitle('Game')
        self.show()
    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)


class Board(QtGui.QFrame):
    BoardWidth = 400
    BoardHeight = 400
    SquareWidth = 15
    SquareHeight = 15
    Speed = 10000

    def __init__(self, parent):
        super(Board, self).__init__(parent)
        #self.setAutoFillBackground(True)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.timer_draw = QtCore.QTimer()
        self.timer_draw.timeout.connect(self.drawApple)
        self.timer_draw.start(self.Speed)
        self.timer_draw.setInterval(self.Speed)
        self.timer_erase = QtCore.QTimer()
        self.timer_erase.timeout.connect(self.eraseApple)
        self.timer_erase.start(self.Speed + self.Speed/2)
        self.timer_erase.setInterval(self.Speed)
        self.apple_color = QtCore.Qt.red
        self.bkg_color = QtCore.Qt.white
        self.draw_apple = False
        self.x_apple = 0
        self.y_apple = 0
        self.rect = QRect(self.x_apple, self.y_apple, self.SquareWidth, self.SquareHeight)

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        print "Paint Event?"
        if self.draw_apple == True:
            print "Draw"
            self.apple_color = QtCore.Qt.red
        else:
            print "Do not draw"
            self.apple_color = self.bkg_color
        painter.setPen(self.apple_color)
        painter.drawRect(self.rect)

    def drawApple(self):
        print "Enters drawApple"
        self.x_apple = np.random.randint(0, math.floor(self.BoardWidth/self.SquareWidth)) * self.SquareWidth
        self.y_apple = np.random.randint(0, math.floor(self.BoardHeight/self.SquareHeight)) * self.SquareHeight
        self.draw_apple == True


    def eraseApple(self):
        print "Enters eraseApple"
        self.draw_apple == True

def main():

   app = QtGui.QApplication([])
   game = Game()    
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

【问题讨论】:

  • 建议您在 python2.7 旁边添加一个 python 标签

标签: python python-2.7 pyqt pyqt4


【解决方案1】:

您应该像调用paintEvent() 一样调用update() 函数。我还建议为该任务使用单个计时器。您只需拒绝变量 draw_apple 即可更改状态。

import sys, random
import numpy as np
import math
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QTimer, QRect
from PyQt4.QtGui import QPaintEvent

class Game(QtGui.QMainWindow):

    def __init__(self):
        super(Game, self).__init__()
        self.initUI()

    def initUI(self):
        palette = QtGui.QPalette()
        palette.setColor(QtGui.QPalette.Background, QtCore.Qt.white)
        self.setPalette(palette)
        self.tboard = Board(self)
        self.setCentralWidget(self.tboard)
        self.resize(400, 400)
        self.center()
        self.setWindowTitle('Game')
        self.show()

    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)


class Board(QtGui.QFrame):
    BoardWidth = 400
    BoardHeight = 400
    SquareWidth = 15
    SquareHeight = 15
    Speed = 10000

    def __init__(self, parent):
        super(Board, self).__init__(parent)
        #self.setAutoFillBackground(True)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.timer_draw = QtCore.QTimer(self)
        self.timer_draw.timeout.connect(self.drawApple)
        self.timer_draw.start(self.Speed)

        self.apple_color = QtCore.Qt.red
        self.draw_apple = False
        self.x_apple = 0
        self.y_apple = 0
        self.drawApple()

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        print "Paint Event?"
        if self.draw_apple == True:
            print "Draw"
            self.apple_color = QtCore.Qt.red
        else:
            print "Do not draw"
            self.apple_color = QtCore.Qt.white
        painter.setPen(self.apple_color)
        painter.drawRect(self.rect)

    def drawApple(self):
        self.draw_apple = not self.draw_apple

        self.x_apple = np.random.randint(0, math.floor(self.BoardWidth/self.SquareWidth)) * self.SquareWidth
        self.y_apple = np.random.randint(0, math.floor(self.BoardHeight/self.SquareHeight)) * self.SquareHeight

        self.rect = QRect(self.x_apple, self.y_apple, self.SquareWidth, self.SquareHeight)
        self.update()

def main():

   app = QtGui.QApplication([])
   game = Game()    
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2011-02-06
    • 2013-01-09
    相关资源
    最近更新 更多