Let's create a new application based on the Dialog without Buttons template. As said earlier, this application will prompt the user to enter a name and, on clicking the push button after entering a name, the application with display a hello message along with the entered name. Here are the steps to create this application:
- Drag a Label widget from the Display Widgets category and drop it on the form. Set its text property to Enter your name. Set the objectName property of the Label widget to labelResponse.
- Drag one more Label widget from the Display Widgets category and drop it on the form. Do not change the text property of this Label widget and leave its text property to its default value, TextLabel. This is because the text property of this Label widget will be set through code, that is, it will be used to display the hello message to the user.
- Drag one Line Edit from the Input Widgets category and drop it on the form. Set its objectName property to lineEditName.
- Drag one Push Button widget from the Buttons category and drop it onto the form. Set its text property to Click. You can change the text property of the Push Button widget through any of three ways: by double-clicking the Push Button widget and overwriting the default text, by right-clicking the Push Button widget and selecting the Change text... option from the context menu that pops up, or by selecting the text property from the Property Editor window and overwriting the default text.
- Set the objectName property of the Push Button widget to ButtonClickMe.
- Save the application with the name demoLineEdit.ui. Now the form will appear, as shown in the following screenshot:
The user interface that you create with Qt Designer is stored in a .ui file that includes all the form's information: its widgets, layout, and so on. The .ui file is an XML file, and you need to convert it to Python code. That way, you can maintain a clear separation between the visual interface and the behavior implemented in code.
- To use the .ui file, you first need to convert it into a Python script. The command utility that you will use for converting a .ui file into a Python script is pyuic5. In Windows, the pyuic5 utility is bundled with PyQt. To do the conversion, you need to open a Command Prompt window and navigate to the folder where the file is saved and issue the following command:
C:\Pythonbook\PyQt5>pyuic5 demoLineEdit.ui -o demoLineEdit.py
Let's assume that we saved the form at this location: C:\Pythonbook\PyQt5>. The preceding command shows the conversion of the demoLineEdit.ui file into a Python script, demoLineEdit.py.
The Python code generated by this method should not be modified manually, as any changes will be overwritten the next time you run the pyuic5 command.
The code of the generated Python script file, demoLineEdit.py, can be seen in the source code bundle of this book.
- Treat the code in the demoLineEdit.py file as a header file, and import it to the file from which you will invoke its user interface design.
The header file is a term referred to those files which are imported into the current file. The command to import such files is usually written at the top in the script, hence named as header files.
- Let's create another Python file with the name callLineEdit.py and import the demoLineEdit.py code into it as follows:
import sys
from PyQt5.QtWidgets import QDialog, QApplication
from demoLineEdit import *
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.ButtonClickMe.clicked.connect(self.dispmessage)
self.show()
def dispmessage(self):
self.ui.labelResponse.setText("Hello "
+self.ui.lineEditName.text())
if __name__=="__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())