Build a Simple Python Novel Reader with Adjustable Speed
This article walks through creating a lightweight novel reader in Python using Tkinter, covering package imports, GUI layout, file handling, customizable reading speeds, and code snippets, enabling users to display text from a file character‑by‑character or line‑by‑line at set intervals.
Introduction
This tutorial demonstrates how to build a simple novel reader with Python's Tkinter library, allowing text from a file to be displayed at a configurable speed.
Implementation Steps
1. Import required packages
import time
from tkinter import messagebox
import tkinter as t
from tkinter import ttk
from tkinter import filedialog
from tkinter import simpledialog2. Create the main GUI
class gui:
def __init__(self):
self.root = t.Tk()
self.root.title('小说阅读器V1.0')
self.root.geometry('700x700')
self.root.wm_attributes('-topmost', 1)
self.root.wm_minsize(140, 170)
self.root.wm_maxsize(1440, 2800)
self.root.resizable(width=False, height=True)
self.te = t.Text(self.root, width=60, height=40)
self.b1 = t.Button(self.root, text='打开文件', font=('宋体', 10, 'bold'), command=self.open_file)
self.cb = ttk.Combobox(self.root, width=12)
self.b2 = t.Button(self.root, text='清空内容', command=self.clean)
self.l1 = t.Label(self.root, text='请选择阅读速度:')
self.cb['values'] = ('请选择-----', '全部读取', '一秒一行', '两秒一行', '自定义')
self.cb.current(0)
self.cb.bind('<<ComboboxSelected>>', self.go)
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()3. Open file dialog
def open_file(self):
self.file = filedialog.askopenfilename(title='打开文件', filetypes=[('文本文件', '*.txt'), ('All Files', '*')])
return self.file4. Read file content
self.ff = open(self.file, 'r', encoding='utf8')
aa = self.ff.read()5. Clean whitespace
self.ab = aa.replace('
', '').replace('\t', '').strip()6. Implement reading speed options
if self.cb.get() == '请选择-----':
pass
elif self.cb.get() == '全部读取':
if self.ab:
self.te.insert('insert', self.ab)
self.te.update()
else:
self.ff.close()
elif self.cb.get() == '一秒一行':
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', '
')
self.te.update()
else:
time.sleep(0.1)
else:
self.ff.close()
elif self.cb.get() == '两秒一行':
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', '
')
self.te.update()
else:
time.sleep(0.2)
else:
self.ff.close()
elif self.cb.get() == '自定义':
res = simpledialog.askinteger(title='请输入', prompt='几秒读取一行:', 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', '
')
self.te.update()
else:
time.sleep(res / 10)
else:
self.ff.close()7. Clear text area
def clean(self):
self.te.delete('1.0', t.END)Result
The application displays the novel text according to the selected speed; changing the number in the condition if y % 10 == 0 adjusts how many characters appear per line.
Conclusion
This simple Tkinter‑based GUI demonstrates how to create a customizable novel reader, and the speed can be easily modified by changing the numeric value in the code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
