我正在制作一个多页面应用程序,其中我需要使用QStackedLayout
在页面之间导航。我制作了两个按钮Previous接下来和导航。
问题是按钮在点击2-3次后停止工作。我猜这与我试图在if
语句中检索CurrentIndex()
的值的方式有关。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.frame = QtWidgets.QFrame()
self.stack_lay = QtWidgets.QStackedLayout()
cam_widget = Page1(self)
self.stack_lay.addWidget(cam_widget)
self.stack_lay.setCurrentWidget(cam_widget)
self.frame.setLayout(self.stack_lay)
self.previous = QtWidgets.QPushButton('Previous')
self.previous.clicked.connect(self.previous_page)
self.previous.setDisabled(True)
self.next = QtWidgets.QPushButton('Next')
self.next.clicked.connect(self.next_page)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.frame)
layout.addWidget(self.previous)
layout.addWidget(self.next)
self.setLayout(layout)
def previous_page(self):
if self.stack_lay.currentIndex() == 1:
page1_widget = Page1(self)
self.stack_lay.addWidget(page1_widget)
self.stack_lay.setCurrentWidget(page1_widget)
self.previous.setDisabled(True)
self.next.setDisabled(False)
elif self.stack_lay.currentIndex() == 2:
page2_widget = Page2(self)
self.stack_lay.addWidget(page2_widget)
self.stack_lay.setCurrentWidget(page2_widget)
self.previous.setDisabled(False)
self.next.setDisabled(False)
def next_page(self):
if self.stack_lay.currentIndex() == 0:
page2_widget = Page2(self)
self.stack_lay.addWidget(page2_widget)
self.stack_lay.setCurrentWidget(page2_widget)
self.previous.setDisabled(False)
self.next.setDisabled(False)
elif self.stack_lay.currentIndex() == 1:
page3_widget = Page3(self)
self.stack_lay.addWidget(page3_widget)
self.stack_lay.setCurrentWidget(page3_widget)
self.previous.setDisabled(False)
self.next.setDisabled(True)
class Page1(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
label = QtWidgets.QLabel('Page 1')
lay = QtWidgets.QVBoxLayout()
lay.addWidget(label)
self.setLayout(lay)
class Page2(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Page2, self).__init__(parent)
label = QtWidgets.QLabel('Page 2')
lay = QtWidgets.QVBoxLayout()
lay.addWidget(label)
self.setLayout(lay)
class Page3(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Page3, self).__init__(parent)
label = QtWidgets.QLabel('Page 3')
lay = QtWidgets.QVBoxLayout()
lay.addWidget(label)
self.setLayout(lay)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = Window()
window.show()
sys.exit(app.exec_())
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.frame = QtWidgets.QFrame()
self.stack_lay = QtWidgets.QStackedLayout()
self.page1_widget = Page1(self)
self.page2_widget = Page2(self)
self.page3_widget = Page3(self)
self.stack_lay.addWidget(self.page1_widget)
self.stack_lay.addWidget(self.page2_widget)
self.stack_lay.addWidget(self.page3_widget)
self.stack_lay.setCurrentWidget(self.page1_widget)
self.frame.setLayout(self.stack_lay)
self.previous = QtWidgets.QPushButton('Previous')
self.previous.clicked.connect(self.previous_page)
self.previous.setDisabled(True)
self.next = QtWidgets.QPushButton('Next')
self.next.clicked.connect(self.next_page)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.frame)
layout.addWidget(self.previous)
layout.addWidget(self.next)
self.setLayout(layout)
def previous_page(self):
if self.stack_lay.currentIndex() == 1:
self.stack_lay.setCurrentWidget(self.page1_widget)
self.previous.setDisabled(True)
self.next.setDisabled(False)
elif self.stack_lay.currentIndex() == 2:
self.stack_lay.setCurrentWidget(self.page2_widget)
self.previous.setDisabled(False)
self.next.setDisabled(False)
def next_page(self):
if self.stack_lay.currentIndex() == 0:
self.stack_lay.setCurrentWidget(self.page2_widget)
self.previous.setDisabled(False)
self.next.setDisabled(False)
elif self.stack_lay.currentIndex() == 1:
self.stack_lay.setCurrentWidget(self.page3_widget)
self.previous.setDisabled(False)
self.next.setDisabled(True)
class Page1(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
label = QtWidgets.QLabel('Page 1')
lay = QtWidgets.QVBoxLayout()
lay.addWidget(label)
self.setLayout(lay)
class Page2(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Page2, self).__init__(parent)
label = QtWidgets.QLabel('Page 2')
lay = QtWidgets.QVBoxLayout()
lay.addWidget(label)
self.setLayout(lay)
class Page3(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Page3, self).__init__(parent)
label = QtWidgets.QLabel('Page 3')
lay = QtWidgets.QVBoxLayout()
lay.addWidget(label)
self.setLayout(lay)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = Window()
window.show()
sys.exit(app.exec_())