Undo with GTK TextView

I'm trying to keep dependencies to a minimum for a program I contribute to, it's a small text editor.

GTK Textview doesn't seem to come with a built-in undo function. Is there any reference implementation I've been missing on so far? Is everyone writing their own undo function for their TextView widgets?

I'll be happy about any sample code - most happy about python sample code, as our project is in python.


Asked by: Catherine312 | Posted: 05-10-2021






Answer 1

as a follwow-up: I ported gtksourceview's undo mechanism to python: http://bitbucket.org/tiax/gtk-textbuffer-with-undo/

serves as a drop-in replacement for gtksourceview's undo

(OP here, but launchpad open-id doesn't work anymore)

Answered by: Thomas548 | Posted: 06-11-2021



Answer 2

As far as I know, GTK TextView doesn't include an undo function. So while I am not familiar with Python's GTK library, I would think it doesn't have one.

The Ruby-GNOME2 project has a sample text editor that has undo/redo functionality. Basically they are connecting to the insert_text and delete_range signals of the TextView widget and recording the events and associated data in a list.

Answered by: Steven794 | Posted: 06-11-2021



Answer 3

Depending on just how dependency-averse you are, and what kind of text editor you're building, GtkSourceView adds undo/redo among many other things. Very worth looking at if you want some of the other features it offers.

Answered by: Gianna608 | Posted: 06-11-2021



Answer 4

Use GtkSource

.

  • [Cmnd] + [Z] for undo (default)
  • [Cmnd] + [Shift] + [Z] for redo (default)
  • [Cmnd] + [Y] for redo (added manually)

example:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
gi.require_version('GtkSource', '3.0')
from gi.repository import GtkSource

import os


class TreeviewWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="TreeviewWindow")
        self.set_size_request(300, 300)
        self.connect("key-press-event", self._key_press_event)
        self.mainbox = Gtk.VBox(spacing=10)
        self.add(self.mainbox) 

        self.textbuffer = GtkSource.Buffer()
        textview = GtkSource.View(buffer=self.textbuffer)
        textview.set_editable(True)
        textview.set_cursor_visible(True)
        textview.set_show_line_numbers(True)
        self.mainbox.pack_start(textview, True, True, 0)
        self.show_all()  

    def _key_press_event(self, widget, event):
        keyval_name = Gdk.keyval_name(event.keyval)
        ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
        if ctrl and keyval_name == 'y':
            if self.textbuffer.can_redo():
                self.textbuffer.do_redo(self.textbuffer)
        
    def main(self):
        Gtk.main()
        
if __name__ == "__main__":
    base = TreeviewWindow()
    base.main()
    

Answered by: Lana442 | Posted: 06-11-2021



Similar questions

python - Move cursor upon right click in TextView?

At the moment, when one right clicks in a TextView, a popup menu is brought up, but the cursor doesn't actually change position to where one is right clicking, it just leaves the cursor alone. For me, whom is trying to implement a spell checking menu, this isn't good since I have to click THEN right click in order to get the cursor in the right spot. So, my question is if there is any way to modify this behavior somehow so...


python - Why does this pygtk TextView refuse to wrap?

I have a textview inside a scrolledwindow that refuses to wrap to words/chars/wordschars no matter how I set the wrap mode. It simply extends the size of itself and its containers as it pleases. Here's an example: import gtk window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_default_size(256,256) scroll = gtk.ScrolledWindow() scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_ALWAYS) view = gtk.TextView() vie...


python - pygtk textview tab width?

Is there a way to set the tab width of the pygtk TextView widget to be something other than 8spaces? Right now I tried textview.set_tabs(pango.TabArray(4, False)) However, it seems like I misunderstood the docs. What the actual way to do it? Thanks


Python Gtk TextView insert text at end

I want to insert text to a textbuffer at the end... Im using this: gtk.TextBuffer.insert_at_cursor But if I clicking into the text there the new appears at the cursor... How can i add text at the end?


python - GTK3+ TextView add lines of text with different colors per line

