[Tutor] my first gui

Lie Ryan lie.1296 at gmail.com
Wed Jun 3 18:20:20 CEST 2009


prasad rao wrote:
> Hello.
> I made my first mager gui program.I need your openions suggestions and
> improvements.
>  
> 
> #! usr\\bin\\env python

Why is the shebang like that?

> from Tkinter import *

importing * is considered namespace pollution since you cannot control
what name would be introduced to the global namespace. Better to use the
regular "import Tkinter" (Tips: typing Tkinter for every control is
often distracting, I usually do "import Tkinter as Tk" to shorten it,
much better than import *)

> def myfiles (n='',m=''):
>     import os
>     mf=[os.path.join(x,i)for x,y,z in os.walk(n) for i in z if
> i.endswith(m)]
>    
>     return mf
> 
> def fshow():
>     tclear()
>     x=entry1.get()
>    
>     try:
>        value1,value2=x.split(',')
>        mf=myfiles(value1,value2)
>    
>        text.insert(END,('Total files in %s are %d
> \n'%(entry1.get(),len(mf)))+ mystring(mf))
>     except:
>         mf=myfiles(x)
>         text.insert(END,('Total files in %s are %d
> \n'%(entry1.get(),len(mf)))+ mystring(mf))


> def atime(x):
>        
>    import time
>    import os
>    atime=time.strftime("%c",time.localtime(os.path.getatime(x)))
>    return atime

The way you're doing right now will import time and os every function
call. Although python interpreter is smart enough to not reiimport a
module twice, for various reasons it is usually better to place all
imports on the top of the module.

> def mtime(x):
>       
>  import time
>  import os
>  mtime=time.strftime("%c",time.localtime(os.path.getmtime(x)))
>  return mtime
>     
> def ctime(x):
>  import time
>  import os
>  ctime=time.strftime("%c",time.localtime(os.path.getctime(x)))
>  return ctime
> def mystring(x):
>  q=''
>  for n,m in  enumerate(x,start=1):
> 
>    o=str(n)+'.'+str(m)

you could use %=formatting there
o = '%s.%s' % (n, m)

>    q+=(o+'\n')
>  return q+'\n'

>               
> def info():
>         tclear()
>         import glob,os
>         mf=''
>         md=''
>         mfl,mdl=[],[]    
>         mdd=glob.glob(entry1.get()+os.sep+'*')
>         for x in mdd:
>             if os.path.isfile(x)==True:

in python, you usually don't test against True in an if statement
(though it won't really matter)
if os.path.isfile(x):
    pass

>   mfl.append(x)

I assume this line should appear more indented, probably the mail client
is playing some tricks...

>             else:mdl.append(x)
>        
>         mf+=mystring(mfl)
>         md+=mystring(mdl)
>         mf=("Total files in %s are %d \n\n"%(entry1.get(),len(mfl)))+mf
>         md=('Total directories in %s are %d
> \n\n'%(entry1.get(),len(mdl)))+md
>         mf+='\n\n'
>         text.insert(END,mf+md)

What's wrong with the indentation? Is this the mail client messing
things up or you should use consistent indentation. PEP 8
(http://www.python.org/dev/peps/pep-0008/ ) recommends 4 spaces. Now
we're talking about PEP 8; I should also mention that the recommended
way to style your code is like this:

def func(a, b, c=2, d=3):
    e = a + b
    r = calling(a, s='John')

you can read the details in the PEP 8 link. Basically space after
commas, and space on both side of operators; whitespace on assignment =
except on function argument assignment.

> def destroy():
>     root.destroy()
> def eclear():
>     entry1.delete(0,END)
>     entry2.delete(0,END)
>     entry3.delete(0,END)
>     entry4.delete(0,END)
> def tclear():
>     text.delete(1.0,END)
> def ashow():
>     x=entry1.get()
>     try:
>         n,m=x.split(',')
>                 
>         value=atime(n)
>     except:value=atime(x)

don't use bare except; catch only the specific exception you want to catch.

>     entry2.insert(0,value)
> def mshow():
>      
>     x=entry1.get()
>     try:
>         n,m=x.split(',')
>                 
>         value=mtime(n)
>     except:value=mtime(x)
>    
>     entry3.insert(0,value)
> def cshow():
>    
>     x=entry1.get()
>     try:
>         n,m=x.split(',')
>                 
>         value=ctime(n)
>     except:value=ctime(x)
>     entry4.insert(0,value)

You could use an
if __name__ == '__main__':
here

> root = Tk()
>                    
> frame1=Frame(root,relief='sunken',border=1)
> frame1..pack(side='top',expand="true")
> 
> frame2=Frame(root,relief='sunken',border=1)
> frame2.pack(side='top',expand="true")
> 
> frame3=Frame(root,relief='sunken',border=1)
> frame3.pack(side='top',expand="true")
> 
> frame4=Frame(root,relief='sunken',border=1,)
> frame4.pack(side='top',expand="true")
> 
> frame5=Frame(root,relief='sunken',border=1)
> frame5.pack(side='top',expand="true")
> 
> label5=Label(frame1,text="Enter file path to get information about the
> file \
> or enter directory(or directory,fileextension) to get files init ",border=1)
> label5.pack(side='top',fill='both')
> 
> b1=Button(frame2,text='quit',command=destroy,border=1)
> b1.pack(side='left',padx=5,pady=5)
> 
> b2=Button(frame2,text='clear',command=eclear,border=1)
> b2.pack(side='left',padx=5,pady=5)
> 
> b3=Button(frame2,text='accessed',command=ashow,border=1)
> b3.pack(side='left',padx=5,pady=5)
> 
> b4=Button(frame2,text='modified',command=mshow,border=1)
> b4.pack(side='left',padx=5,pady=5)
> 
> b5=Button(frame2,text='created',command=cshow,border=1)
> b5.pack(side='left',padx=5,pady=5)
> 
> b5=Button(frame2,text='files',command=fshow,border=1)
> b5.pack(side='left',padx=5,pady=5)
> 
> b6=Button(frame2,text='deltxt',command=tclear,border=1)
> b6.pack(side='left',padx=5,pady=5)
> 
> b7=Button(frame2,text='files+dirs',command=info,border=1)
> b7.pack(side='left',padx=5,pady=5)
> 
> label4=Label(frame3,text="Enter full path",border=1)
> label4.pack(side='left',fill='both')
> 
> entry1=Entry(frame3,relief='sunken',border=1)
> entry1.pack(side='left',fill='both')
> 
> lable1=Label(frame3,text='access time',border=1)
> lable1..pack(side='left',fill='both')
> 
> entry2=Entry(frame3,relief='sunken',border=1)
> entry2.pack(side='left',fill='both')
> 
> lable2=Label(frame3,text='modifide time',border=1)
> lable2.pack(side='left',fill='both')
> 
> entry3=Entry(frame3,relief='sunken',border=1)
> entry3.pack(side='left',fill='both')
> 
> lable3=Label(frame3,text='created time',border=1)
> lable3.pack(side='left',fill='both')
> 
> entry4=Entry(frame3,relief='sunken',border=1)
> entry4.pack(side='left',fill='both')

Instead of names like entry1, entry2, entry3, and entry4 you should use
a more descriptive name like path, accesstime, modtime, createdtime. I
usually use a plain name (e.g. path) for the most significant GUI
element and a plain name++ (e.g. path_label) for GUI element that
describes the main elements.

> text=Text(frame4,relief='sunken',border=1,width=130,height=40,padx=5,pady=5)
> text.pack(side='top')
> text.xview(SCROLL,30,UNITS)
> text.yview(SCROLL,30,UNITS)
> 
> root.title("info of file")
> 
> root.mainloop()
> 
> ==
> 
> Thank you
> 
> Prasad
> 
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list