pygtk只读gtk.TextView(让它忽略鼠标点击)

M0E*_*lnx 2 python gtk pygtk

我需要一种方法来使一个gtk.TextView在pygtk GUI中忽略鼠标点击.

我已将'editable'属性设置为false以提供用户输入,但它仍然会响应鼠标单击.

此textview显示其他命令的一些输出,因此如果用户单击其中的任何位置,它会将光标移动到单击的位置.我需要避免这种情况.

我需要类似于set_property('sensitive',False)结果的东西,但不会使窗口小部件变灰.它只需要坐在那里忽略各种用户输入.

任何人有任何想法如何实现这一目标?

提前致谢.

Der*_*ern 8

你所做的更适合你的目的.但是,为了将来参考,如果你真的想阻止点击,你会想要连接TextViewbutton-press-event这样:

tview.connect('button-press-event', tviewClicked)
Run Code Online (Sandbox Code Playgroud)

并定义处理函数,以便它返回True:

def tviewClicked(widget,event):
    return True
Run Code Online (Sandbox Code Playgroud)

从处理函数返回True告诉GTK不要将其传递给其他任何东西,因此点击永远不会被发送到TextView.用户将无法再点击它.

我知道这是一个老问题,但也许它会帮助其他人来到这个页面.


M0E*_*lnx 5

找到了答案。对于任何有兴趣的人,这里都有。

事实是,没有办法让它忽略鼠标点击。当您想要只读类型的文本视图时,您可以将 'editable' 属性设置为 False。这将忽略键盘输入。

另一件事是在插入文本时,您想使用insert方法而不是insert_at_cursor方法。

样本

tview = gtk.TextView()
tview.set_property('editable', False)


# Insert text at the end on the textview.
buffer = tview.get_buffer()
buffer.insert(buffer.get_end_iter(), 'This text goes at the end of the existing text')
Run Code Online (Sandbox Code Playgroud)

HTH


sae*_*gnu 5

textview.set_property('editable', False)
textview.set_property('cursor-visible', False)
Run Code Online (Sandbox Code Playgroud)

如果您甚至想允许用户选择文本或右键单击并选择复制...您应该button-press-event像@Derek Redfern 写的那样覆盖。