qt怎么在循环中创建多个qlabel

   2025-02-20 6290
核心提示:您可以使用循环来创建多个QLabel,然后将它们添加到您想要的布局中。这里是一个示例代码:import sysfrom PyQt5.QtWidgets impor

您可以使用循环来创建多个QLabel,然后将它们添加到您想要的布局中。这里是一个示例代码:

import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabelclass MyWidget(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):layout = QVBoxLayout()for i in range(5):label = QLabel("Label {}".format(i))layout.addWidget(label)self.setLayout(layout)self.show()if __name__ == '__main__':app = QApplication(sys.argv)widget = MyWidget()sys.exit(app.exec_())

在上面的例子中,我们创建了一个QWidget并使用QVBoxLayout作为其布局。然后,我们使用循环创建了5个QLabel,并将它们添加到布局中。最后,我们设置了该布局为QWidget的布局,并显示了该窗口。

这将创建一个窗口,其中包含5个标签,每个标签的文本为"Label n",其中n是从0到4的数字。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言