开发者

pygtk read-only gtk.TextView (make it ignore mouse clicks)

开发者 https://www.devze.com 2023-02-09 06:34 出处:网络
I need a way to make a gtk.TextView ignore mouse clicks in a pygtk GUI. I have set the \'editable\' property to false to prent user input, but it still responts to mouse clicks.

I need a way to make a gtk.TextView ignore mouse clicks in a pygtk GUI.

I have set the 'editable' property to false to prent user input, but it still responts to mouse clicks.

This textview displays some output from other commands, so if the user clicks anywhere on it, it moves the cursor to the clicked location. I need to avoid that.

I need something similar to the set_pro开发者_如何学Pythonperty('sensitive', False) results, but without graying out the widget. It just needs to sit there and ignore all kinds of user input.

Anyone have any ideas how to accomplish this?

Thanks in advance.


What you did is better for your purposes. For future reference, though, if you actually wanted to just block clicking, you would want to connect the TextView to button-press-event like so:

tview.connect('button-press-event', tviewClicked)

and define the handler function so that it returns True:

def tviewClicked(widget,event):
    return True

Returning True from a handler function tells GTK not to pass it on to anything else, so the click never gets sent to the TextView. The user won't be able to click on it any more.

I know this is an old question, but maybe it'll help someone else who comes to this page.


textview.set_property('editable', False)
textview.set_property('cursor-visible', False)

If you even want to allow the user to select the text or right click and choose Copy... you should overwrite button-press-event as @Derek Redfern wrote.


Found the answer. For anyone who is interested, here it goes.

The truth is, there is no way to make it ignore the mouse clicks. What you can do when you want a read-only kind of text view, you set the 'editable' property to False. This ignores the keyboard input.

The other thing is when inserting text, you want to use the insert method rather than the insert_at_cursor method.

Sample

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')

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消