簡體   English   中英

使用Kivy進行Python Cross Class函數調用

[英]Python Cross Class function call with Kivy

我想從cpopup類調用reflist()函數。 我不太確定該怎么做。 self.dismiss()行之后,我嘗試了很多事情。

narr.reflist()感覺應該可以,但是不能。 我想也許是因為我需要引用類的實例,而不是類本身,但是我不確定實例名稱是什么以及如何引用它。

narr().reflist()比較成功,因為它不會出錯。 但是結果出乎意料(什么也沒有發生)。 也許可以,但是我只是不了解類中的變量范圍。

execfile("drawinglist.py")
    dlists = []
    dsource = []

    class cpopup(Popup):
        filechooser = ObjectProperty(None)
        def on_press_dismiss(self, *args):
            self.dismiss()
            #return False

        def load(self, path, filename):
            fdid = self.id
            bodycon = ObjectProperty(None)
            if fdid == "Narr":
                print parseNarrative(filename)
                dsource.append(filename)
                dlists.append(parseNarrative(filename))
                self.dismiss()
                narr.reflist()
            if fdid == "Dir":
                print parseFolder(filename, True)
                self.dismiss()
            if fdid == "Dl":
                print parseDrawinglist(filename)
                self.dismiss()

    class narr(BoxLayout):
        bodycon = ObjectProperty(None)
        dlists  = ObjectProperty(None)
        def loadNarr(self):
            pop = cpopup()
            pop.title="Select a file to generate Drawing List from"
            pop.id = "Narr"
            pop.filechooser.multiselect = True
            pop.open()

        def loadDir(self):
            pop = cpopup()
            pop.title="Select a Directory to load drawing list from."
            pop.id = "Dir"
            pop.filechooser.dirselect = True
            pop.open()

        def loadDl(self):
            pop = cpopup()
            pop.title="Select a drawing list (.pdfs) to generate list from"
            pop.id = "Dl"
            pop.filechooser.multiselect = True
            pop.open()

        def reflist(self):
            words = ""
            source = ""
            index = []

            for d in dlists:
                for a in d:
                    dwg = str(a)
                    if dwg not in index:
                        index.append(dwg)

            for l in dsource:
                fl = l[0]
                fl = fl.split("/")
                fl = fl[-1]
                source = source + fl +"\n"

            self.dlists.text = source
            self.bodycon.text = words

    class narrApp(App):
        def build(self):
            return narr()

    if __name__=="__main__":
        narrApp().run()

很多方式,取決於用法:

  • 如果您的類彼此相等則通過App.get_running_app()在第二個類的實例中調用它,如下所示(僅適用於Kivy):

     class MyWidget(Widget): def __init__(self, *args, **kwargs): self.app = App.get_running_app() self.app.my_widget = self def func(self, *args): pass class MyAnotherWidget(Widget): # you'll need this piece in the class you want to # access func() from and you'll need App instance # present too def __init__(self, *args, **kwargs): self.app = App.get_running_app() self.app.my_widget.func() 
  • 主↔從屬類(始終在上面):

     class Slave: def func(self, *args): pass class Master: # creating of the slave instance is required def __init__(self, *args, **kwargs): self.slave_class = Slave() self.slave_class.master_class = self self.slave_class.func() 

    基本上這是如何App ↔一切關系的作品。 這里有一個主要的主類,您要做的就是將“從屬”類的其他引用放到已經創建的“ master”類實例中。

  • 靜態方法

     class Something: @staticmethod def static_func(*args): pass class SomethingElse: def __init__(self, *args, **kwargs): Something.static_func() 

    裝飾器允許您像這樣使用它, 但是您無法訪問靜態方法所屬的類中的任何內容 (沒有self ,沒有類樂趣)。 您肯定已經在這種形狀的某個包中遇到了它們: Package.do_something(*args) ,那么這可能是一個靜態方法。

  • 類方法

     class Something: def __init__(self, first=None, second=None, *args, **kwargs): print(first, second) @classmethod def class_func(cls, arg_list): special_Something = cls(arg_list[0], arg_list[1]) return special_Something class SomethingElse: def __init__(self, *args, **kwargs): new_something = Something.class_func(['hi', 'there']) 

    基本上,例如,如果您要傳遞用於創建類的特殊格式,但是該類具有不同的輸入參數,則可以執行此類的類方法,以使您想要與之一起使用的類的輸入有所了解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM