一个无所谓-熟悉一下python

朋友用python写了一个小软件,刚好没有怎么认真用python编过程序,于是就改进一下

主要练习了一些知识:

   1.python Tkinter 页面布局,这次软件用了用了grid

   2.Event 事件绑定

   3. 字符串的处理。

   4.tkFileDialog 的使用

   5.一点点面向对象的编程。

   6.python编码的结构以及练习Code好习惯

后期改进:

1. 加滚动条

2. 加算法变换文件

3. 可以修改保存路径

4.强化错误处理

5.添加函数,根据类型,用office直接双击打开转换成功的文件。

 

遇到的坑:

鼠标事件绑定出错:

当用bind方法,绑定事件时,必须分行来写,如下为正确的写法,否则如果用一行来写,鼠标绑定事件会有type 的错误

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)

 错误的写法,这样会到时Event绑定不成功:

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)

字符串转换要点

弹出字符串最后一个字符优雅的写法 files[:-1]

粗糙的代码:

for name in filenames:
                if name == filenames[-1]:
                    self.Text_in.insert(END,name)
                    break
                self.Text_in.insert(END,name+'\n')

 

为了最后一行不加'\n',只能用这种方法,有没有优雅的写法

 

 

 

 

 

#-*- coding:utf-8 -*-
# __author__ = 'Big Cow'

from Tkinter import *
import tablib
import Tkinter
import tkFileDialog




class MainFrame(Tkinter.Frame):
    def __init__(self,master):
        Tkinter.Frame.__init__(self,master)
        
        Label_top=Label(master,text='转换小软件')
        Label_top.grid(row=0,columnspan=3)
        
        Label_L=Label(master,text="转换输入:")
        Label_L.grid(row=1,column=0)
        
        Label_M=Label(master,text="========")
        Label_M.grid(row=1,column=1)
        
        Label_R=Label(master,text="转换输出")
        Label_R.grid(row=1,column=2)

        self.Text_in_sv = Tkinter.Scrollbar(self, orient=Tkinter.VERTICAL)  #文本框-竖向滚动条  
        self.Text_in_sh = Tkinter.Scrollbar(self, orient=Tkinter.HORIZONTAL)  #文本框-横向滚动条  
  

        
        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)
        
        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.grid(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 = {}
        options['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 = tkFileDialog.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.readlines():
                        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 "请输入正确的文件!!"

        
        
        


def main():
    root = Tkinter.Tk()
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    root.title('Txt2Xls')
    root.geometry('620x360')

    main_frame = MainFrame(root)
    main_frame.mainloop()
    
if __name__ == "__main__":
    main()
    pass

 

posted @ 2016-05-14 22:08  QQ是条狗  阅读(385)  评论(0编辑  收藏  举报