Python - 按一个按钮显示下一个标签

Python - showing next Tab by pressing a button

如何更改按按钮显示的标签? 通过按下消息框中的按钮调用该函数。 我试过 setCurrentIndex() 但显示的标签不会改变。

我正在使用 Python 3.6 和 pyqt5。

这是我的代码:

import sys
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
from PyQt5.QtWidgets import *

class main_window(QTabWidget):
    def __init__(self, parent=None):
        super(QTabWidget, self).__init__(parent)
        self.setGeometry(50, 50, 1100, 750)
        self.setWindowTitle("Programm")  #


        self.centralWidget = QtWidgets.QWidget()
        self.tabWidget = QtWidgets.QTabWidget(self.centralWidget)
        self.tabWidget.setGeometry(QtCore.QRect(10, 10, 1200, 1000))
        self.tabWidget.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.tabWidget.setTabPosition(QtWidgets.QTabWidget.West)

        self.tab_v1 = QtWidgets.QWidget()
        self.addTab(self.tab_v1, "Tab 1")

        self.tab_v2 = QtWidgets.QWidget()
        self.addTab(self.tab_v2, "Tab 2")

        self.tab_v3 = QtWidgets.QWidget()
        self.addTab(self.tab_v3, "Tab 3")

        self.openFile = QPushButton("Choose Tab ", self.tab_v1)
        self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))
        self.openFile.clicked.connect(self.on_click_do)


    def on_click_do(self):

        box1 = QMessageBox()
        box1.setIcon(QMessageBox.Question)
        box1.setWindowTitle('Information')
        box1.setText(
            "Do you want to go to the next Tab?")
        box1.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        buttonY = box1.button(QMessageBox.Yes)
        buttonY.setText('Yes') ##translation
        buttonN = box1.button(QMessageBox.No)
        buttonN.setText('No') ## translation
        box1.exec_()

        if box1.clickedButton() == buttonN:
            pass

        elif box1.clickedButton() == buttonY:
            self.tabWidget.setCurrentIndex(1)

def main():
    app = QApplication(sys.argv)
    ex = main_window()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

试一试:

import sys
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
from PyQt5.QtWidgets import *

class main_window(QTabWidget):
    def __init__(self, parent=None):
        super(QTabWidget, self).__init__(parent)
        self.setGeometry(50, 50, 1100, 750)
        self.setWindowTitle("Programm")  #


        self.centralWidget = QtWidgets.QWidget()
        self.tabWidget = QtWidgets.QTabWidget(self.centralWidget)
        self.tabWidget.setGeometry(QtCore.QRect(10, 10, 1200, 1000))
        self.tabWidget.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.tabWidget.setTabPosition(QtWidgets.QTabWidget.West)

        self.tab_v1 = QtWidgets.QWidget()
        self.addTab(self.tab_v1, "Tab 1")

        self.tab_v2 = QtWidgets.QWidget()
        self.addTab(self.tab_v2, "Tab 2")

        self.tab_v3 = QtWidgets.QWidget()
        self.addTab(self.tab_v3, "Tab 3")

        self.openFile = QPushButton("Choose Tab ", self.tab_v1)
        self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))
        self.openFile.clicked.connect(self.on_click_do)


    def on_click_do(self):

        box1 = QMessageBox()
        box1.setIcon(QMessageBox.Question)
        box1.setWindowTitle('Information')
        box1.setText(
            "Do you want to go to the next Tab?")
        box1.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        buttonY = box1.button(QMessageBox.Yes)
        buttonY.setText('Yes') ##translation
        buttonN = box1.button(QMessageBox.No)
        buttonN.setText('No') ## translation
        box1.exec_()

        if box1.clickedButton() == buttonN:
            pass

        elif box1.clickedButton() == buttonY:
            #self.tabWidget.setCurrentIndex(1)         # ---
            self.setCurrentIndex(1)                    # +++

def main():
    app = QApplication(sys.argv)
    ex = main_window()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()