Its been tough switching from the comfort zone offered by vb.net in GUI development for windows to GUI development for Linux. Here i try to make a simple ‘Helloworld’ GUI application using python language which i believe is useful for a beginner to start out. The softwares i used to setup this one is as follows:
1. Linux Mint 17 Qiana
2. PyCharm Community Edition
3. Qt 4 Designer
Language used: Python
GUI designing is done using Qt 4 Designer. Here a simple GUI is made by first creating a blank widget. Drag in a few controls like a push button and a TextEdit control from the toolbox of the designer. I also placed a label with the text field set as ‘Hello world program’. Here i don’t want to worry about layouts just now, so just place the controls anywhere on the form.
Now press CTRL+R to preview the GUI made and save the work done. Here after saving i got a file named ‘untitled.ui’. Take note of the folder path where this is saved since we might need it later.
Our GUI for the application is ready but as you can see nothing happens when you click the button. So we need to code the actions to be performed using python. Start a new project in PyCharm.
Now copy and paste the earlier gui file created using qt to the PyCharm project folder. Here the project name was ‘untitled1’ so i paste the ‘untitled.gui’ file to Home->PyCharmProjects->untitled1 folder.
Now open the terminal window from PyCharm using the alt+F12 shortcut in PyCharm and execute the following command. (This is very important)
pyuic4 untitled.ui -o test1gui.py
Now you will find a new file named test1gui.py added to your python project. This py file will contain the python code for the corresponding GUI you developed using Qt earlier.
Now add a new python file to the project using file->add->python file option. Here i named the python file ‘test1’. We will use this to write our python code to make our GUI work. First we need to import some stuff.
from PyQt4 import QtCore, QtGui
from test1gui import Ui_Form
Now we will need to declare the form class and initialize it and define the click function for the push button. Use the below code for that.
def __init__(self, parent=None):
self.ui = Ui_Form()
print (‘Hello world again’)
Finally we need to write the code to display the form by running the python code
if __name__ == “__main__”:
app = QtGui.QApplication(sys.argv)
myapp = MyForm()