![]() ![]() Then you import QApplication from PyQt5.QtWidgets and Window from views. In this module, you import sys to get access to exit(), which allows you to cleanly exit the application when the user closes the main window. argv ) # Create the main window win = Window () win. views import Window def main (): """RP Contacts main function.""" # Create the application app = QApplication ( sys. # -*- coding: utf-8 -*- # rpcontacts/main.py """This module provides RP Contacts application.""" import sys from PyQt5.QtWidgets import QApplication from. To do that, create a new module called main.py in your rpcontacts package and add the following code to it: Since you already have a main window for the contact book, it’s time to write the code for creating a functional PyQt application using QApplication. In the initializer method, you set the window’s title to "RP Contacts", resize the window to 550 by 250 pixels, define and set the central widget using QWidget, and finally define a layout for the central widget using a horizontal box layout. This class inherits from QMainWindow and provides the code to generate the application’s main window. layout )įirst, you import the required classes from PyQt5.QtWidgets. # -*- coding: utf-8 -*- """This module provides views to manage the contacts table.""" from PyQt5.QtWidgets import ( QHBoxLayout, QMainWindow, QWidget, ) class Window ( QMainWindow ): """Main Window.""" def _init_ ( self, parent = None ): """Initializer.""" super (). You can download them by clicking the link below: You’ll also create the minimal required project structure, including the project’s main package and an entry-point script to run the application.Īll the code and files you’ll add to the contact book project in this section are collected under the source_code_step_1/ directory. In this first step, you’ll create a minimal but functional PyQt GUI application to provide the foundation on which you’ll start building the contact book. Once you’ve installed PyQt, you’re ready to start coding! Step 1: Creating the Contact Book’s Skeleton App With PyQt To follow best practices in your development process, you can start by creating a virtual environment and then installing PyQt using pip. As a hint, you should pip install PyQt6 and then update the imports to use PyQt6 instead of PyQt5. If you feel the need to run the project with this new version of PyQt, then give it a try. However, the project in this tutorial hasn’t been tested with PyQt 6.0. ![]() This is the first release of the library that binds to Qt version 6. PyQt version 6.0 was released on January 4, 2021. ![]() Version 5.15.2 is required for the project to work on macOS Big Sur. Note: In this tutorial, you’ll use PyQt version 5.15.2 to build your contact book application. The contact book application you’re going to build in this tutorial has a single external dependency: PyQt. If you get stuck, then take your time and review the resources linked above. You’ll learn through the process of getting your hands dirty on a real project. Data Management With Python, SQLite, and SQLAlchemyĭon’t worry if you’re not an expert in these areas before starting this tutorial.Handling SQL Databases With PyQt: The Basics.PyQt Layouts: Create Professional-Looking GUI Applications.Python and PyQt: Creating Menus, Toolbars, and Status Bars.Python and PyQt: Building a GUI Desktop Calculator.To brush up on these topics, you can check out the following resources: Manage SQL databases with Python and PyQt.Create GUI applications with PyQt and Python.In this regard, you’ll need to know the basics of how to: ![]() To get the most out of this project, some previous knowledge of GUI programming with Python and PyQt would help. It’ll also have a dialog to add new contacts to the database. In general, the application will have a main window to display, add, remove, and update contacts. Finally, model.py will implement the model to manage the data in the application’s database. For example, views.py will contain the code to generate the GUI of windows and dialogs, database.py will contain code to work with the database, and main.py will host the application itself. The name of each file gives an idea of its role in the application. You’ll cover each of these files step by step in this tutorial. rpcontacts/ is a subdirectory that provides the application’s main package.rpcontacts.py provides the entry-point script to run the application.README.md provides general information about the project.requirements.txt provides the project’s requirements list.rpcontacts_project/ is the project’s root directory.Here’s a brief summary of the contents of your project directory: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |