Python ebooks maker 1

Let’s make an ebook… with Python!

We are going to make an app to create an… ebook. Well it will be a simple html file, but with a little of efforts it could really become an ebook. The app will be explained in 4 steps (and 4 posts), it will be made with a sort of live coding attitude and there will be many changes on the go. At the end the app will be rough but fully functional and I wish it can give you some ideas to bring the app to a step further making it awesome. Let’s start.

Toggle fullscreen? How?

As we are learning many stuffs about tkinter with the sqlite posts, why don’t we take a little break and take a look at some other features in a simple project that starts and ends in just one post? In this one we will make a little app with tkinter that will let me check all the content in the files in a folder with this simple layout that you can see in the image and video below:

Our little program with a window that toggles fullscreen with F11

Take a look at the output

In the following video you will see what we get:

This code is meant to let you go a little deeper with:

  • text widget,
    • delete the text, insert the text, make the widget adapt to the window,
  • create buttons on the go,
  • toggle tkinter window fullscreen,
  • and other interesting aspect like for loops, attributes of tkinter etc.

 

Full code

#!/usr/bin/env python
# coding: utf-8
# first day of coding: 6.9.19 for my post ob pythonprogramming

import tkinter as tk
from glob import glob


class Notepad:
    fullscreen = 1 # used by toggle_fullscreen
    def __init__(self, root):
        self.root = root
        self.root.title("My personal notepad")
        self.root.bind("<F11>", lambda x: self.toggle_fullscreen())
        self.root['bg'] = "coral"
        self.frame1 = tk.Frame(self.root)
        self.frame1['bg'] = "coral"
        self.frame1.pack(side="left")
        #    ---   as many buttons as many files   --- *** a comment by Giovanni G. 6.9.19
        for file in glob("*.py"):
            self.butnew(file, file)
        #    ---   the text that shows the text in the file selected   --- *** commented by G.G. 6.9.19
        self.text = tk.Text(self.root)
        self.text.pack(fill=tk.BOTH, expand=1)

    def toggle_fullscreen(self):
        """   ---   Makes the window in fullscreen or not with F11 key   --- """
        if Notepad.fullscreen:
            self.root.attributes("-fullscreen", True)
            Notepad.fullscreen = 0
        else:
            self.root.attributes("-fullscreen", False)
            Notepad.fullscreen = 1

    def butnew(self, text, file):
        tk.Button(self.frame1, text = text, command= lambda: self.show_code(file)).pack(side="top")
    
    def new_window(self, _class):
        self.new = tk.Toplevel(self.root)
        _class(self.new)

    def show_code(self, file):
        """Button clicked shows the text in the file"""
        with open(file) as f:
            f = f.read()
        self.text.delete('1.0', tk.END)
        self.text.insert(tk.END, f)

root = tk.Tk()  
win = Notepad(root)
root.mainloop()

A version with a list

In this version we have a list of file instead of buttons that is more handy when there are many files and you need to scroll the list of file names to see them all.

import tkinter as tk
from glob import glob

class Notepad:
	fullscreen = False
	def __init__(self, root):
		self.root = root
		self.root.geometry("600x400")
		self.root.title("My notepad")
		self.root['bg'] = "coral"
		self.frame1 = tk.Frame(self.root)
		self.frame1['bg'] = "coral"
		self.frame1.pack(side="left", fill=tk.Y)
		self.lb = tk.Listbox(self.frame1, width=30)
		self.lb.pack(fill=tk.Y, expand=1)
		for file in glob("*.py"):
			self.lb.insert(tk.END, file)
		self.text = tk.Text(self.root, width=70)
		self.text.pack(fill=tk.BOTH, expand=1)
		self.root.bind("<F11>", lambda x: self.toggle_fullscreen())
		self.lb.bind("<Double-Button>", lambda x: self.show_text())

	def show_text(self):
		num_item = self.lb.curselection() # the index of item selected
		fname = self.lb.get(num_item) # the file name selected
		with open(fname) as file:
			self.text.delete("1.0", tk.END)
			file = file.read()
			self.text.insert(tk.END, file)


	def toggle_fullscreen(self):
		if Notepad.fullscreen == False:
			self.root.attributes("-fullscreen", True)
			Notepad.fullscreen = True
		else:
			self.root.attributes("-fullscreen", False)
			Notepad.fullscreen = False






root = tk.Tk() # creates a window
app = Notepad(root)
root.mainloop()
The second version of the notepad

 

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.