A little software practice python

Source: Internet
Author: User

A friend wrote a small software in Python, just not how to seriously use Python programming, so improve

The main practice of some knowledge:

1.python Tkinter page Layout, this time the software used a grid

2.Event Event Binding

3. Processing of strings.

Use of 4.tkFileDialog

5. A little bit of object-oriented programming.

6.python Coding structure and practice code good Habits

Post-improvement:

1. Add scroll bar

2. Add algorithm transform file

3. You can modify the save path

4. Hardening Error Handling

5. Add a function that, depending on the type, opens the converted file with the direct double-click Office.

The pits encountered:

Error in mouse event binding:

When using the Bind method, the binding event must be written in a branch, as follows, the correct way to write, otherwise, if written in one line, the mouse binding event will have type error

self.Text_in=Text(master,width=40,height=25,yscrollcommand=self.Text_in_sv.set,xscrollcommand=self.Text_in_sh.set, wrap=‘none‘)
self.Text_in.insert(END,‘请双击选择文件,可多选!!‘)
self.Text_in.grid(row=2,column=0)

Incorrect notation, so that the event binding is unsuccessful:

Self. Text_in=text (master,width=40,height=25,yscrollcommand=self. Text_in_sv.set,xscrollcommand=self. Text_in_sh.set, wrap= ' None '). Grid (Row=2,column=0)

String Conversion Essentials

Pop-up string last character elegant notation files[:-1]

Rough Code:

For name in filenames:                if name = = Filenames[-1]: Self                    . Text_in.insert (end,name) break self                . Text_in.insert (end,name+ ' \ n ')


For the last line without ' \ n ', only in this way, there is no elegant wording

#-*-coding:utf-8-*-# __author__ = ' Big Cow ' from Tkinter import *import tablibimport tkinterimport tkfiledialogclass Main Frame (tkinter.frame): def __init__ (self,master): tkinter.frame.__init__ (Self,master) Label_top=la        Bel (master,text= ' Convert small Software ') Label_top.grid (row=0,columnspan=3) Label_l=label (master,text= "Conversion input:")                Label_l.grid (row=1,column=0) Label_m=label (master,text= "========") Label_m.grid (Row=1,column=1) Label_r=label (master,text= "Convert output") Label_r.grid (row=1,column=2) self. TEXT_IN_SV = Tkinter.scrollbar (self, orient=tkinter.vertical) #文本框-vertical scroll bar self. Text_in_sh = Tkinter.scrollbar (self, orient=tkinter.horizontal) #文本框-horizontal scroll bar self. Text_in=text (master,width=40,height=25,yscrollcommand=self. Text_in_sv.set,xscrollcommand=self. Text_in_sh.set, wrap= ' none ') self. Text_in.insert (END, ' Double click to select File, can choose multiple!! ') self. Text_in.grid (row=2,column=0) BuTton_trans=button (master,text= '---"xls ', Width=6,command=self.txt2xls) Button_trans.grid (row=2,column=1) Self. Text_out = Text (master,width=40,height=25,wrap= ' none ') self. Text_out.grid (row=2,column=2,padx=15) label_bottom =label (master,text= ' Power by Cow ') LABEL_BOTTOM.G RID (row=3,columnspan=3,sticky= ' es ') self. Text_in.bind (' <Double-Button-1> ', self. Operatefiles) # define options for opening or saving a file Self.file_opt = options = {} Optio ns[' defaultextension ' = '. txt ' options[' filetypes '] = [(' All Files ', '. * '), (' Text files ', '. txt ')] options [' initialdir '] = ' c:\\ ' options[' initialfile '] = ' cow.txt ' options[' parent '] = Master options[' title '        ] = ' Pls chouse a file ' options[' multiple '] = 1 #save Path self.dir_opt = options = {} options[' initialdir ' = ' c:\\ ' options[' mustexist '] = False options[' parent '] = MasteR options[' title '] = ' This is a title ' Def operatefiles (self,event=none): filenames = Tkfiledialo G.askopenfilename (**self.file_opt) if filenames:self.                    Text_in.delete (0.0, tkinter.end) for name in Filenames:if name = = Filenames[-1]: Self. Text_in.insert (end,name) break self. Text_in.insert (end,name+ ' \ n ') def Openxls (self): Pass def Txt2xls (self): files = Self .                Text_in.get (1.0, END) if files:for text in Files[:-1].split (' \ n '): Print text                      Try:headers = (' servername ', ' location ', ' name ', ' upstream ') data = [] data = Tablib. Dataset (*data, Headers=headers) linuxlines=open (text, "RB") for line in Linuxlines.re                        Adlines (): Col=line.split (' | ') If Len (COL)!=4:continue for server in Col[3].split (";"):                    Data.append ([Col[0],col[1],col[2],server]) Open (text+ ". xls", "WB"). Write (Data.xls) Linuxlines.close () self. Text_out.insert (end,text+ '. xls\n ') except:print "Error" Else:print " Please enter the correct file!! "Def main (): root = tkinter.tk () root.columnconfigure (0, weight=1) root.rowconfigure (0, W    eight=1) root.title (' Txt2xls ') root.geometry (' 620x360 ') Main_frame = MainFrame (root) Main_frame.mainloop () if __name__ = = "__main__": Main () Pass

A little software practice python

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.