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

Understanding Main Windows

00:00 Main Windows. Most of the time, 2 your GUI applications will be of a main window-style. This means they’ll have a menu bar, some toolbars, a status bar, and a central widget that will be the GUI’s main element.

00:15 It’s also common for your apps to have several dialogs to accomplish secondary actions that will depend on a user’s input. You’ll inherit from QMainWindow to develop main window-style applications.

00:29 An instance of a class that derives from QMainWindow is considered the app’s main window and should be unique. QMainWindow provides a framework for building your application’s GUI quickly.

00:42 This class has its own built-in layout, which accepts the graphical components seen on screen. You can’t create a main window without a central widget. You’ll need a central widget, even if it’s just a placeholder.

00:57 When this is the case, you can use a QWidget object for this purpose. You set the window central widget with the setCentralWidget() method.

01:06 The main window’s layout will allow you to have only one central widget, but it can be a single or composite widget. The code example seen on screen will show you how to use QMainWindow to create a main window-style application.

01:29 Once the relevant imports have been completed, this line creates a class window that inherits from QMainWindow. Here you define the class initializer and this calls the base class’s initializer.

01:45 Again, the parent argument is set to none because this is your app’s main window, so it must not have a parent. Here you set the window’s title and this sets QLabel as the window’s central widget.

02:03 These three lines call non-public methods to create different GUI elements.

02:15 createMenu() creates the main menu bar with a dropdown menu called Menu. This menu will have a menu option to exit the app.

02:29 createToolbar() creates the toolbar, which will have a toolbar button to exit the app.

02:44 The final function, createStatusBar(), creates the app’s status bar.

02:53 Implementing GUI components using their own methods as seen here with the menu bar, toolbar, and status bar makes the code more readable and maintainable.

03:04 The final block of code executes the application in the same way as you saw for the dialog example earlier in the course.

03:16 Here you can see the application running on Windows. You can see that your main window-style application has four components: a main menu generically called menu, a toolbar with an exit button, a central widget consisting of a QLabel object with a text message, and a status bar at the bottom of the window.

03:41 If you run this example on macOS, then you may have issues with the app’s main menu. macOS hides certain menu options such as exit. Remember that macOS will show the exit or quit option under the app’s entry at the top of the screen.

03:58 You’ve learned how to build a main window-style application with Python and PyQt. Up to this point, you’ve learned about some of the more important graphical components in PyQt’s set of widgets, but next, you’ll study other important concepts related to building GUI applications with PyQt.

Become a Member to join the conversation.