Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating Dialogs

00:00 Dialogs. In PyQt, you can develop two types of GUI desktop applications. Depending on the class that you use to create the main form or window, you’ll either have a main window-style application where the application’s main window inherits from QMainWindow or a dialog-style application where the application’s main window inherits from QDialog.

00:24 You’ll start with a dialog-style application, and in the next section you’ll learn about main window-style applications. To develop a dialog-style application, you’ll need to create a GUI class that inherits from QDialog, which is the base class of all dialog windows.

00:41 A dialog window is a standalone window that you can use as the main window for your application. They’re commonly used in main window-style applications for brief communication and interaction with the user.

00:53 When you use dialog windows to communicate with the user, those dialogu can be: modal where it blocks input to any other visible windows in the same application.

01:03 You can display a modal dialog by calling its exec() method, or modeless, where it operates independently from other windows in the same application and these can be displayed by using the show() method. Dialog windows can also provide a return value and have default buttons, such as OK and Cancel. A dialog is always an independent window.

01:26 If it has a parent, then it will display centered on top of the parent widget. Dialogs with a parent will share the parent’s task bar entry.

01:35 If you don’t set parent for a given dialog, then the dialog will get its own entry in the system’s task bar. On screen you can see an example of how you’d use QDialog to develop a dialog-style application.

01:51 First, the imports are performed with a few new widgets, which will be used here.

02:06 Here you define a Window class for the app’s GUI by inheriting from QDialog.

02:14 This line calls the parent class’s __init__() method by using super(). This call allows you to properly initialize instances of this class.

02:23 In this example, the parent argument is set to None because this dialog will be your main window. Next, you set the window’s title and this assigns a QVBoxLayout object to dialogLayout.

02:40 This line assigns a QFormLayout object to formLayout. Next, you add widgets to the formLayout.

03:06 This line calls addLayout on dialog layout, embedding the formLayout into the global dialog layout. Next, you define a button box, which provides a convenient space to display the dialog’s buttons.

03:23 These lines add two standard buttons, OK, and Cancel to the dialog.

03:38 This adds the button box to the dialog by calling addWidget.

03:48 The next block of code uses a common Python idiom. The if __name__ == '__main__' construct wraps up the app’s main code. This ensures that the indented code will only run if the containing file is executed as a program rather than imported as a module.

04:08 For more about this construct, check out this Real Python video course.

04:14 On line 23, you’ll see that layout managers can be nested inside one another. You can nest layouts by calling addLayout on the container layout with a nested layout as an argument.

04:27 Once this code is saved, you can run it as seen on screen. Know that you can type information and move between fields as before, but the OK and Cancel buttons currently don’t do anything.

04:39 You still have to use the Close button for the window to close it. You’ll see how to connect buttons to actions later on in the course. But now that you’ve seen how dialog style applications work, in the next section of the course, you’ll take a look at PyQt’s other application style main window.

Become a Member to join the conversation.