2016-12-29 16 views
-1

我在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

回答

1

下面是一個基於代碼的示例,說明如何使用堆疊小部件更改頁面。您沒有發佈您的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_())