Make a simple GUI application in 3 easy steps.
1. Design
Open Qt Creator
, create a new project and make your design. Save your result as .ui
file (here: mainwindow.ui
).
2. Generate corresponding .py file
Now you can create a .py file from your .ui file that you generated in the previous step. Enter the following into your command line:
$ pyuic4 mainwindow.ui -o GUI.py
If the above line is run successfully a GUI.py
file is created.
3. Python codes
You can add your own code (e.g. signals and slots) in the GUI.py
file but it's better to add them in a new file. If you ever want to make changes to your GUI, the GUI.py
file will be overwritten. That's why using another file to add functionality is better in most cases.
Let's call the new file main.py
.
from PyQt4 import QtGui
import sys
import GUI # Your generated .py file
class MyApp(QtGui.QMainWindow, GUI.Ui_MainWindow):
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
self.setupUi(self)
# Connect a button to a function
self.btn_run.clicked.connect(self.run)
def run(self):
# Write here what happens after the button press
print("run")
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
Now you can run main.py
and see your GUI.