Linking a qtDesigner .ui file to python/pyqt?

前端 未结 12 1388
北海茫月
北海茫月 2020-11-28 01:33

So if I go into QtDesigner and build a UI, it\'ll be saved as a .ui file. How can I make this as a python file or use this in python?

12条回答
  •  醉梦人生
    2020-11-28 02:11

    The cleaner way in my opinion is to first export to .py as aforementioned:

    pyuic4 foo.ui > foo.py
    

    And then use it inside your code (e.g main.py), like:

    from foo import Ui_MyWindow
    
    
    class MyWindow(QtGui.QDialog):
        def __init__(self):
            super(MyWindow, self).__init__()
    
            self.ui = Ui_MyWindow()
            self.ui.setupUi(self)
    
            # go on setting up your handlers like:
            # self.ui.okButton.clicked.connect(function_name)
            # etc...
    
    def main():
        app = QtGui.QApplication(sys.argv)
        w = MyWindow()
        w.show()
        sys.exit(app.exec_())
    
    if __name__ == "__main__":
        main()
    

    This way gives the ability to other people who don't use qt-designer to read the code, and also keeps your functionality code outside foo.py that could be overwritten by designer. You just reference ui through MyWindow class as seen above.

提交回复
热议问题