- # explore the mouse wheel with the Tkinter GUI toolkit
- # Windows and Linux generate different events
- # tested with Python3.0
- import tkinter as tk
- def mouse_wheel(event):
- global count
- # respond to Linux or Windows wheel event
- if event.num == 5 or event.delta == -120:
- count -= 1
- if event.num == 4 or event.delta == 120:
- count += 1
- label['text'] = count
- count = 0
- root = tk.Tk()
- root.title('turn mouse wheel')
- root['bg'] = 'darkgreen'
- # with Windows OS
- root.bind("
" , mouse_wheel) - # with Linux OS
- root.bind("
" , mouse_wheel) - root.bind("
" , mouse_wheel) - label = tk.Label(root, font=('courier', 18, 'bold'), width=10)
- label.pack(padx=40, pady=40)
- root.mainloop()
Using the Mouse Wheel with Tkinter (Python)
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('
# 将焦点设置到Button上
bt.focus_set()
bt.grid()
root.mainloop()
linecache模块 enumerate函数
| 从文件中得到指定行. 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) |
在python中使用中文
#coding:gbk 运行结果如下: 下面是我从python documentation 中找到的资料 如果把上面的 #coding:gbk 改成 #coding:gb2312 程序运行就会出错,因为字符串a的最后一个字符是繁体字,其它都是简体中文。我们还可以使用Aliases里面的别名,比如 #coding:936 或 #coding:chinese(只能使用简体中文)。 以上只是我经过试验得出的结论,如果有错误欢迎指正。 |
用python下载邮件附件
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'