Issue
I need to convert this PySide code into PyQt5 and since most of the syntax is the same it is not a big problem. However running this code has certain issues. Can you please help me convert this pyside code into pyqt5. Thank You very much
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWidgets import *
class Animation_area(QWidget):
def __init__(self):
QWidget.__init__(self, None)
self.frame_no = 0
self.images = [
QImage("images/frame-" + str(i + 1) + ".png")
for i in range(20)
]
timer = QTimer(self)
timer.timeout.connect(self.update_value)
timer.start(50)
self.pause = False
def paintEvent(self, e):
p = QPainter()
p.begin(self)
p.drawImage(QRect(0, 0, 320, 320), self.images[self.frame_no])
p.end()
def update_value(self):
if not self.pause:
self.frame_no += 1
if self.frame_no >= 20:
self.frame_no = 0
QSound.play("sounds/rabbit_jump.wav")
self.update()
def PlayandPause(self):
if self.pause == True:
self.pause = False
else:
self.pause = True
class Simple_animation_window(QWidget):
def __init__(self):
QWidget.__init__(self, None)
self.anim_area = Animation_area()
layout = QVBoxLayout()
layout.addWidget(self.anim_area)
self.PlayandPauseButton = QPushButton("Pause")
self.PlayandPauseButton.clicked.connect(self.clicked)
layout.addWidget(self.PlayandPauseButton)
self.setLayout(layout)
self.setMinimumSize(330, 400)
def clicked(self):
self.anim_area.PlayandPause()
if self.sender().text() == "Play":
self.PlayandPauseButton.setText("Pause")
else:
self.PlayandPauseButton.setText("Play")
def main():
app = QApplication(sys.argv)
w = Simple_animation_window()
w.show()
return app.exec_()
if __name__ == "__main__":
sys.exit(main())
Solution
Since you did not post what were the problems, I only solved the problem I had that was when instantiating QSound
, Qt5 reassign packages to have a better organization so we found QSound
in QtMultimedia
.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
class Animation_area(QWidget):
def __init__(self):
QWidget.__init__(self, None)
self.frame_no = 0
self.images = [QImage("images/frame-" + str(i + 1) + ".png") for i in range(20)]
timer = QTimer(self)
timer.timeout.connect(self.update_value)
timer.start(50)
self.pause = False
def paintEvent(self, e):
p = QPainter(self)
p.drawImage(QRect(0, 0, 320, 320), self.images[self.frame_no])
def update_value(self):
if not self.pause:
self.frame_no += 1
if self.frame_no >= 20:
self.frame_no = 0
QSound.play("sounds/rabbit_jump.wav")
self.update()
def PlayandPause(self):
self.pause = not self.pause
class Simple_animation_window(QWidget):
def __init__(self):
QWidget.__init__(self, None)
self.anim_area = Animation_area()
layout = QVBoxLayout()
layout.addWidget(self.anim_area)
self.PlayandPauseButton = QPushButton("Pause")
self.PlayandPauseButton.clicked.connect(self.clicked)
layout.addWidget(self.PlayandPauseButton)
self.setLayout(layout)
self.setMinimumSize(330, 400)
def clicked(self):
self.anim_area.PlayandPause()
if self.sender().text() == "Play":
self.PlayandPauseButton.setText("Pause")
else:
self.PlayandPauseButton.setText("Play")
def main():
app = QApplication(sys.argv)
w = Simple_animation_window()
w.show()
return app.exec_()
if __name__ == "__main__":
sys.exit(main())
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.