[英]How to add a scrollbar to pyqt 5 gui?
I have created a gui with controller file to switch between windows. 我用控制器文件创建了一个GUI,可以在Windows之间切换。 The files includes several parameters which will be set by previous windows, hence, the size can be quite big.
该文件包含几个参数,这些参数将由以前的窗口设置,因此,大小可能会很大。 Therefore, I need to create a scrollbar to be able to reach the last elements, but wonder how I can implement this in my code.
因此,我需要创建一个滚动条以能够到达最后一个元素,但是想知道如何在代码中实现它。
from controller.py 来自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()
and from the Login class file: firstwindow.py 并从登录类文件中:firstwindow.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()
Try it: 试试吧:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.