myromance123
October 25th, 2012, 02:42 PM
Hi there.
Alright, so I am doing a very basic text editor (extremely basic) to allow me to learn and understand Gtk.FileChooserAction.SAVE.
My problem right now is that I do not know how to grab the filename the user puts into the FileChooser and set that as the files actual name when being saved. It currently defaults to "Untitled Document" and won't change. I believe this may be a problem in my logic but am unsure.
Here is my code to better bring clarity to the problem:
#!/usr/env/python
from gi.repository import Gtk
class TextWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Mini Writer")
self.set_size_request(200,200)
table = Gtk.Table(2, 1, True)
self.add(table)
textview = Gtk.TextView()
self.textbuffer = textview.get_buffer()
table.attach(textview, 0, 1, 0, 1)
button = Gtk.Button(label="Save As..")
button.connect("clicked", self.button_pressed)
table.attach(button, 0, 1, 1, 2, ypadding=5)
def add_filters(self, dialog):
filter_text = Gtk.FileFilter()
filter_text.set_name("Text Files")
filter_text.add_mime_type("text/plain")
dialog.add_filter(filter_text)
def button_pressed( self, button ):
print "Button pressed."
self.begin = self.textbuffer.get_start_iter()
self.end = self.textbuffer.get_end_iter()
print self.textbuffer.get_text(self.begin, self.end, False)
dialog = Gtk.FileChooserDialog("Save your text file", self,
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.ACCEPT))
dialog.set_default_size(800, 400)
self.add_filters(dialog)
response = dialog.run()
Gtk.FileChooser.set_do_overwrite_confirmation(dial og, True)
self.user_edited_new_document = True
if( self.user_edited_new_document ):
Gtk.FileChooser.set_current_name(dialog, "Untitled document")
else:
Gtk.FileChooser.set_filename(filename)
if response == Gtk.ResponseType.ACCEPT:
#char *filename
filename = Gtk.FileChooser.get_filename(dialog)
print filename
self.save_to_file(filename)
#g_free (filename)
dialog.destroy()
def save_to_file(self, filename):
#function to write the file to computer
file = open(filename, "w+")
file.write(self.textbuffer.get_text(self.begin, self.end, False))
file.close()
#since now it is no longer a new document, we tell it as so
self.user_edited_new_document = False
win = TextWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Right now this program opens up to a small window named Mini Writer. It has a basic Gtk.TextView() and a button to initiate saving.
Currently I can save anywhere I want, but it will always save to "Untitled Document". The save itself works, and the text is saved no problem. This is being done in IDLE 2.7 on Ubuntu 12.10.
Any help or guidance is greatly appreciated!
Alright, so I am doing a very basic text editor (extremely basic) to allow me to learn and understand Gtk.FileChooserAction.SAVE.
My problem right now is that I do not know how to grab the filename the user puts into the FileChooser and set that as the files actual name when being saved. It currently defaults to "Untitled Document" and won't change. I believe this may be a problem in my logic but am unsure.
Here is my code to better bring clarity to the problem:
#!/usr/env/python
from gi.repository import Gtk
class TextWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Mini Writer")
self.set_size_request(200,200)
table = Gtk.Table(2, 1, True)
self.add(table)
textview = Gtk.TextView()
self.textbuffer = textview.get_buffer()
table.attach(textview, 0, 1, 0, 1)
button = Gtk.Button(label="Save As..")
button.connect("clicked", self.button_pressed)
table.attach(button, 0, 1, 1, 2, ypadding=5)
def add_filters(self, dialog):
filter_text = Gtk.FileFilter()
filter_text.set_name("Text Files")
filter_text.add_mime_type("text/plain")
dialog.add_filter(filter_text)
def button_pressed( self, button ):
print "Button pressed."
self.begin = self.textbuffer.get_start_iter()
self.end = self.textbuffer.get_end_iter()
print self.textbuffer.get_text(self.begin, self.end, False)
dialog = Gtk.FileChooserDialog("Save your text file", self,
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.ACCEPT))
dialog.set_default_size(800, 400)
self.add_filters(dialog)
response = dialog.run()
Gtk.FileChooser.set_do_overwrite_confirmation(dial og, True)
self.user_edited_new_document = True
if( self.user_edited_new_document ):
Gtk.FileChooser.set_current_name(dialog, "Untitled document")
else:
Gtk.FileChooser.set_filename(filename)
if response == Gtk.ResponseType.ACCEPT:
#char *filename
filename = Gtk.FileChooser.get_filename(dialog)
print filename
self.save_to_file(filename)
#g_free (filename)
dialog.destroy()
def save_to_file(self, filename):
#function to write the file to computer
file = open(filename, "w+")
file.write(self.textbuffer.get_text(self.begin, self.end, False))
file.close()
#since now it is no longer a new document, we tell it as so
self.user_edited_new_document = False
win = TextWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Right now this program opens up to a small window named Mini Writer. It has a basic Gtk.TextView() and a button to initiate saving.
Currently I can save anywhere I want, but it will always save to "Untitled Document". The save itself works, and the text is saved no problem. This is being done in IDLE 2.7 on Ubuntu 12.10.
Any help or guidance is greatly appreciated!