简体   繁体   English

如何在python PYQT中使用“ScrollArea”

[英]How use "ScrollArea" in python PYQT

I'm from another country and I'm just learning english then get it我来自另一个国家,我只是在学习英语然后得到它

All today's morning I had tried to make the code work I do not know what to do My purpose is Scroll GroupBox which keep many-many buttons今天整个早上我都试图让代码工作我不知道该怎么做我的目的是滚动 GroupBox,它保留了许多按钮

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'buttons.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(378, 368)
        buttons = []
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(120, 80, 141, 151))
        self.groupBox.setObjectName("groupBox")
        for i in range(1,30):
            buttons.append((QtWidgets.QPushButton(self.groupBox), QtWidgets.QPushButton(self.groupBox)))
            buttons[-1][0].setGeometry(QtCore.QRect(10, i*30, 90, 25))
            buttons[-1][0].setObjectName("group_{0}".format(i))
            buttons[-1][1].setGeometry(QtCore.QRect(100, i*30, 20, 25))
            buttons[-1][0].setObjectName("del_{0}".format(i))
        # Two next rows can be comment to show the groupBox
        scroll = QtWidgets.QScrollArea()
        scroll.setWidget(self.groupBox)


        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Имя группы"))

class ExampleApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

def main():
    app = QtWidgets.QApplication(sys.argv)
    window = ExampleApp()
    window.show()
    app.exec_()

if __name__ == "__main__":
    main()

Please, help me.请帮我。 I do not know what to do!我不知道该怎么办!

It won't work mostly for these reasons:由于以下原因,它不会起作用:

  • scroll is a local variable, so Python will garbage collect it as soon as setupUi returns; scroll是一个局部变量,所以 Python 会在setupUi返回后立即setupUi垃圾回收;
  • it has no parent set, so it wouldn't be shown;它没有父集,因此不会显示;

Also, most importantly, you should NEVER edit the output of pyuic for any reason (even if you believe you know what you're doing, but if you knew, you wouldn't edit it).此外,最重要的是,您永远不应该出于任何原因编辑 pyuic 的输出(即使您相信自己知道自己在做什么,但如果您知道,就不会编辑它)。 Follow the guide about using Designer to understand the correct way of dealing with those files (which must only used as imported modules).按照有关使用 Designer的指南了解处理这些文件(必须仅用作导入模块)的正确方法。

Since you're trying to create your interface by code, there's no use in having a "Ui object", just do it in the main window class.由于您正在尝试通过代码创建界面,因此拥有“Ui 对象”是没有用的,只需在主窗口类中进行即可。
I also suggest you to always try to avoid fixed geometries, but use layout managers instead.我还建议您始终尽量避免使用固定几何图形,而是使用布局管理器。

class ExampleApp(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        # if the scroll area is the only "main" widget of the window, use it
        # as the central widget
        scroll = QtWidgets.QScrollArea()
        self.setCentralWidget(scroll)

        self.groupBox = QtWidgets.QGroupBox()
        scroll.setWidget(self.groupBox)
        scroll.setWidgetResizable(True)

        # create a grid layout for the groupbox
        groupLayout = QtWidgets.QGridLayout(self.groupBox)

        # the above is the same as:
        # groupLayout = QtWidgets.QGridLayout()
        # self.groupBox.setLayout(groupLayout)


        for row in range(30):
            button1 = QtWidgets.QPushButton()
            button1.setFixedWidth(90)
            groupLayout.addWidget(button1, row, 0)
            button2 = QtWidgets.QPushButton()
            button2.setFixedWidth(20)
            groupLayout.addWidget(button2, row, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM