How to Use a form made in qt Designer in another form

时间:2018-03-29 13:58:21

标签: python python-3.x pyqt qt-designer

I created a Form with Qt Designer and convert it to python_file1.py using Pyuc5. The code in python_file1.py begins with the following statements:

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Frame(object):
   def setupUi(self, Frame):
       Frame.setObjectName("Frame")
...

When i use this form in a main window form as a widget by this statements:

from python_file import Ui_Frame as page_frame
...
class AppWindow(QMainWindow):
def __init__(self):
   self.my_page =page_frame()
   ...

It take this error:

Frame.setObjectName("Frame")
AttributeError: 'Ui_Frame' object has no attribute 'setObjectName'

If i edit python_file1.py to this:

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Frame(QtWidgets.QFrame):
   def setupUi(self, Frame):
       Frame.setObjectName("Frame")
...

All is right. python_file1.py generated automatically and all changes made in this file will be lost by PyQt5 UI code generator! How to correct this error without editing python_file1.py.

1 个答案:

答案 0 :(得分:0)

非常感谢eyllanesc。您的回答稍有变化。我按照http://pyqt.sourceforge.net/Docs/PyQt5/designer.html

中的说明进行更改
from PyQt5.QtWidgets import QFrame, QMainWindow
from python_file import Ui_Frame
class Page_Frame(QFrame, Ui_Frame):
   def __init__(self, *args, **kwargs):
     super(Page_Frame, self).__init__()
     # QFrame.__init__(self, *args, **kwargs)
     self.setupUi(self)

class AppWindow(QMainWindow):
def __init__(self):
   self.my_page = Page_Frame()
   ...
相关问题