将数据从一个类传递到另一个类

时间:2016-01-23 15:48:35

标签: python oop gtk3

我知道这里有好几次被问过,相信我,我在这里发布问题之前已经读了很多。但我还没有解决我的问题。所以,任何帮助将不胜感激。

首先,这涉及4个不同类中的4个函数(在单独的文件中,使用import访问,并且这不是关注点)。

以下是涉及的函数的scriptlet:

MAIN计划

class MyWindow(Gtk.Window):
    def __init__(self):
        MenuElem = menu.MenuManager()
        TreeView = view.treeview()

        grid.attach(TreeView.view, 30, 0, 25, 21) #defined properly, 
                                                  #just not shown for bravity

MenuManager模块(显然在menu.py中)

import pybib
import view
class MenuManager(Gtk.UIManager):
    def __init__(self):
        self.parsing = pybib.parser()
        self.TreeView = view.treeview()

    def file_open_clicked(self, widget):
            filename = dialog.get_filename()
            self.parsing.parsing_read(filename)

            ####### HERE I NEED TO CALL VIEW.PY
            ## AND ATTACH IT TO THE GRID.ATTACH LINE
            ## iN MAIN.PY######
            ##e.g. self.Treeview()

pybib.parser是一个相当小的代码,这也应该将参数作为输入和输出,所以请允许我完整发布:

class parser():
    # def __init__(self):
        def parsing_read(self, filename):
            import bibtexparser
            from bibtexparser.bparser import BibTexParser
            # from bibtexparser.bwriter import BibTexWriter
            from bibtexparser.bibdatabase import BibDatabase
            db = BibDatabase()
            with open(filename) as bibtex_file:
                parser = BibTexParser()
                db = bibtexparser.load(bibtex_file, parser=parser)
                for i in range(0, len(db.entries)):
                    tuples = (i+1, db.entries[i].get("title"),
                              db.entries[i].get("author"),
                              db.entries[i].get("journal"),
                              db.entries[i].get("year"))
                    print(tuples)
                    #booklist.append(tuples) I should be able to pass the 
                    #booklist to view somehow

现在,最后, view.py

class treeview():
    def __init__(self):
        #booklist = [] should take the booklist created by pybib
        self.bookstore = Gtk.ListStore(int, str, str, str, str)
        for ref in booklist:
            self.bookstore.append(list(ref))
        self.current_filter_language = None
        self.view = Gtk.TreeView(model=self.bookstore)
        for i, column_title in enumerate(["Index", "Title", "Author",
                                          "Publishers", "Year"]):
            renderer = Gtk.CellRendererText()
            renderer.set_property("wrap-width", 400)
            if i > 2:
                renderer.set_property("wrap-width", 150)
            renderer.set_property("wrap-mode", 0)
            column = Gtk.TreeViewColumn(column_title, renderer, text=i)
            self.view.append_column(column)
        print("view working")

我已检查过各个代码是否正常工作,但无法正确传递数据。我保持小脚本尽可能小到我的理解。

我应该提一下,我在python中的经验仅限于脚本,在单个文件中,这是我第一次真正做oop

请帮助。

0 个答案:

没有答案