python3.5 using Tkinter to make Notepad

Source: Internet
Author: User
Tags save file
Tkinter is a graphical interface library of Python to TK, which allows easy graphical interface design and interactive operation programming. The advantage of Tkinter is that it is easy to use and a good combination with Python. Tkinter is integrated by default in Python 3.x without the need for additional installation operations; The lack of a suitable visual interface design tool requires code to complete the window design and element layout.

This section uses a Python version of 3.x, and if you want to use Tkinter under Python 2.x, install it through Apt-get. It is important to note that Tkinter may be used slightly differently under different Python versions, with Python3 recommended.

Import Tkinter as Tkimport tkinter.scrolledtext as Tkstimport fileinputfrom tkinter import *import osimport Win32clipboard Import TKINTER.FONTT1 = []root = Nonedef die (event = None): Root.destroy () def about (): messagebox.showinfo (title = "Current Version" For 1.0, Welcome to ", message =" * * Author: Han Dong \n** status: Keep trying ing ") #def Newit (event): #editorit = editor () #editorit. Neweditor () class editor (): Def __init__ (SELF,RT): if RT = = none:self.t = Tk. TK () else:self.t = Tk. TopLevel (RT) Self.t.title ("text editor%d"% (len (t1) +1)) Self.frm_file = Tk. Frame (RT) Self.frm_file.grid (row =0,column =0,padx =0,sticky = W) Self.btn_open = button (Self.frm_file,text = "Open", R  Elief = Groove,command = self.openfile) self.btn_open.pack (side = left,padx =5,fill = Both,expand =1) self.btn_new =  button (Self.frm_file,text = "new", relief = Groove,command = Self.neweditor) self.btn_new.pack (side =left,padx =5,fill = BOTH) Self.btn_save = button (Self.frm_file,text = "save", relief = Groove,command = Self.saVefile) self.btn_save.pack (side = left,padx =5,fill = BOTH) Self.btn_saveas = button (Self.frm_file,text = "Save As", Reli EF = Groove,command = self.saveasfile) self.btn_saveas.pack (side =left,padx =5,fill = BOTH) Self.btn_exit = Button (s    Elf.frm_file,text = "Exit", relief = Groove,command = Self.close) self.btn_exit.pack (side = right,padx =5,fill = BOTH) Self.frm_edit = Tk. Frame (RT) Self.frm_edit.grid (row = 0, column =1,padx =1,sticky = W) self.btn_copy = button (Self.frm_edit,text = "Copy"    ) Self.btn_copy.pack (side = left,padx =5,fill = Both,expand =1) self.btn_cut = button (Self.frm_edit,text = "Cut") Self.btn_cut.pack (side = left,padx =5,fill = Both,expand =1) self.btn_paste = button (Self.frm_edit,text = "paste") self . Btn_paste.pack (side = left,padx =5,fill = Both,expand =1) self.btn_find = button (Self.frm_edit,text = "Query") self.bt N_find.pack (side = left,padx =5,fill = Both,expand =1) self.btn_allselect = button (Self.frm_edit,text = "Select All") self.b Tn_alLselect.pack (side = left,padx =5,fill = Both,expand =1) self.btn_font = button (Self.frm_edit,text = "font style") self.btn _font.pack (side = left,padx =5,fill = Both,expand =1) self.btn_color = button (self.frm_edit,text = "Font Color") self.btn_ Color.pack (side = left,padx =5,fill = Both,expand =1) Self.bar = tk. Menu (RT) Self.filem = Tk.  Menu (Self.bar) self.filem.add_separator () self.filem.add_command (label = "new", command = Self.neweditor,accelerator = "Ctr +n") Self.filem.bind_all ("
 
  ", Self.neweditor) Self.filem.add_separator () self.filem.add_command (label =" open ", command = Self.openfile,accelerator = "Ctr + O") Self.filem.bind_all ("
  
   ", Self.openfile) Self.filem.add_separator () self.filem.add_command (label =" Save ", command = Self.savefile,accelerator = "Ctr +s") Self.filem.bind_all ("
   
    ", Self.savefile) Self.filem.add_separator () self.filem.add_command (label =" Save As ", command = Self.saveasfile,accelerat or = "Ctr + D") Self.filem.bind_all ("
    
     ", Self.saveasfile) self.filem.add_command (label =" Close ", command = Self.close,accelerator =" F4 ") self.filem.bind _all ("
     
      ", Self.close) Self.filem.add_separator () self.filem.add_command (label =" Exit ", command = Die,accelerator =" ESC ") Self.filem.bind_all ("
      
       ", die) Self.editm = Tk. Menu (Self.bar) self.editm.add_separator () self.editm.add_command (label = "Copy", command = Self.copy,accelerator = "" *10 + "Ctr + C") Self.editm.bind_all ("
       
        ", Self.copy) Self.editm.add_separator () self.editm.add_command (label =" Paste ", command = Self.paste,accelerator =" "*10 +" Ctr + V ") Self.editm.bind_all ("
        
         ", Self.paste) Self.editm.add_separator () self.editm.add_command (label =" Cut ", command = Self.cut,accelerator =" "*10 +" C TR + X ") Self.editm.bind_all ("
         
          ", Self.cut) Self.editm.add_separator () self.editm.add_command (label =" delete ", command = Self.delete_text,accelerator =" " *10 + "Delete") Self.editm.bind_all ("
          
           ", Self.delete_text) Self.editm.add_separator () self.editm.add_command (label =" find ", command = Self.find_char, Accelerator = "" *10 + "Ctr +f") Self.editm.bind_all ("
           
             ", Self.find_char) Self.editm.add_separator () self.editm.add_command (label =" Select All ", command = Self.select_char_all, Accelerator = "" *10 + "Ctr + A") Self.editm.bind_all ("
            
              ", self.select_char_all) self.formm = tk. Menu (Self.bar) self.formm.add_command (label = "Font Color", command = Self.color_it,accelerator = "" *10 + "Alt + C") self.formm. Bind_all ("", Self.color_it) Self.formm.add_separator () self.formm.add_command (label = "font format", command = Self.font_it, Accelerator = "" *10 + "Alt + F") Self.formm.bind_all ("", self.font_it) SELF.HELPM = tk. Menu (Self.bar) self.helpm.add_command (label = "about", command = About) self.bar.add_cascade (label = "File", menu = Self.filem) Self.bar.add_cascade (label = "Edit", menu = SELF.EDITM) self.bar.add_cascade (label = "format", menu = SELF.FORMM) self.bar.add_ Cascade (label = "Help", menu = SELF.HELPM) self.t.config (menu = Self.bar) #self. F = tk. Frame (self.t,width =) #self. F.pack (expand =1) Self.st = Tkst. Scrolledtext (self.t) self.st.grid (Row =1,column = 0,columnspan =3,pady =3) def close (self,event = None): Self.t.destroy () def openfile (self,event =none): Oname = filedialog.askopenfilename (filetypes = [("Open File", "*.txt")]) if Oname:for line in FilEinput.input (oname): Self.st.insert ("1.0", line) Self.t.title (oname) def savefile (self,event =none): If Os.path.isfile (Self.t.title ()): Opf = open (Self.t.title (), "W") Opf.write (Self.st.get (1.0,TK. END)) Opf.flush () opf.close () Else:sname = filedialog.asksaveasfilename (title = "Save Your Baby Yo", filetypes = [("Save File", "*.txt" )],defaultextension = ". txt") If SNAME:OFP = open (sname, "W") Ofp.write (Self.st.get (1.0,TK). END)) Ofp.flush () Ofp.close () Self.t.title (sname) def saveasfile (self,event = None): sname = Filedialog.asksaveasfilename (title = "Save Your Baby Yo", filetypes = [("Save File", "*.txt")],defaultextension = ". txt") if SNAME:OFP = Open (Sname, "W") Ofp.write (Self.st.get (1.0,TK. END)) Ofp.flush () Ofp.close () Self.t.title (sname) def neweditor (self,event = None): Global Root t1.append (editor (root)) d EF copy (self,event = None): Text = Self.st.get (tk. Sel_first,tk. Sel_last) Self.st.clipboard_clear () self.st.clipboard_append (text) def paste (self,event= None): Try:text = Self.st.selection_get (selection = "ClipboaRD ") Self.st.insert (tk. Insert,text) self.st.clipboard_clear () except Tk. Tclerror:pass def cut (self,event = None): Text = Self.st.get (tk. Sel_first,tk. Sel_last) Self.st.delete (tk. Sel_first,tk. Sel_last) Self.st.clipboard_clear () self.st.clipboard_append (text) def delete_text (self): Self.st.delete (tk. Sel_first,tk. Sel_last) def find_char (self,event = None): target = simpledialog.askstring ("Simple text editor", "Look for string") if Target:end = self.st.in Dex (TK. End) print (end) Endindex = End.split (".") end_line = Int (endindex[0]) end_column = Int (endindex[1]) pos_line =1 pos_column =0 length =len (target) while pos_line <= end_line:if pos_line = end_line and pos_column +length > End_column:bre AK Elif Pos_line < end_line and Pos_column + length >500:pos_line = pos_line + 1 Pos_column = (pos_column + length) -500 if pos_column > End_column:break else:pos = str (pos_line) + "." +str (pos_column) where = Self.st.search (TARGET,POS,TK. END) if Where:print (where) Where1 =where.split (".") Sele_end_col = STR (int (where1[1]) +length) Sele = where1[0] + "." + Sele_end_col Self.st.tag_add (tk. Sel,where,sele) Self.st.mark_set (tk. Insert,sele) Self.st.see (tk. INSERT) Self.st.focus () again = Messagebox.askokcancel (title = "Continue query") If again:pos_line = Int (where1[0]) Pos_column = i NT (Sele_end_col) else:aa=messagebox.showinfo (title = "You finally gave me up", message = "You gave up my--!") if Aa:sys.exit () def Select_char _all (self,event= None): Self.st.tag_add (tk. Sel,1.0,tk. END) Self.st.see (tk. INSERT) Self.st.focus () def color_it (self,event = None): color = Colorchooser.askcolor () self.st["foreground"] = color[1] def font_it (self,event = None): Self.t_font = tk. TopLevel () self.t_font.title ("Font selection panel") self.label_size = label (self.t_font,text = "Font Size") Self.label_shape = label ( Self.t_font,text = "Font shape") self.label_font = label (self.t_font,text = "font type") self.label_weight = label (self.t_font,text = "Font weight") self.label_size.grid (row = 0, column =0,PADX =30) self.label_shape.grid (row = 0,column =4,padx =30) self.label_f ont.grID (row = 0,column =2,padx =30) self.label_weight.grid (Row =0,column = 6,padx =30) self.scroll_size = Scrollbar (self.t_font ) Self.scroll_size.grid (Row =1,column=1,stick = NS) Self.scroll_shape = Scrollbar (self.t_font) Self.scroll_shape.grid ( Row =1,column=3,stick = ns) Self.scroll_font = Scrollbar (Self.t_font) self.scroll_font.grid (Row =1,column=5,stick = ns) s Elf.scroll_weight = Scrollbar (Self.t_font) self.scroll_weight.grid (row =1,column =7,stick = NS) List_var_font = Stringvar () List_var_size = Stringvar () List_var_shape = Stringvar () List_var_weight = Stringvar () Self.list_font = Listbo X (self.t_font,selectmode = browse,listvariable = List_var_font,exportselection =0) self.list_font.grid (row = 1,column = 2,PADX =4) List_font_item = ["\" Arial\ "", "\" Arial baltic\ "", "\" Arial black\ "", "\" Arial ce\ "", "\" Arial cyr\ "", "\" Arial Greek\ "", "\" Arial narrow\ "", "\" Arial tur\ "", "\" Baiduan number\ "", "\" Batang,batangche\ ""] for item in List_font_item: Self.list_font.insert (0,item) Self.list_font.bind (" 
             
              ", self.change_font) Self.list_shape = Listbox (Self.t_font,selectmode = browse,listvariable =list_var_shape, Exportselection =0) Self.list_shape.grid (row= 1,column =4,padx =4) List_shape_item = ["Italic", "Roman"] for item in List_ Shape_item:self.list_shape.insert (0,item) Self.list_shape.bind ("
              
               ", self.change_shape) self.list_size = Listbox (Self.t_font,selectmode = browse,listvariable = List_var_size, Exportselection =0) self.list_size.grid (row = 1,column = 0,padx =4) List_size_item = [ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] for item in List_size_item:self.list_size.insert (0,item) self.list_ Size.bind ("
               
                ", self.change_size) Self.list_weight = Listbox (Self.t_font,selectmode = browse,listvariable = List_var_weight, Exportselection =0) Self.list_weight.grid (row=1,column =6,padx =4) List_weight_item = ["bold", "normal"] for item in List_ Weight_item:self.list_weight.insert (0,item) Self.list_weight.bind ("
                
                  ", self.change_weight) Self.labfra_display = labelframe (self.t_font,text =" Font style demo area ") Self.labFra_display.grid (Row =2 , column =0,pady =4) self.lab_display = Label (Self.labfra_display,text = "I'm Here") self.lab_display.pack () Self.btn_ok = but ton (Self.t_font,text = "OK", width = 10,height =2,command = self.change) self.btn_ok.grid (row = 2,column = 2,pady =4) self. Btn_cancel = button (self.t_font,width =10,height =2,text = "Cancel", command = Self.exit_subwindow) Self.btn_cancel.grid (row =2,column =4,pady =4) def change_size (self,event): Tk.customfont = font. Font (family = "Helvetica", size = 12,weight = "normal", slant = "Roman", underline =0) size = tk.customfont["Size"] Tk.custom Font.configure (Size =self.list_size.get (Self.list_size.curselection ())) self.st.config (font = tk.customfont) Self.size_count = 1 Pass def change_font (self,event): Tk.customfont = font. Font (family = "Helvetica", size = 12,weight = "normal", slant = "Roman", underline =0) family = tk.customfont["Family"] tk.cu Stomfont.configure (Family =self.list_font.get (Self.list_font.curselection ())) self.st.config (font = tk.customfont) Self.font_count = 1 Pass def Change_shape (self,event): Tk.customfont = font. Font (family = "Helvetica", size = 12,weight = "normal", slant = "Roman", underline =0) slant = tk.customfont["slant"] Tk.cust Omfont.configure (Slant =self.list_shape.get (Self.list_shape.curselection ())) self.st.config (font = tk.customfont) Self.shape_count =1 Pass def change_weight (self,event): Tk.customfont = font. Font (family = "Helvetica", size = 12,weight = "normal", slant = "Roman", underline =0) weight = tk.customfont["Weight"] tk.cu Stomfont.configure (Weight =self.list_weight.get (self.list_weight.curselection ())) self.st.config (font = Tk.customfont) Self.shape_count =1 def Change (self,event): Pass #self. st["Font"] = (Self.list_size.get (self.list_ Size.curselection ())) #self. st["Font"] = (Self.list_font.get (self.list_font.curselection ())) #self. st["Font"] = ( Self.list_shape.get (Self.list_shape.curselection ())) def exit_sUbwindow (self): Self.t_font.destroy () if __name__ = = "__main__": root = None T1.append (editor (root)) root = t1[0].t ROOT.M Ainloop ()
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

The above is the article to share the code, I hope you can like

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.