Can't start designed pyQt application

送分小仙女□ 提交于 2019-12-13 16:38:49

问题


I have just designed my application inside pyQt Designer 5, generated my main.ui to main.py and my assets.qrc to assets_rc.py. No errors, when I now run main.py from my terminal nothing happends. Have I missed a step? Am I supposed to edit my main.py file now?

Cheers!

Python 3.3.0 pyQT 5


回答1:


This is for PyQt4, but should be the same for PyQt5.

Let's say your ui is called "mainwindow.ui". Compile this with pyuic4 into "mainWindowUi.py" (or whatever, just stick to the name).

Now create a file "mainWindow.py" with more or less this content:

from PyQt4 import QtGui
from mainWindowUi import Ui_MainWindow #same name as appears in mainWindowUi.py

class MainWindow (QtGui.QMainWindow): #Or wherever you are inheriting from
    def __init__ (self, parent = None):
        super (MainWindow, self).__init__ ()
        self.ui = Ui_MainWindow () #same name as appears in mainWindowUi.py
        self.ui.setupUi (self)

    #implement slots and signals and other funny things

Now create a file "program.py" with more or less this content:

#! /usr/bin/python3.3

import sys
from PyQt4 import QtGui
from mainWindow import MainWindow

def main():
    app = QtGui.QApplication (sys.argv)
    m = MainWindow ()
    m.show ()
    sys.exit (app.exec_ () )


if __name__ == '__main__':
    main ()

Run the program.py file. This is more or less the skeleton of a Qt application.



来源:https://stackoverflow.com/questions/20409016/cant-start-designed-pyqt-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!