我正在使用 QTabWidget 进行一些后处理。最初,选项卡的内容是打开 txt 文件的按钮。单击此按钮后,选项卡的内容会发生变化,并且会根据 txt 文件中的数据进行绘图。此外,还有一个关闭绘图的按钮。
使用QStackedWidget,“openFileWidget”是一个CentralWidget,打开txt文件后被“newWindow”小部件代替。如果我关闭“newWindow”小部件,我会返回(空)选项卡。如果我想让“openFileWidget”重新出现,最好的方法是什么?由于 newWindow 已关闭,还需要使用takeAt
吗?
import os, sys
from PySide import QtCore, QtGui
class MainStartTabWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MainStartTabWindow, self).__init__(parent)
tabWidget = QtGui.QTabWidget()
tabWidget.addTab(tab1Tab(), self.tr("tab 1"))
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(tabWidget)
self.setLayout(mainLayout)
class tab1Tab(QtGui.QMainWindow):
def __init__(self, parent=None):
super(tab1Tab, self).__init__(parent)
self.initTab1Content()
def initTab1Content(self):
self.centralOpenFile = QtGui.QStackedWidget()
self.setCentralWidget(self.centralOpenFile)
widgetOpenFile = openFileWidget(self)
widgetOpenFile.openFileButton.clicked.connect(self.setOpenFile)
widgetOpenFile.openFileButton.clicked.connect(self.FileOpened)
self.centralOpenFile.addWidget(widgetOpenFile)
def FileOpened(self):
FileOpened_widget = newWindow(self.path)
self.centralOpenFile.addWidget(FileOpened_widget)
self.centralOpenFile.setCurrentWidget(FileOpened_widget)
def setOpenFile(self):
options = QtGui.QFileDialog.Options()
fileChoice = "txt (*.txt)"
self.path, filtr = QtGui.QFileDialog.getOpenFileName(self, "Open file", "", fileChoice, "", options)
return self.path
class openFileWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(openFileWidget, self).__init__(parent)
self.openFileButton = QtGui.QPushButton("click to open")
self.openFileButton.setFixedSize(150,150)
layoutNoFileYet = QtGui.QGridLayout()
layoutNoFileYet.addWidget(self.openFileButton, 1, 1)
self.setLayout(layoutNoFileYet)
class newWindow(QtGui.QMainWindow):
def __init__(self, PathToPlanFile, parent=None):
super(newWindow, self).__init__(parent)
self.createFrameUI()
def createFrameUI(self):
self.frameUI = QtGui.QWidget()
self.buttonClose = QtGui.QPushButton("close")
self.buttonClose.clicked.connect(self.close)
self.label = QtGui.QLabel("plot based on openend file")
layoutFileLoaded = QtGui.QGridLayout()
layoutFileLoaded.addWidget(self.buttonClose, 1, 1)
layoutFileLoaded.addWidget(self.label, 2, 1)
self.frameUI.setLayout(layoutFileLoaded)
self.setCentralWidget(self.frameUI)
def main():
app = QtGui.QApplication(sys.argv)
testPlot = MainStartTabWindow()
testPlot.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()