In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use Python to make a simple novel editor". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
/ concrete implementation /
For novel readers, of course, the interface is indispensable, so let's start writing the interface. 1. First import the package we need.
Import timefrom tkinter import messageboximport tkinter as tfrom tkinter import ttkfrom tkinter import filedialogfrom tkinter import simpledialog
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 window size self.root.wm_attributes ('- topmost',1) # window top self.root.wm_minsize ) # set window minimum size self.root.wm_maxsize (1440, 2800) # set window maximum size self.root.resizable (width=False, height=True) # set window width is immutable Height variable self.te=t.Text (self.root,width=60,height=40) # multiline text box self.b1= t.Button (self.root,text=' open file', font = ("Song style", 10 language bold`), command=self.open_file) self.cb=ttk.Combobox (self.root,width= 12) # drop-down list box self.b2=t.Button (self.root,text=' clears content' Command=self.clean) # Button self.l1=t.Label (self.root,text=' Please choose reading speed:') # tag self.cb ['values'] = (' Please select -', 'read all', 'one second one line', 'two second one line', 'custom') # set the contents of the drop-down list box self.cb.current (0) # set the current selection status to 0 That is, the first item self.cb.bind (", self.go) # binds the go function It then triggers the event self.b1.place (Xerox 30PowerYee 30) self.b2.place (Xerox 360 YPY 26) self.l1.place (Xerox 130LYY 30) self.te.place (XLY 30LYY 30) self.cb.place (XLY 230 LYY 30) self.root.mainloop ()
3. Write the code to open the file dialog box
Def open_file (self): self.file=filedialog.askopenfilename (title=' Open File', filetypes= [('text file','* .txt'), ('All Files',' *)]) return self.file
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 ()
5. Wrap all the spaces in the file
Self.ab=aa.replace. Replace. Strip ()
6. Implement the function of each option in the drop-down list
If self.cb.get () = 'Please select -': 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 yawning 0 and yawning 0self.te.insert # judge that if the length of ten words is read, insert the text content into the text box and wrap the text ('insert' '\ n') # insert newline self.te.update () # update content else: time.sleep (0.1) # display one every 0.1 seconds Ten words on a line Else: self.ff.close () # closes the file elif self.cb.get () = = 'two seconds one line': for y in range (len (self.ab)): if self.ab: self.te.insert ('insert') Self.ab [y]) if yawning 0 and yawning 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=' reads 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 yawning 0 and yawning 0: self.te.insert (' insert') '\ n') self.te.update () else: time.sleep (res/10) else: self.ff.close ()
This enables you to output a line every ten bytes every second, of course, you can also output it word by word, if so, just put the following code:
For y in range (len (self.ab)): # traversing the contents of the file if self.ab: self.te.insert ('insert',self.ab [y]) # inserting the content if: read 0 and ytes 0: # determine whether to insert the text content into the text box self.te.insert (' insert') if the length is read to ten bytes '\ n') self.te.update () # Update content else: time.sleep (0.1)
Change to:
For y in range (len (self.ab)): # traversing the contents of the file if self.ab: if y% 10 characters 0 and yawns 0: # determine if ten bytes are 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
8. Clear the content
Def clean (self): self.te.delete ('1.0, t.END) # Delete all the contents of the text box
In this way, it can be realized.
Let's take a look at the specific results:
In this way, we have easily implemented a novel reader. By the way, if you want to display a few more characters per line, you only need to modify the numbers in the following line:
If y% 10 percent 0 and yawns 0:
Change 10 to another number, and he will display characters of the corresponding length.
This is the end of the content of "how to make a simple novel Editor with Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.