简体   繁体   中英

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

# -*- 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;
  • 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). Follow the guide about using Designer to understand the correct way of dealing with those files (which must only used as imported modules).

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.
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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