-1

我在 pyqt 中制作了一些页面,然后在 python 中对其进行了编辑。

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

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

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_())
4

1 回答 1

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_())
于 2016-12-30T03:22:52.883 回答