I'm slightly stumped right now, i use a TextView as a status log, new lines added whenever something happens, this is the function i use for that, probably not right but it does work however i can't make it add changing the color of the entire line. def logtostatus(self, text): tvLog = self.tabs['Status'][1].get_children()[0].get_children()[0] buf = tvLog.get_buffer() start = buf.get_end_iter() ...


python - Drag and drop gtk textview

I would like to know if i can stop gtk textview from writing the data after a drop event. I have connected drag-data-recieved signal but I can't stop the textview from writing the data even if it returns true. Thanks.


python - How to modify a part of a text of a gtk textview

I learn GTK with Python and I've a problem with the widget texview. I'd like to changed / modify specific part of a text in a buffer (textview). For example, the text buffer: long_text = "line 1 word 1 word 2 word 3 \n" \ "line 2 [WORD TO REPLACE] \n" \ "line 3 \n" I 'd to replace the section...


python - Move cursor upon right click in TextView?

At the moment, when one right clicks in a TextView, a popup menu is brought up, but the cursor doesn't actually change position to where one is right clicking, it just leaves the cursor alone. For me, whom is trying to implement a spell checking menu, this isn't good since I have to click THEN right click in order to get the cursor in the right spot. So, my question is if there is any way to modify this behavior somehow so...


python - PyGTK TextView that only accepts single lines

I feel rather silly to ask this, but how does one create a TextView that only accepts single lines? Most other gui toolkits have a method such as "setMultiline" etc, but PyGTK seems to really enjoy multiple lines. Some other languages have "TextFields" for these simpler text boxes. http://www.pygtk....


python - Why does this pygtk TextView refuse to wrap?

I have a textview inside a scrolledwindow that refuses to wrap to words/chars/wordschars no matter how I set the wrap mode. It simply extends the size of itself and its containers as it pleases. Here's an example: import gtk window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.set_default_size(256,256) scroll = gtk.ScrolledWindow() scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_ALWAYS) view = gtk.TextView() vie...


python - pygtk textview tab width?

Is there a way to set the tab width of the pygtk TextView widget to be something other than 8spaces? Right now I tried textview.set_tabs(pango.TabArray(4, False)) However, it seems like I misunderstood the docs. What the actual way to do it? Thanks


python - How to modify a textview from a different process ?

So I am making a GUI to get tweets. I have made an event box which would take the signal and change the textview. I am using multiprocessing to change the textview, but it just doesn't change. I even tried changing the size of the window. But nothing changes. I can get textbuffer of the textview but can not change it. import pygtk pygtk.require('2.0') import gtk from multiprocessing import Process class mu...


ubuntu - Python, Quickly and Glade, showing stdout in a TextView

I've spent ages looking for a way to do this, and I've so far come up with nothing. :( I'm trying to make a GUI for a little CLI program that I've made - so I thought using Ubuntu's "Quickly" would be the easiest way. Basically it appears to use Glade for making the GUI. I know that I need to run my CLI backend in a subprocess and then send the stdout and stderr to a textview. But I can't figure out how to do this...


Python Gtk TextView insert text at end

I want to insert text to a textbuffer at the end... Im using this: gtk.TextBuffer.insert_at_cursor But if I clicking into the text there the new appears at the cursor... How can i add text at the end?


Wait for textview to finish in python

I have a script that displays data in a textview box. It has multiple points that it updates the textview box. My problem is that the textview box only updates after the sctipt has finshed. So how do I write text to the textview box then wait for that text to show then continue. Thanks for the help. self.textbuffer.insert_at_cursor("Keyword : " + var + "\n") #.... #....Do some code here but wait for above...


python - GTK: How can the size of a TextView be set manually?

I am reworking a GUI from scratch using Glade + Python. It was previously done in Tkinter and I could set the height and width of several consoles. Now, in GTK (newbie) I can't find a proper solution to this: specify a size to the TextView. I imagine it is done by nesting it in a block that sets the size. What's my best option? Here's my case: http://pas...


Python - Android logcat output of stdout to textview then save to .txt file error

I have the following Python code, it uses Android logcat via stdout to pull the logs off the phone into a wxPython Text field. I login to my app, username jack and password Jack@123$ the logcat output stops with an error, it seems to not like the @ or $ symbols: return codecs.charmap_decode(input,errors,decoding_table) UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 76: character...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top