Rapid GUI Development with PyQt5 and Eric6
Configuring Eric6
Launch Eric6 and navigate to Settings -> Preferences.
In the left panel, select Project -> Multiple Projects. Click the icon on the right and choose a directory on your hard drive (e.g., F:\pyqt). Click OK.
Practical Example
Creating a Project
In Eric6, select Project -> New.
Click the icon next to the Project Folder field. Manually create a new directory named cw01 within F:\pyqt\ and select it. Click OK.
A prompt will appear asking if you want to add existing files to the project. Select Yes.
In the subsequent file type association dialog, click OK.
The main window's title bar will now display cw01 – eric6. In the Project Browser under the Source Code tab, an empty __init__.py file is automatically generated.
Creating a Form
Switch to the Forms tab in the Project Browser. Right-click in the blank area and select New Form.
In the New Form dialog, keep the default Dialog type and click OK.
In the save dialog, the path is already set to you're project folder. Enter a filename (e.g., login_form.ui) and click Save.
The login_form.ui file will now appear under the Forms tab.
Designing the Form
Upon saving, Qt Designer opens automatically with login_form.ui loaded.
From the Widget Box on the left, drag and drop the following onto the form: one Push Button, two Labels, and two Line Edits.
Use the Object Inspector on the right to select controls. Modify their objectName properties in the Property Editor:
- Left Push Button:
btn_login - Right Push Button:
btn_cancel
Save the form in Qt Designer and close the application.
Compiling and Testing
Back in Eric6, right-click the login_form.ui file and select Compile Form.
A success message will appear shortly.
Return to the Source Code tab. A new file named Ui_login_form.py will be present. Double-click to view the generated code.
Select Start -> Run Script (or press F2). In the Run Script dialog, click OK.
The designed form window will appear.
Enhancing the Application
We can now add functionality to the Cancel button.
In the Project Browser, right-click the Ui_login_form.py file and select Generate Dialog Code.
In the Dialog Code Generator window, click New.
Expand btn_cancel (QPushButton) and check the box for on_btn_cancel_clicked(). Click OK.
A new file, login_form.py, will appear in the Source Code tab.
Double-click login_form.py to view its contents. Running the script (F2) at this point may cause an error. Modify the import statement:
Change:
from .Ui_login_form import Ui_Dialog
To:
from Ui_login_form import Ui_Dialog
Save the file. Running the script may still cause it to close immediately. Add the following code to the file's main block and udpate the slot functon:
# -*- coding: utf-8 -*-
"""
Module implementing the main dialog.
"""
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog, QApplication
from Ui_login_form import Ui_Dialog
class MainDialog(QDialog, Ui_Dialog):
"""
Main dialog class.
"""
def __init__(self, parent=None):
"""
Constructor
"""
super(MainDialog, self).__init__(parent)
self.setupUi(self)
@pyqtSlot()
def on_btn_login_clicked(self):
"""
Slot for login button click.
"""
self.label.setText('PyQt5 Application')
if __name__ == '__main__':
app = QApplication(sys.argv)
dlg = MainDialog()
dlg.show()
sys.exit(app.exec_())
Save and run the script (F2). The form will display correctly. Clicking the Login button will change the label text to 'PyQt5 Application'.