You can get the contents of a TextBuffer using get_text():
start_iter = self.textbuffer.get_start_iter()
end_iter = self.textbuffer.get_end_iter()
text = self.textbuffer.get_text(start_iter, end_iter, True)
And save it like any other text file:
with open(save_file, 'w') as f:
f.write(text)
I suggest that you take a look at the TextBuffer api docs. As well as the Python GTK+ 3 Tutorial. It can show you how to use the FileChooserDialog.
Here is a very basic Python Gtk+3 text editor example that can open and save a text file:
#! /bin/python
from gi.repository import Gtk
class TextViewWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="TextView Example")
self.set_default_size(500, 350)
self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(self.box)
toolbar = Gtk.Toolbar()
open_btn = Gtk.ToolButton.new_from_stock(Gtk.STOCK_OPEN)
open_btn.connect("clicked", self.on_open_clicked)
toolbar.insert(open_btn, 0)
save_btn = Gtk.ToolButton.new_from_stock(Gtk.STOCK_SAVE)
save_btn.connect("clicked", self.on_save_clicked)
toolbar.insert(save_btn, 1)
self.box.pack_start(toolbar, False, True, 0)
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.textview = Gtk.TextView()
self.textbuffer = self.textview.get_buffer()
scrolledwindow.add(self.textview)
self.box.pack_start(scrolledwindow, True, True, 0)
def on_open_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose a file", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
filter_text = Gtk.FileFilter()
filter_text.set_name("Text files")
filter_text.add_mime_type("text/plain")
dialog.add_filter(filter_text)
response = dialog.run()
if response == Gtk.ResponseType.OK:
selected_file = dialog.get_filename()
with open(selected_file, 'r') as f:
data = f.read()
self.textbuffer.set_text(data)
elif response == Gtk.ResponseType.CANCEL:
dialog.destroy()
dialog.destroy()
def on_save_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Save file", self,
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
response = dialog.run()
if response == Gtk.ResponseType.OK:
save_file = dialog.get_filename()
start_iter = self.textbuffer.get_start_iter()
end_iter = self.textbuffer.get_end_iter()
text = self.textbuffer.get_text(start_iter, end_iter, True)
with open(save_file, 'w') as f:
f.write(text)
elif response == Gtk.ResponseType.CANCEL:
dialog.destroy()
dialog.destroy()
win = TextViewWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
I've found the solution, I'm using GTK+ 3.22 for reference, just need to turn on this setting:
gsettings set org.gtk.Settings.Debug enable-inspector-keybinding true
And then the "Help" menu of gnome-terminal will show a "Inspector" item you just need to click it.
This is for the VTE widget only:
Alternatively, if you are using jhbuild, the vte module has a test program for the vte widget (that gnome-terminal uses) so you can run it like this:
# inside jhbuild/checkout/vte
GTK_DEBUG=interactive src/testvte
Update:
On 20.04, the "Inspector" link is in the gnome-terminal window's "hamburger‑menu‑button > Advanced" submenu.
Best Answer
I got this from https://wiki.gnome.org/Projects/GTK%2B/Inspector:
Looking further, I found Bug #1523929 which indicates that users can install
libgtk-3-dev
which then provides the schema. Note that installinglibgtk-3-dev
may bring in a lot of dependencies.