raiseTemmie.py
import random
import sys
import time
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QWidget, QLabel, QApplication
class Example(QWidget):
size=100
imgNum=0
imgQuantity=2
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setStyleSheet("background-color:transparent;")
self.setGeometry(100, 100, 100, 100)
self.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint)
self.label=QLabel(self)
self.pixmaps=[QPixmap('left.png'),QPixmap('right.png'),QPixmap('stand.png')]
for x in range(len(self.pixmaps)):
self.pixmaps[x]=self.pixmaps[x].scaled(self.size,self.size,Qt.KeepAspectRatio)
self.label.setPixmap(self.pixmaps[2])
self.resize(self.pixmaps[2].width(),self.pixmaps[2].height())
self.show()
def moving(self):
distance=random.randint(10,40)
direct =[random.randint(-1,2),random.randint(-1,2)]
for x in range(0,distance):
self.changeFoot()
self.move(self.x()+5*direct[0],self.y()+5*direct[1])
time.sleep(0.05)
# self.changeTimer.stop()
def changeFoot(self):
if self.imgNum<self.imgQuantity-1:
self.imgNum+=1
else :
self.imgNum=0
self.label.setPixmap(self.pixmaps[self.imgNum])
def mousePressEvent(self, QMouseEvent):
self.label.setPixmap(self.pixmaps[2])
self.changeTimer.stop()
changeTimer=QTimer()
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
sys.exit()
if QKeyEvent.key() == Qt.Key_G:
self.moving()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
I thought this code would switch the pixmap and move it simultaneously but it do not work well. The timer start after moving finished. What is the problem?
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
sys.exit()
if QKeyEvent.key() == Qt.Key_G:
self.moving()
if I press 'G', it starts the changeTimer and calls moving()