最近陆陆续续写了很多python的小脚本
记下一点东西方便以后查阅
#1.调用sqlite3数据库 con=sqlite3.connect(xxx) con.text_factory = str #防止中文乱码 cur=con.cursor() cur.execute("xx")#执行语句 con.commit()#写入数据库 #2.获取剪切板内容 def getText(): w.OpenClipboard() try: d = w.GetClipboardData(win32con.CF_TEXT) w.EmptyClipboard() w.CloseClipboard() return d except: w.CloseClipboard() return '' def setText(aString): w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_TEXT, aString) #如果是中文要重新编码 w.CloseClipboard() #3.随父线程结束的子线程 tee=threading.Thread(target=read) tee.setDaemon(True) #设置跟随父线程 tee.start() #4.调用dll from win32con import * from ctypes import * user32=windll.LoadLibrary('user32.dll') user32.MessageBoxW(0,'PASS!'.decode('utf-8'),'WIN'.decode('utf-8'),0|MB_SYSTEMMODAL) #5.全局键盘鼠标监听 多线程 hm = pyHook.HookManager() hm.KeyUp=ee #抬起函数 hm.KeyDown = onKeyboardEvent #按下函数 返回值均要为True,不然会死锁 hm.HookKeyboard() #开始监听 # pythoncom.PumpMessages()#循环获取消息 GUI中不需要 hm.UnhookKeyboard()#解除监听,不解除进程会假死,而且会无法关闭 #6.模拟键盘输入 from win32api import * keybd_event(76,0,0,0)#76为相应键码,按下 keybd_event(76,0,2,0)#抬起 def output(c=""): #输出字符串,这边符号只设置了底下几种,其他均没设置 for j in range(len(c)): i=str(c[j]) flag=0;t=0 if i.islower(): i=i.upper();t=ord(i) elif i.isupper(): flag=1;t=ord(i) elif i=='@': flag=1;t=ord('2') elif i=='_': flag=1;t=189 elif i=='-': t=189 elif i=='.': t=190 else: t=ord(i) if flag==1: keybd_event(16,0,0,0) keybd_event(t,0,0,0) keybd_event(t,0,2,0) if flag==1: keybd_event(16,0,2,0) #7.获取含某字符串的窗口hwnd hhwd=0;#hwnd def iterate(hwnd,nouse): global hhwd if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd): if GetWindowText(hwnd).find('Mozilla Firefox')!=-1: hhwd=hwnd return True EnumWindows(iterate, 0) #8.置顶窗口 windll.user32.SwitchToThisWindow(hwnd,True) #经测试这种最有效 #9.按hwnd关闭窗口 win32gui.SendMessage(hhwd,WM_CLOSE,0,0) #10.清空Text ok.delete(0.0,END) ok.insert(END,'123') #11.遍历文件夹并改名 def rename(): path=eny_path.get() for a,v,c in os.walk(path): t=len(str(int(len(c)/2))) for n in c: p=os.path.splitext(n) x=p[0] while len(x)<t: x='0'+x x=x+p[1] os.rename(os.path.join(a,n),os.path.join(a,x)) #12.利用喇叭蜂鸣…… winsound.Beep(1500,400) #13.注册主程序析构函数 def xxit(): pass atexit.register(xxit) #14.利用lamba给button等传递参数 Button(text='bad',command=lambda:change(1),hei=1,width=5) #15.置顶Tkinter窗口 root.wm_attributes('-topmost',1) #16.通过代理联网 from urllib import quote import cookielib, urllib2 class uu(): # 准备cookie cj = cookielib.LWPCookieJar() cookie_support = urllib2.HTTPCookieProcessor(cj) # 设置代理服务器 proxy_info = { 'host' : 'mengqidlufei.jks5.com' , 'port' :52385 } proxy_support = urllib2 . ProxyHandler ( { 'http' : \ 'http://%(host)s:%(port)d' % proxy_info } ) # 构造opener def __init__(self): opener = urllib2.build_opener(self.cookie_support, self.proxy_support) urllib2.install_opener(opener) # 打开网页 def open(self,url): # print quote(url,':/=?') page = urllib2.urlopen(quote(url,':/=?')) t= page.read() page.close() return t #17.修改系统时间,3倍 import os,time,subprocess def changetime(t): l=time.localtime(t) dat="date %u-%02u-%02u"%(l.tm_year,l.tm_mon,l.tm_mday) tm="time %02u:%02u:%02u"%(l.tm_hour,l.tm_min,l.tm_sec) subprocess.Popen(dat, shell=True) subprocess.Popen(tm, shell=True) print 'ok' def change(): t=time.time() while 1: t+=60 changetime(t) time.sleep(20) #18.py2exe win32的dll错误解决办法 from distutils.core import setup import py2exe,os includes = ["encodings", "encodings.*"] mfcfiles = [os.path.join(mfcdir, i) for i in ["mfc90.dll", "mfc90u.dll", "mfcm90.dll", "mfcm90u.dll", "Microsoft.VC90.MFC.manifest"]] data_files = [("Microsoft.VC90.MFC", mfcfiles),] #加入mfc文件 options = {"py2exe": { "compressed": 1, "optimize": 2, "includes": includes, # "bundle_files": 1 } } setup( version = "3.0", description = xxx, name = xxx, options = options, data_files = data_files, # zipfile=None, windows=[{"script": "v3.0.pyw" }], ) #19.PMW打包问题 #1.首先切换到源目录下:C:\Python27\Lib\site-packages\Pmw\Pmw_1_3_3\bin #2.然后执行 python bundlepmw.py C:\Python27\Lib\site-packages\Pmw\Pmw_1_3_3\lib (注意是lib) #3.然后会在bin下生成个Pmw.py (貌似2.0生成的有问题) #4.复制Pmw.py和lib里的PmwBlt.py和PmwColor.py到要打包的文件夹下 #5.按常规打包即可
时间: 2024-09-14 02:30:36