带有三个标签的简单对话框:'Tab_01','Tab_02'和'Tab_03'。 按“什么标签?”按钮应该打印当前活动的选项卡。 怎么实现呢? (示例代码见下文):
from PyQt4 import QtGui, QtCore
import sys, os
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
mainWidget=QtGui.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtGui.QVBoxLayout()
mainWidget.setLayout(mainLayout)
self.tabWidget = QtGui.QTabWidget()
mainLayout.addWidget(self.tabWidget)
self.tabWidget.connect(self.tabWidget, QtCore.SIGNAL("currentChanged(int)"), self.tabSelected)
myBoxLayout = QtGui.QVBoxLayout()
self.tabWidget.setLayout(myBoxLayout)
self.tabWidget.addTab(QtGui.QWidget(),'Tab_01')
self.tabWidget.addTab(QtGui.QWidget(),'Tab_02')
self.tabWidget.addTab(QtGui.QWidget(),'Tab_03')
ButtonBox = QtGui.QGroupBox()
ButtonsLayout = QtGui.QHBoxLayout()
ButtonBox.setLayout(ButtonsLayout)
Button_01 = QtGui.QPushButton("What Tab?")
ButtonsLayout.addWidget(Button_01)
Button_01.clicked.connect(self.whatTab)
mainLayout.addWidget(ButtonBox)
def tabSelected(self, arg=None):
print '\n\t tabSelected():', arg
def whatTab(self):
print '\n\t current Tab:', '?'
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
答案 0 :(得分:4)
正如您所看到的那样here QTabWidget有方法currentIndex
和currentWidget
。
答案 1 :(得分:3)
这是解决方案(感谢Hyperboreus!):
from PyQt4 import QtGui, QtCore
import sys, os
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
mainWidget=QtGui.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtGui.QVBoxLayout()
mainWidget.setLayout(mainLayout)
self.tabWidget = QtGui.QTabWidget()
mainLayout.addWidget(self.tabWidget)
self.tabWidget.connect(self.tabWidget, QtCore.SIGNAL("currentChanged(int)"), self.tabSelected)
myBoxLayout = QtGui.QVBoxLayout()
self.tabWidget.setLayout(myBoxLayout)
self.tabWidget.addTab(QtGui.QWidget(),'Tab_01')
self.tabWidget.addTab(QtGui.QWidget(),'Tab_02')
self.tabWidget.addTab(QtGui.QWidget(),'Tab_03')
ButtonBox = QtGui.QGroupBox()
ButtonsLayout = QtGui.QHBoxLayout()
ButtonBox.setLayout(ButtonsLayout)
Button_01 = QtGui.QPushButton("What Tab?")
ButtonsLayout.addWidget(Button_01)
Button_01.clicked.connect(self.whatTab)
mainLayout.addWidget(ButtonBox)
def tabSelected(self, arg=None):
print '\n\t tabSelected() current Tab index =', arg
def whatTab(self):
currentIndex=self.tabWidget.currentIndex()
currentWidget=self.tabWidget.currentWidget()
print '\n\t Query: current Tab index =', currentIndex
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())