Python-按下按钮显示下一个选项卡



如何通过按下按钮来更改显示的Tab键?按下信息框中的按钮即可调用该函数。我已经尝试过设置CurrentIndex((,但是显示的Tab键不会改变。

我使用的是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()

最新更新