Python Ebooks maker – 4 – save files

Today we are going to add a button to the GUI to save the text we changed in the files that we could just read previously. It is a very basic and rough way to do it, but it’s a start that we are going to make better in the next versions of this app to read and write ebook made by us with the app itself. Let’s see the changes in the code and go in the previous posts if you did not read them and you want to know how we get here.

A button to save the files

We are going to put, right before the listbox – in the __init__ method of the class Ebook, with the file names on the left (the menu), a button with this text: “Save”, to save the change you made in the files.

		self.button = tk.Button(self.frame1, text="Save", command = self.save)
		self.button.pack()

Here it is:

The save button

The save method

This button, on click, calls the save method in the Ebook class:

	def save(self):
		with open(self.filename, "w") as file:
			file.write(self.text.get("1.0", tk.END))

As you can see we have the self.filename attribute. We created it into the show_text function substituting the local variable filename with self.filename, so that it is now avaiable in all the class.

			self.filename = self.lstb.get(index)
			with open(self.filename) as file:

Another little change in the show_text method it to make run its code only to this condition:

		if not self.lstb.curselection() is ():

I needed to put this, because when you select something in the text editor, the app loses the selection in the listbox, so that, due to the bind to selection in the listbox, the index = self.lstb.curselection()[0] gives an error, even if the program does not crash, that is annoying. So, with that condition, if the listbox is not selected, it does nothing, not causing any error.

The whole code to save files

So, it is just with a couple of lines of code and some adjustment that now we are finally able to change our files that are present in the folder lezioni (or whatever name you choose for it).

import tkinter as tk
import glob


class Ebook:
	def __init__(self, root):
		"""Define window for the app"""
		self.root = root
		self.root.geometry("600x400")
		self.root["bg"] = "coral"
		self.menu()
		self.editor()

	# Widgets on the left ===============|
	def menu(self):
		"""Listbox on the left with file names"""
		self.frame1 = tk.Frame(self.root)
		self.frame1["bg"] = "coral"
		self.frame1.pack(side='left', fill=tk.Y)
		self.button = tk.Button(self.frame1, text="Save", command = self.save)
		self.button.pack()
		self.lstb = tk.Listbox(self.frame1, width=30)
		self.lstb['bg'] = "black"
		self.lstb['fg'] = 'gold'
		self.lstb.pack(fill=tk.Y, expand=1)
		# ========= insert files name ==========
		files = glob.glob("lezioni\\*.txt")
		print(files)
		for file in files:
			self.lstb.insert(tk.END, file)
		# ===== show text on select ============
		self.lstb.bind("<<ListboxSelect>>", lambda x: self.show_text_in_editor())

	def save(self):
		with open(self.filename, "w") as file:
			file.write(self.text.get("1.0", tk.END))


	def show_text_in_editor(self):
		"""Shows text of selected file in the editor"""
		if not self.lstb.curselection() is ():
			index = self.lstb.curselection()[0]
			self.filename = self.lstb.get(index)
			with open(self.filename) as file:
				content = file.read()
			self.text.delete("1.0", tk.END)
			self.text.insert(tk.END, content)

	def editor(self):
		"""The text where you can write"""
		self.text = tk.Text(self.root)
		self.text['bg'] = "darkgreen"
		self.text['fg'] = 'white'
		self.text['font'] = "Arial 24"
		self.text.pack(fill=tk.Y, expand=1)


if __name__ == "__main__":
	root = tk.Tk()
	app = Ebook(root)
	app.root.title("Lezioni")
	root.mainloop()

Live coding video

The live coding video shows you the save method adding.

What next?

Now we need to create new files from the app, even if we can do it manually saving them in the folder, but it is annoying, so… see ya in the next post.

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.