当前位置:网站首页>Pyqt5 rapid development and practice 5.2 container: load more controls
Pyqt5 rapid development and practice 5.2 container: load more controls
2022-07-28 15:51:00 【Ding Jiaxiong】
PyQt5 Rapid development and actual combat
List of articles
5. The first 5 Chapter PyQt5 Advanced interface controls
5.2 Containers : Load more controls
How to load more controls in the existing window space .
5.2.1 QTabWidget
QTabWidget Control provides a tab and a page area , The first tab page is displayed by default . You can view the corresponding page by clicking each tab . If there are many input fields displayed in one window , You can split these fields , Placed in tabs on different pages .
QTabWidget Common methods in class
| Method | describe |
|---|---|
| addTab() | Add a control to Tab Control |
| insertTab() | Will a Tab The tab of the control is inserted into the specified location |
| removeTab() | Divide according to the specified index Tab Control |
| setCurrentIndex() | Set the index of the currently visible tab |
| setCurrentWidget() | Set the currently visible page |
| setTabBar() | Set the gizmo of the tab bar |
| setTabPosition() | Set the location of the tab :QTabWidgct.North, Displayed at the top of the page ;QTabWidget.South, Displayed at the bottom of the page ;QTabWidget.West, Displayed on the left side of the page ;QTabWidget.East, Displayed on the right side of the page |
| setTabText() | Definition Tab Display value of tab |
QTabWidget Class
| The signal | describe |
|---|---|
| currentChanged | Transmit this signal when switching the current page |
Case study ——QTabWidget Use
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class TabDemo(QTabWidget):
def __init__(self, parent=None):
super(TabDemo, self).__init__(parent)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab1, "Tab 1")
self.addTab(self.tab2, "Tab 2")
self.addTab(self.tab3, "Tab 3")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.setWindowTitle("Tab Example ")
def tab1UI(self):
layout = QFormLayout()
layout.addRow(" full name ", QLineEdit())
layout.addRow(" Address ", QLineEdit())
self.setTabText(0, " Contact information ")
self.tab1.setLayout(layout)
def tab2UI(self):
layout = QFormLayout()
sex = QHBoxLayout()
sex.addWidget(QRadioButton(" male "))
sex.addWidget(QRadioButton(" Woman "))
layout.addRow(QLabel(" Gender "), sex)
layout.addRow(" Birthday ", QLineEdit())
self.setTabText(1, " Personal details ")
self.tab2.setLayout(layout)
def tab3UI(self):
layout = QHBoxLayout()
layout.addWidget(QLabel(" subject "))
layout.addWidget(QCheckBox(" Physics "))
layout.addWidget(QCheckBox(" Advanced Mathematics "))
self.setTabText(2, " Education level ")
self.tab3.setLayout(layout)
if __name__ == '__main__':
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = TabDemo()
win.show()
sys.exit(app.exec_())

5.2.2 QStackedWidget
QStackedWidget Is a stack window control , You can fill in some widgets , But only one widget can be displayed at a time .OStackedWidget Use QStackedLayout Layout .QStackedWidget Control and QTabWidget similar , It can effectively display the controls in the window .
Case study ——QTabWidget Use
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class StackedExample(QWidget):
def __init__(self):
super(StackedExample, self).__init__()
self.setGeometry(300, 50, 10, 10)
self.setWindowTitle('StackedWidget Example ')
self.leftlist = QListWidget()
self.leftlist.insertItem(0, ' Contact information ')
self.leftlist.insertItem(1, ' Personal information ')
self.leftlist.insertItem(2, ' Education level ')
self.stack1 = QWidget()
self.stack2 = QWidget()
self.stack3 = QWidget()
self.stack1UI()
self.stack2UI()
self.stack3UI()
self.Stack = QStackedWidget(self)
self.Stack.addWidget(self.stack1)
self.Stack.addWidget(self.stack2)
self.Stack.addWidget(self.stack3)
hbox = QHBoxLayout(self)
hbox.addWidget(self.leftlist)
hbox.addWidget(self.Stack)
self.setLayout(hbox)
self.leftlist.currentRowChanged.connect(self.display)
def stack1UI(self):
layout = QFormLayout()
layout.addRow(" full name ", QLineEdit())
layout.addRow(" Address ", QLineEdit())
self.stack1.setLayout(layout)
def stack2UI(self):
layout = QFormLayout()
sex = QHBoxLayout()
sex.addWidget(QRadioButton(" male "))
sex.addWidget(QRadioButton(" Woman "))
layout.addRow(QLabel(" Gender "), sex)
layout.addRow(" Birthday ", QLineEdit())
self.stack2.setLayout(layout)
def stack3UI(self):
layout = QHBoxLayout()
layout.addWidget(QLabel(" subject "))
layout.addWidget(QCheckBox(" Physics "))
layout.addWidget(QCheckBox(" Advanced Mathematics "))
self.stack3.setLayout(layout)
def display(self, i):
self.Stack.setCurrentIndex(i)
if __name__ == '__main__':
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = StackedExample()
win.show()
sys.exit(app.exec_())

