关于python:PyQt4到PyQt5怎么样?

PyQt4 to PyQt5 how?

我用pyqt4创建的代码,我想将其转换为pyqt5。我尝试了一些脚本来转换,但是除了名称没有任何改变。我需要手动更改什么才能使代码与pyqt5一起工作?第一部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
from pymaxwell import *
from numpy import *
from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtGui import QMainWindow, QApplication
from PyQt4.QtCore import *
from PyQt4.phonon import Phonon
from ffmpy import FFmpeg
import os
import app_window_dark
import about

uifile = 'Ui/app_window_dark.ui'
aboutfile = 'Ui/about.ui'

Ui_MainWindow, QtBaseClass = uic.loadUiType(uifile)
Ui_Dialog= uic.loadUiType(uifile)

class About(QtGui.QMainWindow, about.Ui_Dialog):
    def __init__(self, parent=None):
        super(About, self).__init__()
        QtGui.QMainWindow.__init__(self, parent)
        Ui_Dialog.__init__(self)
        self.setWindowModality(QtCore.Qt.ApplicationModal)
        point = parent.rect().bottomRight()
        global_point = parent.mapToGlobal(point)
        self.move(global_point - QPoint(395, 265))
        self.setupUi(self)

class MyApp(QtGui.QMainWindow, app_window_dark.Ui_MainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.about_btn.clicked.connect(self.popup)

        #prev next
        self.btn_next.clicked.connect(self.renderSet)
        self.btn_prev.clicked.connect(self.renderSet)

还有这个

1
2
3
4
5
6
7
if __name__ =="__main__":
    app = QApplication(sys.argv)
    #style = QApplication.setStyle('plastique')
    window = MyApp()
    window.setFixedSize(750, 320)
    window.show()
    sys.exit(app.exec_())


从qt4到qt5以及从pyqt4到pyqt5的主要变化是对某些类的重新排列,从而使qt项目具有可扩展性并生成较小的可执行文件。

qtgui库分为两个子模块:qtgui和qtwidgets,第二个子模块只有qmainwindow、qpushbutton等,这是您必须做的更改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[...]
from PyQt5 import QtGui, QtCore, uic, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtCore import *
[...]

Ui_MainWindow, QtBaseClass = uic.loadUiType(uifile)
Ui_Dialog= uic.loadUiType(uifile)

class About(QtWidgets.QMainWindow, about.Ui_Dialog):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.setWindowModality(QtCore.Qt.ApplicationModal)
        point = parent.rect().bottomRight()
        global_point = parent.mapToGlobal(point)
        self.move(global_point - QPoint(395, 265))

class MyApp(QtWidgets.QMainWindow, app_window_dark.Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        self.about_btn.clicked.connect(self.popup)

        #prev next
        self.btn_next.clicked.connect(self.renderSet)
        self.btn_prev.clicked.connect(self.renderSet)

注:Pyqt5中不存在Phonon,必须使用qtmadium,可以在以下答案中找到准确的解决方案:Pyqt5中不存在Phonon类