簡體   English   中英

在python中從一個類調用到另一個類

[英]call from a class to another class in python

我是python的新手,我正在嘗試執行以下操作:我有兩個類,一個用於構建GUI表單,另一個用於處理請求。 提交按鈕位於GUI表單上,並且handle操作位於handle類中。

因此,我嘗試從GUI類調用handle類,因此我做了如下操作:

class Appview:

    def __init__(self, master):

        master.title("Simple EXCEL")
        master.resizable(False, False)

        self.filename = ""
        self.input_file = ttk.Entry(self.frame_content, textvariable = self.filename, width = 24)
        self.input_file.grid(row = 2, column = 1, pady = 5, padx = 5)       
        ttk.Button(self.frame_content, text = "choose file", command = self.browse_file).grid(row = 2, column = 2)

        #ADDING#
        a = HandleData(self)
        ttk.Button(self.frame_content, text = "add", command = HandleData.submit).grid(row = 3, column = 0 ,columnspan = 3, pady = 5)

    """
    Choosing a file to browse
    """   
    def browse_file(self):
        self.filename = filedialog.askopenfilename(title = 'Choose a ffffile')
        self.input_file.insert(0, self.filename) #updating the file entry

這是Handle類:

class HandleData:

    def __init__(self):
        self.the_file = Appview.filename.get()

    def submit(self):
        messagebox.showinfo(title = "Okie Dokie", message = "well done: {}".format(self.the_file))

但我不斷收到此錯誤:

追溯(最近一次通話):
文件“ C:\\ Users \\ Amir \\ workspace \\ Python Workout \\ main.py”,第91行,如果__ name__ ==“ __ main__”:main()
主appview中的文件“ C:\\ Users \\ Amir \\ workspace \\ Python Workout \\ main.py”,第88行= Appview(root)
文件“ C:\\ Users \\ Amir \\ workspace \\ Python Workout \\ main.py”,第60行,位於__ init__ a = HandleData(self)中
TypeError:__ init __()接受1個位置參數,但給出了2個

有什么想法嗎?

當你做-

a = HandleData(self)

您試圖創建HandleData的實例(對象),當創建實例時,調用__init__() (在創建對象之后),第一個參數作為創建的實例本身,因此當您發送self ,它會成為__init__()的第二個參數,但是HandleData __init__()僅接受一個參數,因此會發生此問題。

您可以使HandleData接受第二個參數,可以是AppView,然后在HandleData.submit ,可以從該對象獲取文件名。 范例-

處理數據-

class HandleData:

    def __init__(self, appview):
        self.appview = appview

    def submit(self):
        filename = self.appview.filename
        messagebox.showinfo(title = "Okie Dokie", message = "well done: {}".format(filename))

這將從AppView的實例變量filename獲取filename的數據,如果要從Entry input_file獲取數據,則可以使用-

filename = appview.input_file.get()

為什么需要對HandleData submit()HandleData進行更改,是因為在執行__init__()本身時我們無法獲取文件名,因為您在AppView.__init__()HandleData創建了對象,因此您直到那時都沒有選擇任何文件名。 因此,您可以將appview對象保存在appview中, self.appview單擊“ submit按鈕時,您將訪問設置為它的filename屬性並執行邏輯。

AppView類的更改-

self.datahandler = HandleData(self)
ttk.Button(self.frame_content, text = "add", command = self.datahandler.submit).grid(row = 3, column = 0 ,columnspan = 3, pady = 5)
a = HandleData(self)

看看這個,你通過自己在傳遞什么? 您在HandleData的引用中。但是HandleData需要它所屬的類的引用,並且它會自動獲取它。

因此,它基本上得到2個參數,應該得到1個。

嘗試:

a = HandleData()

您需要在__init__函數中使用多余的參數來容納第二個類:

def __init__(self, other_class):
    self.the_file = other_class.filename.get()

然后您可以像調用它一樣調用它:

a = HandleData(self)

因為在這種情況下, self引用了AppView類。 HandleData類的__init__函數中時,它引用HandleData類。

查看有關類的文檔

暫無
暫無

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

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