Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1752313
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2013-04-16 22:49:02

        最近工作的时候,由于老是使用COMMAND,感觉不是太爽,因此想能不能写一个GUI?
想到就动手,随便找了已给GUI的书,敲了一段不到100行的代码后发现,不论怎么运行,都出不来结果,网上一搜,发现还真有人实现--
代码粘贴,复制,发现仍旧是没有效果?因此重新调试代码,step by step,1个多小调试,发现自己犯了一个比较低级的错误,可笑的是
 明明代码错误,但是仍给出了正确的截图?我手上的电子书也是出版物,可见一斑(书名就不说了,没有必要人身攻击--哈哈)?但是,对于想学习的人而言,一定不要眼高手低,很多东西,貌似很简单,真正的动手时,你可能就会错误百出,还是那句话,
实践出真知,同时自己也要多思考~~~~~~~~
偶自己的代码如下:

点击(此处)折叠或打开

  1. import os
  2. from time import sleep
  3. from Tkinter import *

  4. class DirList(object):
  5.     def __init__(self, initdir = None):
  6.         self.top = Tk()
  7.         self.label = Label(self.top,text="Driectory List V1.1")
  8.         self.label.pack()
  9.         self.cwd = StringVar(self.top)
  10.         self.dirlabel = Label(self.top,fg='blue',font=('Helvetica',12,'bold'))
  11.         self.dirlabel.pack()
  12.         #comment add by kinfinger
  13.         self.dirfm =Frame(self.top)
  14.         self.dirfm.pack()  # 被丢失的代码
  15.         self.dirsb=Scrollbar(self.dirfm)
  16.         self.dirsb.pack(side=RIGHT,fill=Y)
  17.         self.dirs =Listbox(self.dirfm,height =15,width= 50,yscrollcommand=self.dirsb.set)
  18.         self.dirs.bind('',self.setDirAndGo)
  19.         self.dirsb.config(command = self.dirs.yview)
  20.         self.dirs.pack(side = LEFT,fill = BOTH)

  21.         self.dirn = Entry(self.top,width = 50,textvariable =self.cwd)
  22.         self.dirn.bind('',self.doLS)
  23.         self.dirn.pack()

  24.         self.bfm= Frame(self.top)
  25.         self.clr =Button(self.bfm,text ='Clear',command = self.clrDir,activeforeground ='white',activebackground ='blue',)
  26.         self.ls =Button(self.bfm,text ='List Directory',command =self.doLS, activeforeground ='white',activebackground ='green')
  27.         self.quit =Button(self.bfm,command=self.top.quit,text= 'quit',activeforeground ='white',activebackground ='red')
  28.         self.clr.pack(side = LEFT)
  29.         self.ls.pack(side = LEFT)
  30.         self.quit.pack(side = LEFT)
  31.         self.bfm.pack()

  32.         if initdir: #comment none
  33.             self.cwd.set(os.curdir)
  34.             self.doLS
  35.     def clrDir(self,ev=None):
  36.         self.cwd.set('')
  37.     def setDirAndGo(self,ev=None):
  38.         self.last = self.cwd.get()
  39.         self.dirs.config(selectbackground ='red')
  40.         check =self.dirs.get(self.dirs.curselection())
  41.         if not check:
  42.             check = os.curdir
  43.         self.cwd.set(check)
  44.         self.doLS()
  45.     def doLS(self,ev=None):
  46.         error = ''
  47.         tdir = self.cwd.get()
  48.         if not tdir: tdir = os.curdir

  49.         if not os.path.exists(tdir):
  50.             error = tdir + ': no such file'
  51.         elif not os.path.isdir(tdir):
  52.             error =tdir +':not a directory'

  53.         if error:
  54.             self.cwd.set(error)
  55.             self.top.update()
  56.             sleep(2)
  57.             if not (hasattr(self,'last')) \
  58.                 and self.last:
  59.                 self.last = os.curdir
  60.             self.cwd.set(self.last)
  61.             self.dirs.config(\
  62.             selectbackground ='LightSkyBlue')
  63.             self.top.update()
  64.             return

  65.         self.cwd.set(\
  66.             'fetch directory contents....'
  67.         )
  68.         self.top.update()
  69.         dirlist = os.listdir(tdir)
  70.         print dirlist
  71.         dirlist.sort()
  72.         if os.chdir(tdir):
  73.             print 'success'
  74.         else:
  75.             print tdir
  76.         print os.getcwd()+'+++++++++++'
  77.         self.dirlabel.config(text=os.getcwd())
  78.         self.dirs.delete(0,END)
  79.         self.dirs.insert(END,os.curdir)
  80.         print os.curdir+'not change'
  81.         for eachFile in dirlist:
  82.             self.dirs.insert(END,eachFile)
  83.             #print eachFile
  84.             self.cwd.set(os.curdir)
  85.             self.dirs.config(\
  86.             selectbackground ='LightSkyBlue')
  87. def main():
  88.     d=DirList(os.curdir)
  89.     mainloop()
  90. if __name__ == '__main__':
  91.     main()
同时为了显示中文:
做如下修改:
unicode(eachFile,'cp936')
效果如下:

关于编码:
ref: 
http://blog.csdn.net/zbyufei/article/details/5905816
阅读(9028) | 评论(6) | 转发(3) |
给主人留下些什么吧!~~

kinfinger2013-04-18 18:21:49

zhumao:你这一段代码,复制,粘贴,也不运行。。。
  File "a.py", line 15
SyntaxError: Non-ASCII character '\xe8' in file a.py on line 15, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
把中文注释删除就好了。。。。

是注释,这就是书上和网上所忘记的代码

回复 | 举报

kinfinger2013-04-18 18:20:19

Larpenteur:我对那个小低级的失误比较感兴趣,方便分享吗?

self.dirfm.pack()  # 被丢失的代码

回复 | 举报

hmfiwotn2013-04-18 11:34:08

路过看一下

hmfiwotn2013-04-18 11:33:28

路过,看一下

zhumao2013-04-18 10:51:00

你这一段代码,复制,粘贴,也不运行。。。
  File "a.py", line 15
SyntaxError: Non-ASCII character '\xe8' in file a.py on line 15, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
把中文注释删除就好了。。。。