显示标签为“python”的博文。显示所有博文
显示标签为“python”的博文。显示所有博文

Using the Mouse Wheel with Tkinter (Python)

  1. # explore the mouse wheel with the Tkinter GUI toolkit
  2. # Windows and Linux generate different events
  3. # tested with Python3.0
  4.  
  5. import tkinter as tk
  6.  
  7. def mouse_wheel(event):
  8. global count
  9. # respond to Linux or Windows wheel event
  10. if event.num == 5 or event.delta == -120:
  11. count -= 1
  12. if event.num == 4 or event.delta == 120:
  13. count += 1
  14. label['text'] = count
  15.  
  16. count = 0
  17. root = tk.Tk()
  18. root.title('turn mouse wheel')
  19. root['bg'] = 'darkgreen'
  20.  
  21. # with Windows OS
  22. root.bind("", mouse_wheel)
  23. # with Linux OS
  24. root.bind("", mouse_wheel)
  25. root.bind("", mouse_wheel)
  26.  
  27. label = tk.Label(root, font=('courier', 18, 'bold'), width=10)
  28. label.pack(padx=40, pady=40)
  29.  
  30. root.mainloop()

tkinter响应键盘事件

from tkinter import *
root = Tk()

def printCoords(event):
print ('event.char = ',event.char)
print ('event.keycode = ',event.keycode)

# 创建一个Button,并将它与BackSpace键绑定
bt = Button(root,text = 'Press Key')
bt.bind('',printCoords)

# 将焦点设置到Button上
bt.focus_set()
bt.grid()
root.mainloop()

linecache模块 enumerate函数

从文件中得到指定行.

标准库提供的linecache模块提供了解决方案:

import linecache
theline = linecache.getline(thefilepath, desired_line_number)

在处理这一类的问题时,linecache总是可以做为一种方案

,尤其是要重复执行的时候,因为linecache提供的机制能防止重复进行一些无用的操作.当你使用完毕的时候 ,调用clearcache方法来清空使用的缓存.也可以使用checkcache确保对文件的修改正确的保存在磁盘上.
linecache读入并缓存给出文件的所有内容,所以假如文件非常的巨大,linecache可能要做更多的工作.如果这成为你程序的瓶颈,用循环处理可能会获得更好的效果:

def getline(thefilepath, desired_line_number):
if desired_line_number <>
for current_line_number, line in enumerate(open(thefilepath, 'rU')):
if current_line_number == desired_line_number-1: return line
return ''

需要注意的地方是enumerate从0开始计数,所以我们要在计算的时候减一.

enumerate每次回返回一个tuple, (index, value)
例子很简单:
s = ['abc', 'This is a test', 'Hello,
Python']
for i, line in enumerate(s):
print i+1, line

防止窗口改变大小


import Tkinter

top = Tkinter.Tk()
top.resizable( False,False) #resizable第一个参数为假,窗口横向不能改变大小,第二个参数是纵向的。
label = Tkinter.Label(top, text='tk.resizable(False, False)')
label.pack();
Tkinter.mainloop()

在python中使用中文



#coding:gbk
a=u'中华人民共和國' #字符串前面一定要加上字母u,不然会显示成乱码。
#最后一个字是繁体,其它都是简体。
import Tkinter
w=Tkinter.Tk()
b=Tkinter.Button(text=a,command=w.destroy)
b.pack()
w.mainloop()

运行结果如下:

下面是我从python documentation 中找到的资料
Codec | Aliases | Languages

代码 别名 语言
gb2312 chinese, csiso58gb231280, euc-cn, Simplified Chinese(简体中文)
euccn, eucgb2312-cn, gb2312-1980,
gb2312-80, iso-ir-58
gbk 936, cp936, ms936 Unified Chinese(统一的中文)

如果把上面的 #coding:gbk 改成 #coding:gb2312 程序运行就会出错,因为字符串a的最后一个字符是繁体字,其它都是简体中文。我们还可以使用Aliases里面的别名,比如 #coding:936 或 #coding:chinese(只能使用简体中文)。

以上只是我经过试验得出的结论,如果有错误欢迎指正。

用python下载邮件附件

import poplib
import cStringIO
import email
import email.Header
import base64,osos.chdir('d:\\python')
#POP3取信
M = poplib.POP3('xxx.xxx.xxx)
M.user('xxxxxx')
M.pass_('xxxxxx')#打印有多少封信
numMessages = len(M.list()[1])
print 'num of messages', numMessages
for i in range(numMessages):
m = M.retr(i+1)

buf = cStringIO.StringIO()
for j in m[1]:
print >>buf, j
buf.seek(0)#解析信件内容
msg = email.message_from_file(buf)
for part in msg.walk():
contenttype = part.get('Content-Disposition')
filename=email.Header.decode_header(part.get_filename())
if filename and contenttype and contenttype[0:10]== 'attachment':
filename=os.path.basename(filename[0][0])
print contenttype[0:10],filename
f = open(filename,'wb')
f.write(part.get_payload(decode=True))
f.close()
M.quit()
print 'exit'