In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use PyQt to create a professional appearance of GUI", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use PyQt to create a professional appearance of GUI" bar!
Quick form creation: QFormLayout
If you have been creating forms to perform operations such as entering data into the database, then QFormLayout is for you. This class arranges the widget into a two-column layout. The first column usually displays tags that describe the expected input, and the second column usually contains input widgets, such as QLineEdit,QComboBox or QSpinBox, that allow the user to enter or edit data.
To add widgets to the form layout, use .addRow (). This method has several variables, but in most cases, you can choose from the following two:
.addRow (label,field) adds a new line to the bottom of the form layout. The line should contain a QLabel object (label) and an input widget (field).
.addRow (labelText,field) automatically creates and adds a new QLabel object with labelText as its text. Field. Field contains an input widget.
This is a sample application that uses QFormLayout objects to arrange widgets:
Import sys from PyQt5.QtWidgets import (QApplication, QFormLayout, QLabel, QLineEdit, QWidget,) class Window (QWidget): def _ _ init__ (self): super (). _ init__ () self.setWindowTitle ("QFormLayout Example") self.resize # Create a QHBoxLayout instance layout = QFormLayout () # Add widgets to the layout layout.addRow ("Name:", QLineEdit ()) layout.addRow ("Job:", QLineEdit ()) emailLabel = QLabel ("Email:") layout.addRow (emailLabel QLineEdit () # Set the layout on the application's window self.setLayout (layout) if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) window = Window () window.show () sys.exit (app.exec_ ())
In line 17, create a QFormLayout object. Then, on lines 19 to 22, add some lines to the layout. Notice that in lines 19 and 20, you use the second variable of the method, and on line 22, you use the first variable, passing the QLabel object as the first parameter to .addRow ().
If you run this code, you will see the following window on the screen:
Using QFormLayout, you can organize widgets in two columns. The first column contains tags that require the user to provide some information. The second column shows the widgets that allow the user to enter or edit this information.
Nesting layouts to build complex GUI
You can use nested layouts to create complex GUI, and it is difficult to create these GUI using one of the layout managers of generic PyQt. To do this, you need to call .addLayout () on the external layout. In this way, the internal layout becomes a child of the external layout.
Suppose you need to create a dialog box that displays labels and row edits in the form layout, and below these widgets you want to place multiple check boxes in the vertical layout. This is a model of the appearance of your dialog box:
The blue rectangle represents your external layout. A green rectangle is a form layout that retains tags and row edits. The red rectangle is the vertical layout used to accommodate the option check box. Both green and red layouts are nested in blue layouts, which are vertical layouts.
This is an example of how to build this layout using PyQt:
Import sys from PyQt5.QtWidgets import (QApplication, QCheckBox, QFormLayout, QLineEdit, QVBoxLayout, QWidget ) class Window (QWidget): def _ init__ (self): super (). _ init__ () self.setWindowTitle ("Nested Layouts Example") # Create an outer layout outerLayout = QVBoxLayout () # Create a form layout for the label and line edit topLayout = QFormLayout () # Add a label and a line edit to the form layout topLayout.addRow ("Some Text:" QLineEdit () # Create a layout for the checkboxes optionsLayout = QVBoxLayout () # Add some checkboxes to the layout optionsLayout.addWidget (QCheckBox ("Option one")) optionsLayout.addWidget (QCheckBox ("Option two")) optionsLayout.addWidget (QCheckBox ("Option three")) # Nest the inner layouts into the outer layout outerLayout.addLayout (topLayout) outerLayout.addLayout OptionsLayout) # Set the window's main layout self.setLayout (outerLayout) if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) window = Window () window.show () sys.exit (app.exec_ ())
This is what you do in this code:
In line 17, you will create an external or top-level layout and use it as the main layout for the parent layout and window. In this case, QVBoxLayout is used because you want the widgets to be arranged vertically on the form. In your model, this is the blue layout.
In line 19, you create a form layout to hold the tags and row edits.
In line 21, add the required widgets to the layout. This is equivalent to your green layout.
In line 23, you will create a vertical layout to accommodate the check box.
On lines 25 to 27, add the desired check boxes. This is your red layout.
On lines 29 and 30, nest topLayout and optionsLayout under outsideLayout.
If you run the application, you will see a window similar to the following:
In this application, you nest two different layouts under an external layout to create a general layout for the window. At the top of the window, use a horizontal layout to place labels and row edits. Then, use a vertical layout to place some check boxes below it.
Use multi-page layouts and widgets
So far, you have learned how to use traditional or generic layout managers to arrange widgets in the windows of your application. These layout managers place widgets on a single-page layout. In other words, your GUI will always display the same set of widgets to the user.
Sometimes you need to create a layout to display a different set of widgets in response to certain user actions on the GUI. For example, if you want to create a preference dialog box for a given application, you may need to display a tab-based or multi-page layout to the user, where each tab or page contains a different set of closely related options. Each time a user clicks a tab or page, the application displays a different set of widgets.
PyQt provides a built-in layout called QStackedLayout and handy widgets (such as QTabWidget) that will allow you to create such a multi-page layout. The next few sections will take you step by step through some of these tools.
Create a widget stack
QStackedLayout provides a layout manager that allows you to arrange widgets on one stack, one on the other. In this layout, only one widget is visible at a given time.
To populate the stacked layout with widgets, you need to call .addWidget () on the layout object. This adds each widget to the end of the list of widgets within the layout. You can also insert or delete widgets at a given location in the widget list using .insertWidget (index) or .removeWidget (widget), respectively.
Each widget in the widget list is displayed as a separate page. If you want to display multiple widgets on a page, use the QWidget object for each page and set the appropriate widget layout for the page widget. If you need to get the total number of small parts (pages) in the layout, you can call .count ().
The important thing to keep in mind when working with QStackedLayout objects is that you need to explicitly provide a mechanism for switching between pages. Otherwise, your layout will always display the same page to the user. To switch between pages, you need to call .setCurrentIndex () on the layout object.
This is an example of how to switch between pages using a stackable layout with combo boxes:
Import sys from PyQt5.QtWidgets import (QApplication, QComboBox, QFormLayout, QLineEdit, QStackedLayout, QVBoxLayout, QWidget ) class Window (QWidget): def _ init__ (self): super (). _ init__ () self.setWindowTitle ("QStackedLayout Example") # Create a top-level layout layout = QVBoxLayout () self.setLayout (layout) # Create and connect the combo box to switch between pages self.pageCombo = QComboBox () self.pageCombo.addItems (["Page 1") "Page 2"]) self.pageCombo.activated.connect (self.switchPage) # Create the stacked layout self.stackedLayout = QStackedLayout () # Create the first page self.page1 = QWidget () self.page1Layout = QFormLayout () self.page1Layout.addRow ("Name:", QLineEdit ()) self.page1Layout.addRow ("Address:" QLineEdit () self.page1.setLayout (self.page1Layout) self.stackedLayout.addWidget (self.page1) # Create the second page self.page2 = QWidget () self.page2Layout = QFormLayout () self.page2Layout.addRow ("Job:", QLineEdit ()) self.page2Layout.addRow ("Department:" QLineEdit () self.page2.setLayout (self.page2Layout) self.stackedLayout.addWidget (self.page2) # Add the combo box and the stacked layout to the top-level layout layout.addWidget (self.pageCombo) layout.addLayout (self.stackedLayout) def switchPage (self): self.stackedLayout.setCurrentIndex (self.pageCombo.currentIndex ()) if _ _ name__ = "_ _ main__ App = QApplication (sys.argv) window = Window () window.show () sys.exit (app.exec_ ())
In lines 21 to 23, you will create a QComboBox object that will allow you to switch between pages in the layout. Then, add two options to the combo box in the list and connect them to .switchPage () designed to handle page switching.
Inside .switchPage (), you call .setCurrentIndex () on the layout object, passing the current index of the combo box as a parameter. In this way, when the user changes the options in the combo box, the pages on the stack layout change accordingly.
On line 25, create the QStackedLayout object. On lines 27 to 32, add the first page to the layout, and on lines 34 to 39, add the second page to the layout. Each page is represented by a QWidget object that contains multiple widgets in a convenient layout.
The final step in making everything work properly is to add combo boxes and layouts to the main layout of the application.
Your application now behaves as follows:
In this case, there are two pages in your application layout. Each page is represented by a QWidget object. When you select a new page in the combo box at the top of the window, the layout changes to display the selected page.
In addition to stacked layouts and stacked widgets, you can also use QTabWidget to create multi-page user interfaces. You will learn how to do this in the next section.
Using PyQt's tag widget
Another popular way to create multi-page arrangement in PyQt is to use a class called QTabWidget. This class provides a label bar and a page area. You can use the tab bar to switch between pages and use the page area to display the pages associated with the selected tabs.
By default, the tab bar is located at the top of the page area. However, you can use .setTabPosition () and one of the four possible tab positions to change this behavior:
To add tabs to the tab widget, use .addTab (). This method has two variable or overloaded implementations:
1, .addTab (page, label)
2, .addTab (page, icon, label)
In both cases, the method adds a new tag, with label as the tag title. . The page must be a widget that represents the page associated with the tab at hand.
In the second variable of the method, the icon must be a QIcon object. If you pass the icon to .addTab (), the icon is displayed to the left of the label title.
A common practice when creating tab widgets is to use QWidget objects for each page. In this way, you can add additional widgets to the page using the layout that contains the required widgets.
In most cases, you will use the tag widget to create dialogs for GUI applications. This layout allows you to provide users with multiple options in a relatively small space. You can also use the tab system to organize options according to some classification criteria.
This is a sample application that shows the basics of how to create and use QTabWidget objects:
Import sys from PyQt5.QtWidgets import (QApplication, QCheckBox, QTabWidget, QVBoxLayout, QWidget,) class Window (QWidget): def _ _ init__ (self): super (). _ init__ () self.setWindowTitle ("QTabWidget Example") self.resize # Create a top-level layout layout = QVBoxLayout () self.setLayout (layout) # Create the tab widget with two tabs tabs = QTabWidget () tabs.addTab (self.generalTabUI (), "General") tabs.addTab (self.networkTabUI () "Network") layout.addWidget (tabs) def generalTabUI (self): "Create the General page UI." GeneralTab = QWidget () layout = QVBoxLayout () layout.addWidget (QCheckBox ("General Option 1")) layout.addWidget (QCheckBox ("General Option 2")) generalTab.setLayout (layout) return generalTab def networkTabUI (self): "" Create the Network page UI. "" NetworkTab = QWidget () layout = QVBoxLayout () layout.addWidget (QCheckBox ("Network Option 1")) layout.addWidget (QCheckBox ("Network Option 2")) networkTab.setLayout (layout) return networkTab if _ _ name__ = = "_ main__": app = QApplication (sys.argv) window = Window () window.show () sys.exit (app.exec_ ())
In this example, you use a tab widget to show the user a concise dialog box that displays options related to the General and Network sections of the hypothetical preferences menu. On line 20, create the QTabWidget object. Then, use .addTab () to add two tabs to the tab widget.
In .generalTab UI () and networkTabUI (), create a specific GUI for each tab. To do this, you can use the QWidget object, the QVBoxLayout object, and some check boxes to save the options.
If you run the application immediately, the following dialog box appears on the screen:
You have a full-featured tab-based GUI. Note that to switch between pages, simply click the appropriate tab.
Thank you for your reading, the above is "how to use PyQt to create a professional appearance of GUI" content, after the study of this article, I believe you on how to use PyQt to create a professional appearance of GUI this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.