共計(jì) 3192 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“怎么用 Python 制作簡(jiǎn)易的小說編輯器”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓丸趣 TV 小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
/ 具體實(shí)現(xiàn) /
小說閱讀器的話,當(dāng)然界面是少不了的,下面我們開始編寫界面。1、首先導(dǎo)入我們需要用到的包
import time
from tkinter import messagebox
import tkinter as t
from tkinter import ttk
from tkinter import filedialog
from tkinter import simpledialog
2、編寫主界面
class gui:
def __init__(self):
self.root=t.Tk()
self.root.title(小說閱讀器 V1.0) # 窗口名稱
self.root.geometry(700x700) # 設(shè)置窗口大小
self.root.wm_attributes(-topmost ,1) # 窗口置頂
self.root.wm_minsize(140, 170) # 設(shè)置窗口最小化大小
self.root.wm_maxsize(1440, 2800) # 設(shè)置窗口最大化大小
self.root.resizable(width=False, height=True) # 設(shè)置窗口寬度不可變,高度可變
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= 清空內(nèi)容 ,command=self.clean) # 按鈕
self.l1=t.Label(self.root,text= 請(qǐng)選擇閱讀速度:) # 標(biāo)簽
self.cb[values] = (請(qǐng)選擇 ----- , 全部讀取 , 一秒一行 , 兩秒一行 , 自定義) # 設(shè)置下拉列表框的內(nèi)容
self.cb.current(0) # 將當(dāng)前選擇狀態(tài)置為 0, 也就是第一項(xiàng)
self.cb.bind(ComboboxSelected ,self.go) # 綁定 go 函數(shù),然后觸發(fā)事件
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、編寫打開文件對(duì)話框的代碼
def open_file(self):
self.file=filedialog.askopenfilename(title= 打開文件 , filetypes=[( 文本文件 , *.txt), (All Files , *)])
return self.file
這樣就打開了以文本文件為首的文件。
4、選擇打開的文件進(jìn)行讀取
self.ff=open(self.file, r , encoding= utf8)
aa=self.ff.read()
5、將文件中的內(nèi)容的所有空格換行去掉
self.ab=aa.replace(\n ,).replace(\t ,).strip()
6、實(shí)現(xiàn)下拉列表中每個(gè)選項(xiàng)的功能
if self.cb.get()== 請(qǐng)選擇 ----- :
pass
elif self.cb.get()== 全部讀取 :
if self.ab:
self.te.insert(insert ,self.ab) # 插入內(nèi)容
self.te.update() # 更新內(nèi)容
else:
self.ff.close()
elif self.cb.get()== 一秒一行 :
for y in range(len(self.ab)): # 遍歷文件內(nèi)容
if self.ab:
self.te.insert(insert ,self.ab[y]) # 插入內(nèi)容
if y%10==0 and y!=0:# 判斷如果讀取到十個(gè)字的長(zhǎng)度則插入文本內(nèi)容到文本框并換行
self.te.insert(insert , \n) # 插入換行
self.te.update() # 更新內(nèi)容
else:
time.sleep(0.1) # 每隔 0.1 秒顯示一個(gè),一行十個(gè)字,就能達(dá)到一秒出一行
else:
self.ff.close() # 關(guān)閉文件
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 , \n)
self.te.update()
else:
time.sleep(0.2)
else:
self.ff.close()
elif self.cb.get()== 自定義 :
res=simpledialog.askinteger(title= 請(qǐng)輸入 ,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 , \n)
self.te.update()
else:
time.sleep(res/10)
else:
self.ff.close()
這樣就實(shí)現(xiàn)了每隔一秒每十個(gè)字節(jié)輸出一行,當(dāng)然你也可以一個(gè)字一個(gè)字讓它輸出,如果是這樣,只需將下面的代碼:
for y in range(len(self.ab)): # 遍歷文件內(nèi)容
if self.ab:
self.te.insert(insert ,self.ab[y]) # 插入內(nèi)容
if y%10==0 and y!=0: # 判斷如果讀取到十個(gè)字節(jié)長(zhǎng)度則插入文本內(nèi)容到文本框
self.te.insert(insert , \n)
self.te.update() # 更新內(nèi)容
else:
time.sleep(0.1)
改為:
for y in range(len(self.ab)): # 遍歷文件內(nèi)容
if self.ab:
if y % 10==0 and y!=0:
# 判斷如果讀取到十個(gè)字節(jié)長(zhǎng)度則插入文本內(nèi)容到文本框
self.te.insert(insert , \n)
else:
self.te.insert(insert ,self.ab[y]) # 插入內(nèi)容
self.te.update() # 更新內(nèi)容
time.sleep(0.1)
8、清空內(nèi)容
def clean(self):
self.te.delete(1.0 , t.END) # 刪除文本框所有內(nèi)容
這樣就可以實(shí)現(xiàn)了。
下面我們來看下具體效果吧:
這樣我們就輕松實(shí)現(xiàn)了一個(gè)小說閱讀器,順帶提一下,你想一行多顯示幾個(gè)字符,只需要修改下面這行的數(shù)字即可:
if y % 10==0 and y!=0:
把 10 改為其他數(shù)字,他就會(huì)顯示相應(yīng)長(zhǎng)度的字符了。
“怎么用 Python 制作簡(jiǎn)易的小說編輯器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實(shí)用文章!