如何向 pyqt 5 gui 添加滚动条?
Posted
技术标签:
【中文标题】如何向 pyqt 5 gui 添加滚动条?【英文标题】:How to add a scrollbar to pyqt 5 gui? 【发布时间】:2019-07-23 09:29:53 【问题描述】:我创建了一个带有控制器文件的 gui 来在窗口之间切换。这些文件包括几个参数,这些参数将由以前的窗口设置,因此,大小可能非常大。因此,我需要创建一个滚动条才能到达最后一个元素,但想知道如何在我的代码中实现这一点。
来自控制器.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()
【问题讨论】:
对不起,我对此很陌生,我应该做更多的最小还是可重现的问题? 为了向您推荐一些东西,我需要运行您的示例并重现问题。我如何运行您的示例? 谢谢,我相信它现在已经修复了:) 【参考方案1】:试试看:
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()
【讨论】:
以上是关于如何向 pyqt 5 gui 添加滚动条?的主要内容,如果未能解决你的问题,请参考以下文章