5.2.3 QDockWidget
QDock Widget Is one that can dock at QMainWindow Window controls in , It can be kept floating or attached to the main window as a child window at a specified position .QMainWindow The main window object of class retains an area for docking the window , This area is around the center of the control .
QDockWidget Control can be moved to a new area in the main window .
QDockWidget Common methods in class :
| Method | describe |
|---|---|
| setWidget() | stay Dock Window locale QWidget |
| setFloating() | Set up Dock Whether the window can float , If set to True, It means that it can float |
| setAllowedAreas() | Set the area where the window can dock ;LeftDockWidget.Area, Left docking area ;RightDockWidgetArea, Right docking area ;TopDockWidgetArea, Top docking area ;BottomDockWidgetArea, Bottom docking area ;NoDockWidgetArea, No display Widget; |
| setFeatures() | Set the function properties of the docked window ;DockWidgetClosable, Can be turned off ;DockWidgetMovable, Movable ;DockWidgetFloatable, Floatable ;DockWidget VerticalTitleBar, Display the vertical tab bar on the left ;AllDockWidgetFeatures, All functions with the first three attributes ;NoDockWidgetFeatures, Unable to close , Can't move , Can't float ; |
Case study ——QDockWidget Use
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class DockDemo(QMainWindow):
def __init__(self,parent = None):
super(DockDemo, self).__init__(parent)
layout = QHBoxLayout()
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
file.addAction("save")
file.addAction("quit")
self.items = QDockWidget("Dockable",self)
self.listWidget = QListWidget()
self.listWidget.addItem("Item1")
self.listWidget.addItem("Item2")
self.listWidget.addItem("Item3")
self.items.setWidget(self.listWidget)
self.items.setFloating(False)
self.setCentralWidget(QTextEdit())
self.addDockWidget(Qt.RightDockWidgetArea,self.items)
self.setLayout(layout)
self.setWindowTitle("Dock Case study ")
if __name__ == '__main__':
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = DockDemo()
win.show()
sys.exit(app.exec_())

5.2.4 Multi document interface
A typical GUI The application may have multiple windows , Tab controls and stack window controls allow you to use one of them at a time . However , Many times this method is not very useful , Because the views of other windows are hidden .
One way to display multiple windows at the same time is , Create multiple independent windows , These separate windows are called SDI (Single Document Interface, Single document interface ), Each window can have its own menu system 、 Toolbars, etc . This requires more memory resources .
MDI (Multiple Document Interface, Multi document interface ) Applications use less memory resources , Child windows can be placed in the main window container , This container control is called QMdiArea.
QMidArea Controls usually occupy QMainWindow The central position of the object , The child window in this area is QMdiSubWindow Class , You can set any QWidget Internal controls as child window objects , Child window in MDI Areas are arranged in cascading arrangement .
QMdiArea Classes and QMdiSubWindow Common methods in class :
| Method | describe |
|---|---|
| addSubWindow() | Add a widget to MDI Area as a new sub window |
| removeSubWindow() | Delete a widget in a child window |
| setActiveSubWindow() | Activate a child window |
| cascadeSubWindow() | Schedule child windows in MDI Area cascading display |
| tileSubWindows() | Schedule child windows in MDI Area tile |
| closeActiveSubWindow() | Close the active child window |
| subWindowList() | return MDI A list of child windows of the area |
| setWidget() | Set a widget as QMdiSubwindow Internal control of instance object |
Case study —— Multi document interface
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
count = 0
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
file.addAction("cascade")
file.addAction("Tiled")
file.triggered[QAction].connect(self.windowaction)
self.setWindowTitle("MDI demo")
def windowaction(self, q):
print("triggered")
if q.text() == "New":
MainWindow.count = MainWindow.count + 1
sub = QMdiSubWindow()
sub.setWidget(QTextEdit())
sub.setWindowTitle("subwindow" + str(MainWindow.count))
self.mdi.addSubWindow(sub)
sub.show()
if q.text() == "cascade":
self.mdi.cascadeSubWindows()
if q.text() == "Tiled":
self.mdi.tileSubWindows()
if __name__ == '__main__':
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())

