Introduction /

I don’t know when to start. The novel began to set off a wave, it makes our daily life not boring, a lot of things we can not do in the novel can easily achieve.

Today we are going to make a novel reader, a reader that can display the words in your article every how many seconds, just like the timed reading on a mobile phone, isn’t it interesting? So let’s see how it works.

/ Concrete implementation /

Novel reader, of course, the interface is indispensable, let’s start to write interface.

1. Import the packages we need first

import time
from tkinter import messagebox
import tkinter as t
from tkinter import ttk
from tkinter import filedialog
from tkinter import simpledialog
Copy the code

2. Write the main interface

class gui: def __init__(self): Self.root = t.tk () self.root. Title (' novel reader V1.0') # Geometry ("700x700") # set the window size to self.root Self. Root. Wm_attributes (' - the topmost ', 1) # window set-top self. Root. Wm_minsize (# 140, 170) Settings window to minimize size self. Root. Wm_maxsize (1440, Self.root. resizable(width=False, height=True) Self. te=t. ext(self.root,width=60,height=40) # Text =' open file ',font =("宋体",10,'bold'),command=self.open_file) self.b = ttk.com bobox(self.root, Self. root,text=' empty contents ',command=self.clean) # Self. L1 = t.L Abel (self. Root, text = 'please select reading speed:') # label self. Cb [' values'] = (' please select -- -- -- -- -- ', 'all read', 'a second line', 'two seconds a line', 'custom') # set the contents of the drop-down list box Self.cb.bind ("<<ComboboxSelected>>",self.go) # bind the go function, Place (x=130,y=30) self.te.place(x= 130,y=30) self.te.place(x=30,y=60) self.te.place(x=30,y=60) self.te.place(x=30,y=60) self.cb.place(x=230,y=30) self.root.mainloop()Copy the code

3, write the code to open the file dialog box

def open_file(self): The self. The file = filedialog. Askopenfilename (title = 'open the file, filetypes = [(' text file', '*.txt), (' All Files',' * ')]) return the self. The fileCopy the code

This opens the file headed by the text file.

4. Select the open file to read

self.ff=open(self.file,'r', encoding='utf8')
aa=self.ff.read()
Copy the code

5. Remove all white space newlines from the contents of the file

self.ab=aa.replace('\n','').replace('\t','').strip()
Copy the code

6. Implement the function of each option in the drop-down list

If self. Cb. The get () = = 'please select -- -- -- -- -- : pass elif self. Cb. The get () = =' all read: if self. Ab: Self.te. insert('insert',self.ab) # insert content self.te.update() # update content else: self.ff.close() elif self.cb.get()==' line a second ': For y in range(len(self.ab)): # if self.ab: self.te.insert('insert',self.ab[y]) # if y%10==0 and y! Self.te. insert('insert','\n') # insert self.te.update() # update() else: Else: self.ff.close() # elif self.cb.get()==' one line in two seconds ': for y in range(len(self.ab)): if self.ab: self.te.insert('insert',self.ab[y]) if y%10==0 and y! =0: self.te.insert('insert','\n') self.te.update() else: time.sleep(0.2) else: self.te.update() Self.ff.close () elif self.cb.get()==' self ': self.ff.close() elif self.cb.get()==' self ': Res =simpledialog. askINTEGER (title=' please enter ',prompt=' several seconds to read a line :', initialValue =' ') for y in range(len(self.ab)): if self.ab: self.te.insert('insert',self.ab[y]) if y%10==0 and y! =0: self.te.insert('insert','\n') self.te.update() else: time.sleep(res/10) else: self.ff.close()Copy the code

This allows you to print one line every ten bytes every second, or you can print it word by word. If so, simply pass the following code:

For y in range(len(self.ab)): # if self.ab: self.te.insert('insert',self.ab[y]) # if y%10==0 and y! Self.te. insert('insert','\n') self.te.update() # else: time.sleep(0.1)Copy the code

To:

For y in range(len(self.ab)): # if self.ab: if y % 10==0 and y! Self.te. insert('insert','\n') else: self.te.insert('insert','\n') Self.te. update() # self.te.update() # self.te.update() # self.te.update() # self.te.update() # self.te.update() #Copy the code

8. Empty your content

Def clean(self): self.te.delete('1.0', t.endCopy the code

That’s how it works.

Let’s take a look at the specific effects:

This makes it easy to implement a novel reader. By the way, if you want to display more characters on a line, just change the following line:

if y % 10==0 and y! = 0:Copy the code

Change 10 to another number and it will display the corresponding length of characters.

Summary /

This paper, based on Python library, wrote a visual graphical interface, built a simple novel reader, to achieve a custom character size novel reader. That’s all for today’s sharing. Welcome to have a try.