[英]How use "ScrollArea" in python PYQT
我来自另一个国家,我只是在学习英语然后得到它
今天整个早上我都试图让代码工作我不知道该怎么做我的目的是滚动 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()
请帮我。 我不知道该怎么办!
由于以下原因,它不会起作用:
scroll
是一个局部变量,所以 Python 会在setupUi
返回后立即setupUi
垃圾回收;此外,最重要的是,您永远不应该出于任何原因编辑 pyuic 的输出(即使您相信自己知道自己在做什么,但如果您知道,就不会编辑它)。 按照有关使用 Designer的指南了解处理这些文件(必须仅用作导入模块)的正确方法。
由于您正在尝试通过代码创建界面,因此拥有“Ui 对象”是没有用的,只需在主窗口类中进行即可。
我还建议您始终尽量避免使用固定几何图形,而是使用布局管理器。
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.