2016-12-29 3 views
-1

pyqtでいくつかのページを作ってから、それらをpythonで編集しました。PythonでQStackedWidgetで循環的にページを切り替えるには?

3ページあり、このプログラムを3回実行するとします。つまり、ページ1からページ2からページ3からページ1を意味します。私は各ページを接続するために「次へ」ボタンを使用します。

私はループを試しました。ここで私のコードは動作しませんでした。

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_()) 
関連する問題