如何将此代码转换为 PyQt5?

Posted

技术标签:

【中文标题】如何将此代码转换为 PyQt5?【英文标题】:How can i convert this code to PyQt5? 【发布时间】:2018-03-31 23:17:38 【问题描述】:

我有这段代码我想把这段代码从 PyQt4 转换为 PyQt5

这里是代码

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):
 def __init__(self):
    super(Example, self).__init__()
    self.initUI()

 def initUI(self):     
    hbox = QtGui.QHBoxLayout(self)
    left = QtGui.QFrame(self)       
    left.setFrameShape(QtGui.QFrame.StyledPanel)
    right = QtGui.QFrame(self)
    right.setFrameShape(QtGui.QFrame.StyledPanel)
    splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
    splitter.addWidget(left)
    splitter.addWidget(right)
    splitter.setStretchFactor(1, 1)
    splitter.setSizes([125, 150])
    hbox.addWidget(splitter)
    self.setLayout(hbox)
    QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
    self.setGeometry(300, 300, 300, 200)
    self.setWindowTitle('QtGui.QSplitter')
    self.show()

def main():
  app = QtGui.QApplication(sys.argv)
  ex = Example()
  sys.exit(app.exec_())

if __name__ == '__main__':
  main()

如何将此代码转换为 pyqt5

Here is image show the result of the code

【问题讨论】:

from PyQt4 import QtGui, QtCore 更改为from PyQt5 import QtWidgets, QtGui, QtCoreQtGui.Q... 更改为QtWidgets.Q... 【参考方案1】:

试试看:

import sys
#from PyQt4 import QtGui, QtCore
from PyQt5 import Qt

class Example(Qt.QWidget):
 def __init__(self):
    super(Example, self).__init__()

    self.initUI()

 def initUI(self):     
    hbox = Qt.QHBoxLayout(self)
    left = Qt.QFrame(self)       
    left.setFrameShape(Qt.QFrame.StyledPanel)
    left.setStyleSheet('background-color:lightblue; color: white; font-weight: bold;')
    labelLeft = Qt.QLabel(" \n left = Qt.QFrame(self)", left)

    right = Qt.QFrame(self)
    right.setFrameShape(Qt.QFrame.StyledPanel)
    labelRight = Qt.QLabel(" \n right = Qt.QFrame(self)", right)

    splitter = Qt.QSplitter(Qt.Qt.Horizontal)
    splitter.addWidget(left)
    splitter.addWidget(right)
    splitter.setStretchFactor(1, 1)
    splitter.setSizes([125, 150])
    hbox.addWidget(splitter)
    self.setLayout(hbox)

    Qt.QApplication.setStyle(Qt.QStyleFactory.create('Cleanlooks'))
    self.setGeometry(300, 300, 300, 200)
    self.setWindowTitle('Qt.QSplitter')
    self.show()

def main():
  app = Qt.QApplication(sys.argv)
  ex = Example()
  sys.exit(app.exec_())

if __name__ == '__main__':
  main()

【讨论】:

以上是关于如何将此代码转换为 PyQt5?的主要内容,如果未能解决你的问题,请参考以下文章

如何将此 NSClassFromString 代码转换为 Swift?

如何将此.XAML代码转换为C#代码

如何将此项目转换为代码?

如何将此代码转换为 for 循环格式?

PyQt5 视频播放器:转换为面向对象的代码会阻止播放

如何将此代码从 Dijkstra 转换为 Astar?