我将问题分解为非常基本的设置。每个选项卡都允许用户单击一个按钮,然后将项目添加到列表中。同时将该项目添加到列表中,还将其添加到一个名为ALLITEMs的全局变量中。每个标签都被赋予了它的字典,如此处所示。

ALLITEMS = {
        "A" : [],
        "B" : []
    }


在“ closeEvent”的主窗口小部件中,我将此变量保存到json文件中。同样在主窗口小部件“ showEvent”中,我将此数据加载回全局可初始化的ALLITEMS中。这使用户每次启动应用程序时都能保留数据。我不确定的是如何刷新选项卡小部件以使用变量中的数据重新填充GUI。但仅当该工具最初启动时。我不希望用户每次单击该选项卡时都会加载它,这会产生重复的数据。

python - 从文本文件填充UI-Python-LMLPHP

import sys
import json
import os
from PySide import QtGui, QtCore

ALLITEMS = {
    "A" : [],
    "B" : []
}

# Widgets
# ------------------------------------------------------------------------------
class ExampleA(QtGui.QWidget):

    def __init__(self):
        super(ExampleA, self).__init__()
        self.initUI()

    def initUI(self):
        # formatting
        self.resize(550, 400)
        self.setWindowTitle("Tab A")

        # widgets
        self.ui_listview = QtGui.QListWidget()
        self.ui_add = QtGui.QPushButton("Add Item")

        # signals
        self.ui_add.clicked.connect(self.add_item_clicked)

        # main layout
        main_layout = QtGui.QVBoxLayout()
        main_layout.addWidget(self.ui_add)
        main_layout.addWidget(self.ui_listview)
        main_layout.setContentsMargins(0,0,0,0)
        self.setLayout(main_layout)

    def add_item_clicked(self):
        global ALLITEMS
        item = "Another item A"
        ALLITEMS["A"].append(item)
        self.ui_listview.addItem( item )


class ExampleB(QtGui.QWidget):

    def __init__(self):
        super(ExampleB, self).__init__()
        self.initUI()

    def initUI(self):
        # formatting
        self.resize(550, 400)
        self.setWindowTitle("Tab A")

        # widgets
        self.ui_listview = QtGui.QListWidget()
        self.ui_add = QtGui.QPushButton("Add Item")

        # signals
        self.ui_add.clicked.connect(self.add_item_clicked)

        # main layout
        main_layout = QtGui.QVBoxLayout()
        main_layout.addWidget(self.ui_add)
        main_layout.addWidget(self.ui_listview)
        main_layout.setContentsMargins(0,0,0,0)
        self.setLayout(main_layout)

    def add_item_clicked(self):
        global ALLITEMS
        item = "Another item B"
        ALLITEMS["B"].append(item)
        self.ui_listview.addItem( item )


class ExampleMain(QtGui.QWidget):

    def __init__(self):
        super(ExampleMain, self).__init__()
        self.initUI()

    def initUI(self):
        # formatting
        self.resize(200, 200)
        self.setWindowTitle("Test")

        # widgets
        tab_panel = QtGui.QTabWidget()
        tab_panel.addTab(ExampleA(), "Tab A")
        tab_panel.addTab(ExampleB(), "Tab B")

        # main layout
        main_layout = QtGui.QVBoxLayout()
        main_layout.addWidget(tab_panel)
        main_layout.setContentsMargins(0,0,0,0)
        self.setLayout(main_layout)

    def closeEvent(self, event):
        self.save_data()

    def showEvent(self, event):
        self.load_data()

    def save_data(self):
        print "Saving..."
        global ALLITEMS
        json.dump(ALLITEMS, open("Example_Data.json",'w'), indent=4)

    def load_data(self):
        global ALLITEMS
        if os.path.exists( "Example_Data.json" ):
            with open( "Example_Data.json" ) as f:
                data = json.load(f)
                ALLITEMS = data

# Main
# ------------------------------------------------------------------------------
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = ExampleMain()
    ex.show()
    sys.exit(app.exec_())

最佳答案

最简单(尽管不是很有效)的方法是每次更改ALLITEMS词典中的列表小部件。因此,在按下每个按钮时,您可以先更新ALLITEMS,然后清除列表小部件的内容,然后根据ALLITEMS [“ TabName”]的新值重新填充它。然后在显示事件中,您可以执行相同操作-从文件中加载ALLITEMS一次,然后更新列表小部件。

对于更有效的方法,请看一下Qt Model / View编程(http://doc.qt.io/qt-5/model-view-programming.html)。您可能需要用QListView替换QListWidget。

关于python - 从文本文件填充UI-Python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34232269/

10-12 16:20