[英]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.