专栏名称: 编程派
Python程序员都在看的公众号,跟着编程派一起学习Python,看最新国外教程和资源!
目录
相关文章推荐
51好读  ›  专栏  ›  编程派

手把手教你用Python制作简易小说阅读器

编程派  · 公众号  · Python  · 2020-12-04 11:40

正文

请到「今天看啥」查看全文


from tkinter import simpledialog

2、编写主界面

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)    #将当前选择状态置为0,也就是第一项self.cb.bind("<>",self.go)  #绑定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、编写打开文件对话框的代码

def open_file(self):self.file=filedialog.askopenfilename(title='打开文件', filetypes=[('文本文件', '*.txt'), ('All Files', '*')])return self.file

这样就打开了以文本文件为首的文件。

4、选择打开的文件进行读取

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

5、将文件中的内容的所有空格换行去掉

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

6、实现下拉列表中每个选项的功能


  
if self.cb.get()=='请选择-----':






请到「今天看啥」查看全文