1

私がやろうとしているのは、Controllerクラスのonopen関数で、リストボックスを更新するViewクラスでupdate_listbox関数を実行しようとしています。これにより、ビューインスタンスを最初の引数としてupdate_listbox()を呼び出す必要があるというエラーが発生します。私は自分が何をしているのか完全には理解していないので、誰かが私がここで間違っていることとそれを正しく行う方法を私に説明してくれると非常に役立ちます。

乾杯tchadwik

from Tkinter import *
import tkMessageBox
import tkFileDialog
from tkFileDialog import askopenfilename
from tkMessageBox import askokcancel

class Controller(object):
    def __init__(self, master=None):

        self._master = master
        #filemenubar
        self.menu()
        #listbox
        self.listboxFrame = View(master)
        self.listboxFrame.pack(fill=BOTH, expand=YES)
        #entry widget
        self.EntryFrame = Other(master)
        self.EntryFrame.pack(fill = X)

    def menu(self):

        menubar = Menu(self._master)
        self._master.config(menu=menubar)

        fileMenubar = Menu(menubar)
        fileMenubar.add_command(label="Open Products File", command=self.onopen)
        fileMenubar.add_command(label="Save Products File", command=self.onsave)
        fileMenubar.add_command(label="exit", command=self.onExit)
        menubar.add_cascade(label="File", menu=fileMenubar)

    def onopen(self):
        fname = askopenfilename()
        products.load_items(fname)
        View.update_listbox() #
        #this gives me error stating that it needs View instance as first argument
        #and adding self here only gives it the controller instance

class View(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.list = Listbox(self, selectmode=EXTENDED)
        self.list.pack(fill=BOTH, expand=YES)
        self.current = None
        self.update_listbox()

    def update_listbox(self):   
        temp=products.get_keys()
        for i in temp:
            self.list.insert(END, str(products.get_item(i))) 
4

1 に答える 1

3

オブジェクトをインスタンス化する必要がありViewます。例えば:

def onopen(self):
    fname = askopenfilename()
    products.load_items(fname)

    myview = View(self._master)   # Instantiate the object
    myview.update_listbox()       # Now call update_listbox()

これは、メンバー変数 (例: self.list) のメモリが、オブジェクトがインスタンス化されるまで割り当てられないためです。または、クラス定義からオブジェクトを作成するときに発生する、が呼び出されるself.listまで作成されないという別の言い方もあります。View.__init__()ViewView

于 2012-10-10T05:07:30.390 に答える