【问题标题】:Insert Text into a TextBuffer via a Function - python通过函数将文本插入 TextBuffer - python
【发布时间】:2012-03-05 13:19:54
【问题描述】:

我有以下问题:

如何在我的文本缓冲区中插入文本?

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'

感谢您的帮助!

问候

【问题讨论】:

    标签: python textview pygobject gtk-textbuffer


    【解决方案1】:

    当你说TextBufferCommandInput = MainWindow.TextViewCommandInput.get_buffer() 您在 MainWindow 中要求一个名为 TextViewCommandInput 的类属性。你没有类属性TextViewCommandInput,你有一个实例属性TextViewCommandInput。您需要将 MainWindow 的一个实例传递给 DrawCommandView 才能访问 TextViewCommandInput。

    【讨论】:

    • 感谢您的回答,但我是 python 新手 - 那么,如何将 MainWindow 的实例传递给 DrawCommandView?你的意思是这样的:Instance = MainWindow() Instance.TextViewCommandInput.get_buffer() 现在我没有收到任何错误,但 TextView 中没有任何内容
    • 是的,这就是我的意思——尽管在函数中创建实例,或者在其他地方创建实例并将其作为参数传递给函数并不重要。我对 gtkBuilder 不熟悉,所以我认为我在这方面帮不上什么忙。
    【解决方案2】:

    我相信你必须set_text() 而不是get_buffer()

    请参阅set_text() 文档。

    然后get_buffer() 可以检索到set_text() 插入的文本。

    这里有一些我一直在使用的方法来获得使用缓冲区的通用方式。

    def GetTextBuffer(self):
        self.text_buffer = self.text_edit.get_buffer()
        # self.text_edit is a Gtk.TextView() instance
        # to get the text buffer from TextView
        # buffer.get_text( start iterator, end iterator, bool )
        # the third argument set to True = Include hidden characters
        # third argument set to False = Don't include hidden characters
        # hidden characters would be visual formatting markup and such
        return self.text_buffer.get_text(
            self.text_buffer.get_start_iter(),
            self.text_buffer.get_end_iter(), 
            False)
    
    def SetTextBuffer(self, to_buffer):
        # to_buffer is user input from widgets, or default values set at run time.
        text_buffer = self.text_edit.get_buffer()
        text_buffer.set_text(to_buffer)
    

    【讨论】:

    • 这很有趣乔纳斯。我还没怎么用过stackoverflow,也不知道它有这么多细节。
    猜你喜欢
    • 1970-01-01
    • 2013-04-14
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多