【python】QWidget父子关系,控件显示优先级原理剖析与应用实战演练
    		       		warning:
    		            这篇文章距离上次修改已过452天,其中的内容可能已经有所变动。
    		        
        		                
                
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton
 
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("父子关系示例")
        self.setGeometry(100, 100, 400, 200)
        self.create_ui()
 
    def create_ui(self):
        # 创建一个QWidget作为布局的容器
        container = QWidget()
        # 创建一个垂直布局
        layout = QVBoxLayout()
        # 创建两个按钮
        button1 = QPushButton("按钮1")
        button2 = QPushButton("按钮2")
        # 将按钮添加到布局中
        layout.addWidget(button1)
        layout.addWidget(button2)
        # 将布局设置给容器
        container.setLayout(layout)
        # 将容器设置为中心控件
        self.setCentralWidget(container)
 
# 应用程序入口
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()这段代码演示了如何在PyQt5中创建一个QMainWindow,并在其中添加两个QPushButton控件。通过使用QVBoxLayout来组织这些按钮,最后将布局设置给QMainWindow的中心控件。这样展示了如何通过父子关系和布局管理来控制控件的显示位置和层次关系。
评论已关闭