python一般用来写纯脚本的居多,但也可以做有视图的产品出来,例如做网页和客户端工具。做成工具的好处是,让不懂代码的人也能使用,不需要去修改代码里面的参数,如果使用次数频繁,甚至比纯脚本跟节约时间;最大的好处是打包后可以运行在任何没有安装脚本中第python三方包的电脑上,也可以运行在任何没有安装python的电脑上。下面上我很久之前的tkinter做的第一个测试工具的源代码。
此部分是tkinter代码,直接运行代码,就可以弹出界面。
1 # -*- coding: utf-8 -*- 2 from Tkinter import * 3 from PIL import Image, ImageTk 4 import urllib 5 import urllib2 6 #import http.cookiejar 7 import base64 8 import cookielib,time,sys 9 #import mysql.connector 10 import MySQLdb 11 12 13 14 15 class post(): ##########这种cookie方式略微复杂些,脚本写的太早了,是urllib2,用requests会简化很多 16 def __init__(self,values): 17 self.values=values 18 def qinqiu(self): 19 #从文件中获取cookie并访问 20 cookie = cookielib.MozillaCookieJar() 21 #从文件中读取cookie内容到变量 22 cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True) 23 #利用urllib2的build_opener方法创建一个opener 24 #print 'saddsafsdafdsf',cookie 25 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) 26 27 url ="http://www.xxxx.cn/test/yuanzhao/controls/api.php" 28 29 data = base64.b64encode(self.values) #######视情况而定,一般公司的接口参数并不需要base64进行编码 30 #print data # 31 req = urllib2.Request(url, data) 32 response = opener.open(req) 33 the_page = response.read() 34 print the_page 35 return the_page 36 37 38 class select(): ####可以不用mysql,直接请求特定的某些接口,取出我们需要的返回值也可以。 39 def __init__(self,sql): 40 self.sql=sql 41 def chaxun(self): 42 conn=MySQLdb.connect(host='112.xx.xx.xx',user='root',passwd='*************',db='ld_logistic_sys_******',port=3307) 43 cur=conn.cursor() 44 try: 45 #sql='select settlement_ID,payableAccount from bus_waybill,bus_waybill_fee where bus_waybill.waybillCode=bus_waybill_fee.waybillCode and bus_waybill.waybillCode='+str(yundanhao) 46 sql=self.sql 47 cur.execute(sql) 48 data = cur.fetchall() 49 except: 50 print u'没查询出来' 51 52 53 #print data 54 cur.close() 55 conn.close() 56 return data 57 58 59 def tanchu(str1,height): #####从tkinter主界面弹出 60 top = Toplevel() 61 h=int(height) 62 top.title('提示') 63 Label(top, text=str1,fg='yellow',bg='red').pack() 64 top.geometry('400x'+str(h)+'+700+500') 65 return 66 67 #tanchu('hello',500) 68 69 70 71 72 def fun2(): 73 74 ydgs1=ydgs.get() 75 if ydgs1=='': 76 print u'请填写运单个数' 77 tanchu(u'请填写运单个数',100) 78 return 79 print "创建%s个运单"%(ydgs1) 80 81 if ydnx.get()==u'货物托运凭证': 82 ydnx1=0 83 elif ydnx.get()==u'吊装受理开单': 84 ydnx1=1 85 else: 86 ydnx1=2 87 #print ydnx1 88 89 90 if jsfs.get()==u'现付': 91 jsfs1=0 92 elif jsfs.get()==u'提付': 93 jsfs1=1 94 elif jsfs.get()==u'回单付': 95 jsfs1=2 96 else: 97 jsfs1=3 98 #print jsfs1 99 100 yundanneixin=ydnx1 101 jiesuanfangshi=jsfs1 102 yundangeshu=ydgs1 103 fhkhid1=fhkhid.get() 104 str1='创建的运单' 105 for i in range (int(yundangeshu)): 106 #创建运单 107 108 values = '{"opFrom":"1","userName":"testy","psd_v":"0","waybillType":"'+str(yundanneixin)+'","originatingStation":"25","destinationStation":"27","goodsNO":"名称很长的长货号a1",\ 109 "deliveClientName":"'+str(fhkhid1)+'","deliveClientPhone":"13156983698","deliveClientAdd":"财务发货客户1的详细地址财务发货客户1的详细地址","receiptClientName":"101",\ 110 "receiptClientPhone":"13698563214","receiptClientAdd":"财务收货客户2的详细地址财务收货客户2的详细地址财务收货客户2的详细地址","settleAccountType":"'+str(jiesuanfangshi)+'","receivable":40,\ 111 "buisnessList":[{"buisnessType":"业务类型1业务类型1","articleName":"长品名长品名长品名长","articleNO":"长品号长品号长品号长","count":"1","weight":"1.11","volume":"1.12",\ 112 "pack":"长包装1长包装1长包","freight":"1.13","handlingCharges":"1.14","transportationCharges":"1.15","delayUnloadCharges":"2","advanceCharges":"3",\ 113 "other":"5.22","tax":"12.32%","paymentDay":"一年165天12个月"},{"buisnessType":"业务类型2业务类型2","articleName":"长2名长品名长品名长","articleNO":"长2号长品号长2号长",\ 114 "count":"2","weight":"11","volume":"","pack":"","freight":"5","handlingCharges":"",\ 115 "transportationCharges":"6","delayUnloadCharges":"","advanceCharges":"","other":"",\ 116 "tax":"","paymentDay":"一年265天12个月"},{"buisnessType":"","articleName":"","articleNO":"品号3",\ 117 "count":"","weight":"","volume":"","pack":"","freight":"",\ 118 "handlingCharges":"","transportationCharges":"","delayUnloadCharges":"","advanceCharges":"","other":"","tax":"","paymentDay":""},\ 119 {"buisnessType":"业务类型4业务类型4","articleName":"","articleNO":"","count":"","weight":"","volume":"","pack":"长包装4长包装4长包",\ 120 "freight":"7.36","handlingCharges":"","transportationCharges":"8","delayUnloadCharges":"","advanceCharges":"","other":"","tax":"",\ 121 "paymentDay":""}],"receipted":10,"settler":