5.2.5 QScrollBar
You can see , What the previous window controls have in common is to create new windows to load more controls , and QScrollBar Provides another way of thinking : This window control provides a horizontal or vertical scroll bar , This can expand the effective loading area of the current window , To load more controls .
QScrollBar Class :
| The signal | meaning |
|---|---|
| valueChanged | This signal is emitted when the value of the slider changes |
| sliderMoved | This signal is sent when the user drags the slider |
Case study ——QScrollBar
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout()
self.l1 = QLabel(" Drag the slider to change the color ")
self.l1.setFont(QFont("Arial", 16))
hbox.addWidget(self.l1)
self.s1 = QScrollBar()
self.s1.setMaximum(255)
self.s1.sliderMoved.connect(self.sliderval)
self.s2 = QScrollBar()
self.s2.setMaximum(255)
self.s2.sliderMoved.connect(self.sliderval)
self.s3 = QScrollBar()
self.s3.setMaximum(255)
self.s3.sliderMoved.connect(self.sliderval)
hbox.addWidget(self.s1)
hbox.addWidget(self.s2)
hbox.addWidget(self.s3)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QScrollBar Example ')
self.setLayout(hbox)
def sliderval(self):
print(self.s1.value(), self.s2.value(), self.s3.value())
palette = QPalette()
c = QColor(self.s1.value(), self.s2.value(), self.s3.value(), 255)
palette.setColor(QPalette.Foreground, c)
self.l1.setPalette(palette)
if __name__ == '__main__':
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = Example()
win.show()
sys.exit(app.exec_())

边栏推荐
- Endnote is associated with word
- 软件架构与设计(十)-----架构技术
- Leetcode bracket validity problem
- DNS域名解析协议
- 屏下指纹价格战再起,二线厂商今年有望拿下30%市场?
- How as makes intelligent prompts regardless of case
- Communication between client and server based on rsocket protocol
- Using SYSTEMd to manage services
- Docker容器实现MySQL主从复制
- Software architecture and design (VIII) -- distributed architecture
猜你喜欢

高速计数器转RS485Modbus RTU模块IBF150

5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95

如何快速接入统一的认证鉴权体系

Software architecture and design (IV) -- data flow architecture

一文了解 Rainbond 云原生应用管理平台

【直播预约】数据架构演进下的新挑战——上海站

Encoder high speed pulse counter Modbus RTU module ibf150

比例电磁阀控制阀4-20mA转0-165mA/330mA信号隔离放大器

Docker implements redis cluster mode hash slot partition for 100 million level data storage

虚拟机之NAT模式下设置静态IP
随机推荐
生命的感悟
Regular expression (4)
samba服务器如何配置
MySQL add and delete indexes
12V脉冲转速测量转24V电平信号转换变送器
22. Realization of message processing task
Canoe tutorial
5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95
Docker container implements MySQL master-slave replication
Return the two subscripts of the array according to the input target.
Stateflow logic system modeling
Open light input / relay output rs485/232 remote data acquisition IO module ibf70
PyQt5快速开发与实战 5.2 容器:装载更多的控件
跟我学Rx编程——Concat
21. Definition of message processing task
Software architecture and design (II) -- Architecture Model
The price war of off screen fingerprints has resumed, and second-line manufacturers are expected to win 30% of the market this year?
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
Matlab does not overwrite importing Excel
[delete specified number leetcode]