Teach you how to make a simple novel reader with Python

/Foreword/

I don't know when it started. Novels have started to set off a wave. It makes our daily lives no longer boring, and many things we can't do can be easily achieved in novels.

Many people learn python and don't know where to start.
Many people learn python and after mastering the basic grammar, they don't know where to find cases to get started.
Many people who have done case studies do not know how to learn more advanced knowledge.
For these three types of people, I will provide you with a good learning platform, free to receive video tutorials, e-books, and course source code! ??¤
QQ group: 232030553

What we are going to do today is a novel reader, a reader that can display the words in your article every few seconds, just like regular reading on a mobile phone, isn’t it interesting? So let's take a look at how it is implemented in detail below.

/Implementation/

For a novel reader, of course the interface is indispensable, let's start writing the interface.

1. First import the packages we need to use

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

2. Write the main interface

class gui:
    def __init__(self):
        self.root=t.Tk()
        self.root.title('Fiction Reader V1.0') #Window name
        self.root.geometry("700x700") #Set the window size
        self.root.wm_attributes('-topmost',1) #Window on top
        self.root.wm_minsize(140, 170) # Set the minimum size of the window
        self.root.wm_maxsize(1440, 2800) # Set the maximum size of the window
        self.root.resizable(width=False, height=True) # Set the window width to be immutable and height to be variable
        self.te=t.Text(self.root,width=60,height=40) #Multi-line text box
        self.b1= t.Button(self.root, text='Open file',font =("Song Ti",10,'bold'),command=self.open_file)
        self.cb=ttk.Combobox(self.root, width=12) #drop-down list box
        self.b2=t.Button(self.root,text='clear content',command=self.clean) #button
        self.l1=t.Label(self.root,text='Please select the reading speed:') #label
        self.cb['values'] = ('Please select -----','read all','one row per second','one row for two seconds','custom') #Set the content of the drop-down list box   
        self.cb.current(0) #Set the current selection state to 0, which is the first item
        self.cb.bind("<<ComboboxSelected>>",self.go) #Binding the go function, and then trigger the event
        self.b1.place(x=30,y=30)
        self.b2.place(x=360,y=26)
        self.l1.place(x=130,y=30)
        self.te.place(x=30,y=60)
        self.cb.place(x=230,y=30)
        self.root.mainloop ()
Copy code

3. Write the code to open the file dialog

def open_file(self):
        self.file=filedialog.askopenfilename(title='Open File', filetypes=[('Text File','*.txt'), ('All Files','*')])
        return self.file
Copy code

This opens the file headed by the text file.

4. Select the opened file to read

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

5. Remove all spaces in the content of the file

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

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

if self.cb.get()=='Please choose-----':
            pass
        elif self.cb.get()=='Read all':
            if self.ab:
                self.te.insert('insert',self.ab) #Insert content
                self.te.update() #Update content
            else:
                self.ff.close()
        elif self.cb.get()=='One line per second':
            for y in range(len(self.ab)): #traverse file content
                if self.ab:
                    self.te.insert('insert',self.ab[y]) #Insert content
                    if y%10==0 and y!=0:#Judging if the length of ten words is read, insert the text content into the text box and wrap
                        self.te.insert('insert','\n') #Insert line break
                        self.te.update() #Update content
                    else:
                        time.sleep(0.1) #Display one every 0.1 seconds, ten characters in a line, you can get one line per second
                else:
                    self.ff.close() #Close the file
        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.ff.close()
        elif self.cb.get()=='Custom':
            res=simpledialog.askinteger(title='please enter', prompt='read a line in a few seconds:', 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 code

In this way, one line is output every ten bytes every second. Of course, you can also output it word by word. If so, just change the following code:

for y in range(len(self.ab)): #traverse file content
                if self.ab:
                    self.te.insert('insert',self.ab[y]) #Insert content
                    if y%10==0 and y!=0: #Judging if the length of ten bytes is read, insert the text content into the text box
                        self.te.insert('insert','\n')
                        self.te.update() #Update content
                    else:
                        time.sleep(0.1)
Copy code

To:

for y in range(len(self.ab)): #traverse file content
                if self.ab:
                    if y% 10==0 and y!=0: #Judging if the length of ten bytes is read, insert the text content into the text box
                        self.te.insert('insert','\n')
                    else:
                        self.te.insert('insert',self.ab[y]) #Insert content
                        self.te.update() #Update content
                        time.sleep(0.1)
Copy code

8. Clear content

def clean(self):
    self.te.delete('1.0', t.END) #Delete all contents of the text box
Copy code

This can be achieved.

Let's take a look at the specific effects:

In this way, we have easily implemented a novel reader. By the way, if you want to display a few more characters in one line, you only need to modify the number in the following line:

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

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

/summary/

Based on the Python library, this article writes a visual graphical interface, creates a simple novel reader, and realizes a novel reader with a custom character size. That's it for today's sharing. Welcome everyone to try.

Guess you like

Origin blog.csdn.net/Python_sn/article/details/113108623
Recommended