0

次の問題があります:

テキストバッファにテキストを挿入するにはどうすればよいですか?

Interface.py

class MainWindow:
    def __init__(self):

        # Build our Interface from the XML/Glade file
        gladefile = "MainWindow.glade"
        try:
            self.builder = Gtk.Builder()
            self.builder.add_from_file(gladefile)
        except:
            print("Failed to load Glade file: %s" % gladefile)

        # Connect signals
        self.builder.connect_signals(self)

        # Get the widgets
        self.window = self.builder.get_object("MainWindow")

        ...

        # TextViews
        self.TextViewCommandInput = self.builder.get_object("TextViewCommandInput")
        self.TextViewCommandOutput = self.builder.get_object("TextViewCommandOutput")

        ...

def DrawCommandView(output):
    TextBufferCommandInput = MainWindow.TextViewCommandInput.get_buffer()
    TextBufferCommandInput.insert_at_cursor(output + "\n")

そして、「DrawCommandView」をファイルにインポートします

Commands.py

from Interface import MainWindow, DrawCommandView

output = "Hello World"
DrawCommandView(output)

if __name__ == "__main__":
    StartMainWindow = MainWindow()
    StartMainWindow.main()

しかし、私はこのエラーを受け取り続けます:

Traceback (most recent call last):
  File "/home/user/Dokumente/Workspace/project/Commands.py", line 5, in <module>
    DrawACommandView(output)
  File "/home/user/Dokumente/Workspace/project/Interface.py", line 182, in DrawCommandView

    TextBufferCommandInput = MainWindow.TextViewCommandInput.get_buffer()
AttributeError: class MainWindow has no attribute 'self'

ご協力いただきありがとうございます!

挨拶

4

2 に答える 2

0

あなたが言うときTextBufferCommandInput = MainWindow.TextViewCommandInput.get_buffer() 、TextViewCommandInputという名前のMainWindowでクラス属性を求めています。クラス属性 TextViewCommandInput がなく、インスタンス属性 TextViewCommandInput があります。TextViewCommandInput を取得するには、MainWindow のインスタンスを DrawCommandView に渡す必要があります。

于 2012-03-05T14:30:52.400 に答える