I would not use the result of pyuic4 in the same way. In fact I would not use pyuic4 at all, but let's see the possibilities:
Using pyuic4
Say you have your ui file my_window.ui
in which you create a QMainWindow
widget named MainWindow
. With Designer, you also added a link between the clicked
action of the push button and a custom slot about()
(do you know how to create a custom slot in designer ?). Now using the ui file can be done in 2 steps:
You generate the python equivalent file
ui_my_window.py
withpyuic4
:$ pyuic4 my_window.ui -o ui_my_window.py
Then you create another python file (main.py) which will import the file
ui_my_window.py
. Do not modify the file generated by pyuic4 ! It will be overwritten if you rerun thepyuic4
command !$ cat main.py from PyQt4 import QtGui # import the class created py pyuic4 from ui_my_window import Ui_MainWindow class MyMainWindow(QtGui.QMainWindow): def __init__(self, parent=None) QtGui.QMainWindow.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) # Beware that all widgets are now available through the self.ui attribute # Getting the push button is written: # self.ui.pushButton def about(self): # this is the custom slot created in Designer QtGui.QMessageBox.about(self, "Test", "This is a test.") if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) myMainWindow = MyMainWindow() myMainWindow.show() sys.exit(app.exec_())
So now you have 3 files:
$ ls
main.py ui_my_window.py my_window.ui
Just run python main.py
to launch the GUI.
Without using pyuic4
PyQt4 provides a very usefull module when working with ui files: the uic module. In fact pyuic4 is using this module to generate the python file of the ui file. Using the module allows you to avoid the pyuic4 step (Beware: if you use icons in a resource file (.qrc), you still need to use the pyrcc4
tool and import the resulting file in your code).
In your example, the code becomes very light:
$ cat main.py
import os
from PyQt4 import QtGui
# import the uic module
from PyQt4 import uic
class MyMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None)
QtGui.QMainWindow.__init__(self, parent)
# Load the ui file
uic.loadUi(os.path.join(os.path.dirname(os.path.abspath(__file__)),"my_window.ui"), self)
# Now in this case, all widgets can directly be accessed because the last argument
# of loadUi is self.
# Getting the push button is written:
# self.pushButton
def about(self):
# this is the custom slot created in Designer
QtGui.QMessageBox.about(self, "Test", "This is a test.")
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
myMainWindow = MyMainWindow()
myMainWindow.show()
sys.exit(app.exec_())
In this case you just have 2 files:
$ ls
main.py my_window.ui
Again just run python main.py
to launch the GUI.