我正在使用QTabWidget进行一些后期处理。 最初,选项卡的内容是用于打开txt文件的按钮。单击此按钮后,选项卡的内容会更改,并根据txt文件中的数据绘制图表。此外,还有一个关闭情节的按钮。
使用QStackedWidget,“openFileWidget”是一个CentralWidget,在打开txt文件后被“newWindow”小部件取代。如果我关闭“newWindow”小部件,我会回到(空)标签。
如果我想重新出现“openFileWidget”,最好的方法是什么?当新窗口关闭时,是否仍然需要使用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()
答案 0 :(得分:0)
我会完全改变你的做法。你只想要一个QMainWindow。不需要openFileWidget;它只是一个按钮。接下来我要做的就是创建一个自定义QTabWidget来编辑addTab来打开文件。
import os, sys
from PySide import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.main_widget = QtGui.QTabWidget()
self.setCentralWidget(self.main_widget)
self.main_widget.addTab(Page1(), "Tab 1")
self.main_widget.addTab(Page2(), "Tab 2")
self.main_widget.addTab(Page3(), "Tab 3")
self.main_widget.addTab(Page4(), "Tab 4")
self.main_widget.setCurrentIndex(0)
# end Constructor
# end MainWindow
class Page1(QtGui.QWidget):
"""First page to display."""
def __init__(self):
super().__init__()
self.main_layout = QtGui.QVBoxLayout()
self.setLayout(self.main_layout)
# Widgets
self.hlayout = QtGui.QHBoxLayout()
self.hlayout.setContentsMargins(0, 0, 0, 0)
self.read_btn = QtGui.QPushButton("Open File")
self.clear_btn = QtGui.QPushButton("Clear Data")
self.hlayout.addWidget(self.read_btn)
self.hlayout.addWidget(self.clear_btn)
# Display data here
self.display = DisplayWidget("plot based on openend file")
# Properties
self.data = None
# Signals
self.read_btn.clicked.connect(self.read_file)
self.clear_btn.clicked.connect(self.display.clearData)
# Layout
self.main_layout.addLayout(self.hlayout)
self.main_layout.addWidget(self.display)
# end Constructor
def read_file(self, path=""):
"""Read file dialog."""
if path == "":
options = QtGui.QFileDialog.Options()
fileChoice = "txt (*.txt)"
path, _ = QtGui.QFileDialog.getOpenFileName(self, "Open file", "",
fileChoice, "", options)
# Cancel button was pressed
if path == "":
return
with open(path, "r") as file:
lines = file.readlines()
# close file
self.display.setData(lines)
# end read_file
def set_data(self, data=None):
"""Set the data to display here."""
# end set_data
# end class Page1
class DisplayWidget(QtGui.QWidget):
def __init__(self, text=""):
super().__init__()
# Layout
self.main_layout = QtGui.QVBoxLayout()
self.setLayout(self.main_layout)
# Widgets Have all of your display stuff here
self.label = QtGui.QLabel(text)
# Properties
self.data = None
# Add Layout
self.main_layout.addWidget(self.label)
# end Constructor
def setData(self, data):
"""Set the data to show the plot."""
self.data = data
# Plot stuff here
self.label.setText(str(data))
# end setValue
def clearData(self):
"""Clear the plot data."""
self.data = None
# Clear data here
self.label.setText("Open a file to display data")
# end clearData
# end class DisplayWidget
class Page2(QtGui.QWidget):
pass
# end class Page2
class Page3(QtGui.QWidget):
pass
# end class Page3
class Page4(QtGui.QWidget):
pass
# end class Page4
def main():
app = QtGui.QApplication(sys.argv)
testPlot = MainWindow()
testPlot.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
我不会让用户关闭标签。只要他们愿意就可以清除并重新加载数据。如果你真的希望它们关闭,那么让clear data按钮关闭displaywidget并让read文件按钮每次都创建一个新的widget。我不认为你每次都需要创建一个新的小部件。如果您不喜欢这些标签,您可能需要查看QToolBox。