Python ebook maker 3

I am trying to make an app to create ebooks with Python. This app needs to be simple, but efficient. I am gonna recap what I have done until now, remaking all the code from scratch to the point where the user can read all the text files stored in a folder. The next step will be to be able to save the modifications made to the files in the text editor inside the app iteself.

The code, redone, is this:

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.left_menu()
		self.editor()

	# Widgets on the left ===============|
	def left_menu(self):
		"""List of files in the folder"""
		self.frame1 = tk.Frame(self.root)
		self.frame1["bg"] = "coral"
		self.frame1.pack(side='left', fill=tk.Y)
		self.lstb = tk.Listbox(self.frame1, width=30)
		self.lstb['bg'] = "black"
		self.lstb['fg'] = 'gold'
		self.lstb.pack(fill=tk.Y, expand=1)
		self.insert_files()
		self.lstb.bind("<<ListboxSelect>>", lambda x: self.show_text())

	def editor(self):
		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)


	def insert_files(self):
		"""Insert file names in the list on the left"""
		files = glob.glob("data\\*.txt")
		print(files)
		for file in files:
			self.lstb.insert(tk.END, file)

	def show_text(self):
		"""Shows text of selected file"""
		noi = self.lstb.curselection()[0]
		filename = self.lstb.get(noi)
		fn = self.opentext(filename)
		self.text.delete("1.0", tk.END)
		self.text.insert(tk.END, fn)

	def opentext(self, fname):
		with open(fname) as file:
			file = file.read()
		return file




root = tk.Tk()
app = Ebook(root)
app.root.title("Ebook maker")
root.mainloop()

I divided the class Ebook (previosly called Notepad) in more methods, so that is easier to find the code for the single widgets. Until now the widgets are just two:

  • the listbox with the file list on the left
  • the text widgets that show the text of the selected files.

The listbox (lstb) is in the frame called frame1. The text is ‘in’ the root.

That is. Very simple. See the fast live code below:

Before we go on… let’s simplify the code

A little code adjustment to make it more “simple” to read.

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.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 show_text_in_editor(self):
		"""Shows text of selected file in the editor"""
		index = self.lstb.curselection()[0]
		filename = self.lstb.get(index)
		with open(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()

I think that now the code is better, because there are just three methods in the Ebook class:

  • menu: place the listbox on the left, insert the file names and bind the action to show thr text
  • editor: place the text editor to the right
  • show_text_in_editor: the action to show the text linked to the menu method

I included some methods into the menu method, because they contained code that belonged to the listbox.

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.