gpt4 book ai didi

python - 如何向 pyqt 5 gui 添加滚动条?

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:06 25 4
gpt4 key购买 nike

我创建了一个带有 Controller 文件的 GUI,用于在窗口之间切换。这些文件包含几个将由以前的窗口设置的参数,因此,大小可能相当大。因此,我需要创建一个滚动条才能到达最后一个元素,但想知道如何在我的代码中实现它。

来自controller.py

import sys
from PyQt5 import QtCore, QtWidgets

from firstwindow import Login
class Controller:

def __init__(self):
pass

def show_login(self):
self.login = Login()
self.login.switch_window.connect(self.show_main)
self.login.show()

def show_main(self):
self.MAP = self.login.MAP
#Parameters from file
self.NUM = self.login.spinBoxNUM.value()
self.login.close()


def main():
app = QtWidgets.QApplication(sys.argv)
controller = Controller()
controller.show_login()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

以及来自登录类文件:第一个窗口.py

import sys
from PyQt5 import QtCore, QtWidgets, QtGui

class Login(QtWidgets.QWidget):

switch_window = QtCore.pyqtSignal()

def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.setWindowTitle('First')

def setupUi(self, FirstWindow):
FirstWindow.setObjectName("First")
FirstWindow.setEnabled(True)
FirstWindow.resize(675,776)

FirstWindow.setFocusPolicy(QtCore.Qt.TabFocus)

layout = QtWidgets.QGridLayout()
#Lots of elements + buttons: example:
self.spinBoxNUM = QtWidgets.QSpinBox()
layout.addWidget(self.spinBoxNUM,1,2)
self.spinBoxLEVELS = QtWidgets.QSpinBox()
self.spinBoxLEVELS.setValue(2)
self.spinBoxLEVELS.setMaximum(156)
layout.addWidget(self.spinBoxLEVELS,11,2)

#CONTINUE AND QUIT BUTTON
self.QuitButton = QtWidgets.QPushButton("Quit")
self.QContinueButton = QtWidgets.QPushButton("Continue")
#actions
self.QuitButton.clicked.connect(FirstWindow.close)
self.QContinueButton.clicked.connect(self.login)
layout.addWidget(self.QuitButton,15,1)
layout.addWidget(self.QContinueButton,15,2)

self.setLayout(layout)
def login(self):
self.MAP = [[1 for x in range(4)]for y in range(self.LEVELS)]
self.switch_window.emit()

最佳答案

尝试一下:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

#from firstwindow import Login
class Login(QtWidgets.QWidget):

switch_window = QtCore.pyqtSignal()

def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.setWindowTitle('First')

def setupUi(self, FirstWindow):
FirstWindow.setObjectName("First")
FirstWindow.setEnabled(True)
FirstWindow.resize(675, 776)

FirstWindow.setFocusPolicy(QtCore.Qt.TabFocus)


#Lots of elements + buttons: example:
self.spinBoxNUM = QtWidgets.QSpinBox()
self.spinBoxLEVELS = QtWidgets.QSpinBox()
self.spinBoxLEVELS.setValue(2)
self.spinBoxLEVELS.setMaximum(156)

#CONTINUE AND QUIT BUTTON
self.QuitButton = QtWidgets.QPushButton("Quit")
self.QContinueButton = QtWidgets.QPushButton("Continue")
#actions
self.QuitButton.clicked.connect(FirstWindow.close)
self.QContinueButton.clicked.connect(self.login)


layout = QtWidgets.QGridLayout()
layout.addWidget(self.spinBoxNUM, 1, 2)
layout.addWidget(self.spinBoxLEVELS, 11, 2)
layout.addWidget(self.QuitButton, 15, 1)
layout.addWidget(self.QContinueButton,15, 2)

self.setLayout(layout)

def login(self):
self.MAP = [[1 for x in range(4)]for y in range(7)] # ??? (self.LEVELS)]
self.switch_window.emit()

def sizeHint(self): # <--- sizeHint
return QtCore.QSize(700, 500)



#class Controller:
class Controller(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.show_login()

def show_login(self):
self.login = Login()

self.scrollArea = QtWidgets.QScrollArea() # +++
self.scrollArea.setWidget(self.login) # +++

self.login.switch_window.connect(self.show_main)

self.scrollArea.show() # +++
self.scrollArea.resize(700, 500) # +++
# self.login.show()

def show_main(self):
self.MAP = self.login.MAP
#Parameters from file
self.NUM = self.login.spinBoxNUM.value()

# self.login.close()
self.scrollArea.hide() # +++
self.show() # +++


def main():
app = QtWidgets.QApplication(sys.argv)
controller = Controller()
controller.setWindowTitle("Controller")
sys.exit(app.exec_())

if __name__ == '__main__':
main()

enter image description here

关于python - 如何向 pyqt 5 gui 添加滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57160996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com