Search code examples
python-3.xlayoutpyqt4pyqt5

How can i convert this code to PyQt5?


I have this code i want to convert this code from PyQt4 to PyQt5

Here is the code

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()

How can i convert this code to pyqt5

Here is image show the result of the code


Solution

  • Try it:

    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()
    

    enter image description here