如何在python的QStackedWidget中循环翻页?

问题描述:

我在pyqt中创建了一些页面,然后用python编辑它们。如何在python的QStackedWidget中循环翻页?

我假设有3个页面,我希望这个程序运行3次,这意味着page1到page2到page3到page1。我使用“Next”按钮连接每个页面。

我试过循环。这是我的代码不起作用。

import sys 
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from test import * 

app = QApplication(sys.argv) 
window = QMainWindow() 
ui = Ui_MainWindow() 
ui.setupUi(window) 

for i in range(3): 
    def find_page(): 
     ui.stackedWidget.childern() 
    window.visible = ui.stackedWidget.currentIndex() 

    def next(): 
     ui.stackedWidget.setCurrentIndex(ui.stackedWidget.currentIndex()+1) 
     print(window.visible) 
    ui.next.clicked.connect(next) 
window.show() 
sys.exit(app.exec_()) 
+2

什么是不工作? – Dunno

下面是一个基于代码的示例,说明如何使用堆叠小部件更改页面。您没有发布您的UI文件,所以我不得不即兴创作其他小部件。你将不得不改变PyQt4的进口,但其余的应该是相同的:

import sys 

from PyQt5.QtCore import QTimer 
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QStackedWidget 

app = QApplication(sys.argv) 

window = QMainWindow() 
stack = QStackedWidget(parent=window) 
label1 = QLabel('label1') 
label2 = QLabel('label2') 
label3 = QLabel('label3') 
stack.addWidget(label1) 
stack.addWidget(label2) 
stack.addWidget(label3) 
print('current', stack.currentIndex()) 
window.show() 

def next(): 
     stack.setCurrentIndex(stack.currentIndex()+1) 
     print('current', stack.currentIndex()) 

QTimer.singleShot(1000, next) 
QTimer.singleShot(2000, next) 
QTimer.singleShot(3000, next) 
QTimer.singleShot(4000, app.quit) 

sys.exit(app.exec_())