python一般用来写纯脚本的居多,但也可以做有视图的产品出来,例如做网页和客户端工具。做成工具的好处是,让不懂代码的人也能使用,不需要去修改代码里面的参数,如果使用次数频繁,甚至比纯脚本跟节约时间;最大的好处是打包后可以运行在任何没有安装脚本中第python三方包的电脑上,也可以运行在任何没有安装python的电脑上。下面上我很久之前的tkinter做的第一个测试工具的源代码。

此部分是tkinter代码,直接运行代码,就可以弹出界面。

 # -*- coding: utf-8 -*-
from Tkinter import *
from PIL import Image, ImageTk
import urllib
import urllib2
#import http.cookiejar
import base64
import cookielib,time,sys
#import mysql.connector
import MySQLdb class post(): ##########这种cookie方式略微复杂些,脚本写的太早了,是urllib2,用requests会简化很多
def __init__(self,values):
self.values=values
def qinqiu(self):
#从文件中获取cookie并访问
cookie = cookielib.MozillaCookieJar()
#从文件中读取cookie内容到变量
cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True)
#利用urllib2的build_opener方法创建一个opener
#print 'saddsafsdafdsf',cookie
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) url ="http://www.xxxx.cn/test/yuanzhao/controls/api.php" data = base64.b64encode(self.values) #######视情况而定,一般公司的接口参数并不需要base64进行编码
#print data #
req = urllib2.Request(url, data)
response = opener.open(req)
the_page = response.read()
print the_page
return the_page class select(): ####可以不用mysql,直接请求特定的某些接口,取出我们需要的返回值也可以。
def __init__(self,sql):
self.sql=sql
def chaxun(self):
conn=MySQLdb.connect(host='112.xx.xx.xx',user='root',passwd='*************',db='ld_logistic_sys_******',port=3307)
cur=conn.cursor()
try:
#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)
sql=self.sql
cur.execute(sql)
data = cur.fetchall()
except:
print u'没查询出来' #print data
cur.close()
conn.close()
return data def tanchu(str1,height): #####从tkinter主界面弹出
top = Toplevel()
h=int(height)
top.title('提示')
Label(top, text=str1,fg='yellow',bg='red').pack()
top.geometry('400x'+str(h)+'+700+500')
return #tanchu('hello',500) def fun2(): ydgs1=ydgs.get()
if ydgs1=='':
print u'请填写运单个数'
tanchu(u'请填写运单个数',100)
return
print "创建%s个运单"%(ydgs1) if ydnx.get()==u'货物托运凭证':
ydnx1=0
elif ydnx.get()==u'吊装受理开单':
ydnx1=1
else:
ydnx1=2
#print ydnx1 if jsfs.get()==u'现付':
jsfs1=0
elif jsfs.get()==u'提付':
jsfs1=1
elif jsfs.get()==u'回单付':
jsfs1=2
else:
jsfs1=3
#print jsfs1 yundanneixin=ydnx1
jiesuanfangshi=jsfs1
yundangeshu=ydgs1
fhkhid1=fhkhid.get()
str1='创建的运单'
for i in range (int(yundangeshu)):
#创建运单 values = '{"opFrom":"1","userName":"testy","psd_v":"0","waybillType":"'+str(yundanneixin)+'","originatingStation":"25","destinationStation":"27","goodsNO":"名称很长的长货号a1",\
"deliveClientName":"'+str(fhkhid1)+'","deliveClientPhone":"13156983698","deliveClientAdd":"财务发货客户1的详细地址财务发货客户1的详细地址","receiptClientName":"101",\
"receiptClientPhone":"13698563214","receiptClientAdd":"财务收货客户2的详细地址财务收货客户2的详细地址财务收货客户2的详细地址","settleAccountType":"'+str(jiesuanfangshi)+'","receivable":40,\
"buisnessList":[{"buisnessType":"业务类型1业务类型1","articleName":"长品名长品名长品名长","articleNO":"长品号长品号长品号长","count":"1","weight":"1.11","volume":"1.12",\
"pack":"长包装1长包装1长包","freight":"1.13","handlingCharges":"1.14","transportationCharges":"1.15","delayUnloadCharges":"2","advanceCharges":"3",\
"other":"5.22","tax":"12.32%","paymentDay":"一年165天12个月"},{"buisnessType":"业务类型2业务类型2","articleName":"长2名长品名长品名长","articleNO":"长2号长品号长2号长",\
"count":"2","weight":"11","volume":"","pack":"","freight":"5","handlingCharges":"",\
"transportationCharges":"6","delayUnloadCharges":"","advanceCharges":"","other":"",\
"tax":"","paymentDay":"一年265天12个月"},{"buisnessType":"","articleName":"","articleNO":"品号3",\
"count":"","weight":"","volume":"","pack":"","freight":"",\
"handlingCharges":"","transportationCharges":"","delayUnloadCharges":"","advanceCharges":"","other":"","tax":"","paymentDay":""},\
{"buisnessType":"业务类型4业务类型4","articleName":"","articleNO":"","count":"","weight":"","volume":"","pack":"长包装4长包装4长包",\
"freight":"7.36","handlingCharges":"","transportationCharges":"8","delayUnloadCharges":"","advanceCharges":"","other":"","tax":"",\
"paymentDay":""}],"receipted":10,"settler":"18","receiptStatus":0,"performance":"","remark":"运单的备注中新网10月13日电 据法新社13日报道\
,联合国大会正式任","method":"new_waybill"}' cjyd=post(values)
the_page=cjyd.qinqiu()
str1=str1+the_page #print str1
tanchu(str1,400) ########################################################调度函数####################################################
def diaodu():
if ddjsfs.get()==u'现付':
jiesuanfangshi=0
elif ddjsfs.get()==u'回单付':
jiesuanfangshi=2
else:
jiesuanfangshi=3 #print jiesuanfangshi global ydksid
global ydjsid
if ydksid.get()=='' :
tanchu(u'请填写运单结开始和结束id',100)
print u'请填写运单结开始和结束id' return
if ydjsid.get()=='' :
print u'请填写运单开始和结束id' top = Toplevel()
top.title('提示')
Label(top, text='请填写运单开始和结束id!').pack()
top.geometry('400x300+700+500')
return ydksid1=int(ydksid.get())
ydjsid1=int(ydjsid.get())+1 driverid=jsyid.get()
gongyinshangid=gysid.get()
vehicleid=cphid.get() print u'调度'
for i in range(ydksid1,ydjsid1): if jiesuanfangshi==0: ####运单的结算方式为0是现付,请求参数会有少量不同 values='{"opFrom":"1","userName":"ydf","psd_v":"0","supplierCompany":"'+str(gongyinshangid)+'","predictArriveDate":"2016-10-20","driverID":"'+str(driverid)+'","vehicleID":"'+str(vehicleid)+'","dispatchNO":\
"调度号很长很很长的aaaa111111","dispatchCharges":"5","packCharges":0,"handlingCharges":"2.22","forkliftCharges":0,"otherCharges":"4.44","settleType":"'+str(jiesuanfangshi)+'","settleStatus":\
"0","dispatchTotalCharges":"11.66","paidDispatchTotalCharges":"11.66","receivableKickback":"9.99","paidKickback":"9.99","settler":"32",\
"remark":"http://www.xxxx.cn/test/yuanzhao/admin/view/index.html#/dispatch/dispatchlistdispatch/522","waybillIDList":["'+str(i)+'"],"method":"add_dispatch"}'
else:
values='{"opFrom":"1","userName":"ydf","psd_v":"0","supplierCompany":"'+str(gongyinshangid)+'","predictArriveDate":"2016-10-20","driverID":"'+str(driverid)+'","vehicleID":"'+str(vehicleid)+'","dispatchNO":\
"调度号很长很很长的aaaa111111","dispatchCharges":"5","packCharges":0,"handlingCharges":"2.22","forkliftCharges":0,"otherCharges":"4.44","settleType":"'+str(jiesuanfangshi)+'","settleStatus":\
"0","dispatchTotalCharges":"11.66","paidDispatchTotalCharges":"6.66","receivableKickback":"9.99","paidKickback":"8","settler":"32",\
"remark":"http://www.xxxx.cn/test/yuanzhao/admin/view/index.html#/dispatch/dispatchlistdispatch/522","waybillIDList":["'+str(i)+'"],"method":"add_dispatch"}' #print values
dd=post(values)
the_page=dd.qinqiu() if '{"errorCode":"0","errorMsg":"","dispatchID":' in the_page:
tanchu(u'调度成功',100) ##################################根据运单id查询运单号函数############################################################
def idchayundan():
global dyydh
id=cxdydid.get()
if id=='':
tanchu(u'请输入运单id',100)
return
sql='select waybillCode from bus_waybill where ID='+str(id) idcyd=select(sql)
the_page=idcyd.chaxun() print the_page dyydh1=the_page[0][0]
dyydhe.set(dyydh1) def fahuo():
yundanhao=fhydh.get()
devicename=imei.get()
if yundanhao=='':
tanchu(u'请填写运单号',100)
print (u'请填写运单号')
return print u'发货' values = '{"deviceName":"'+str(devicename)+'","list":[{"waybillNO":"'+str(yundanhao)+'"}],"method":"deliver","opFrom":"0","psd_v":"0","userName":"testy"}'
fh=post(values)
the_page=fh.qinqiu()
if '"errorCode":"0"' in the_page:
tanchu(u'发货成功',100) def faduanxin():
yundanhao=fdxydh.get()
if yundanhao=='':
tanchu(u'请输入运单号',100)
return
print u'发短信' values = '{"waybillNO":"'+str(yundanhao)+'","method":"send_msg","opFrom":"0","psd_v":"0","userName":"testy"}' ####调用此接口会发送一条短信给收货人
fdx=post(values)
fdx.qinqiu() def shouhuo():
yundanhao=shydh.get()
if yundanhao=='':
tanchu(u'请输入运单号',100)
return
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)
shsql=select(sql)
data=shsql.chaxun() #print data jsfs=data[0][0]
ispaid=0
if jsfs==0:
ispaid=1
elif jsfs==1:
ispaid=1
elif jsfs==2:
ispaid=0
elif jsfs==3:
ispaid=0
#print ispaid rece=data[0][1]
#print rece print u'收货' values = '{"consignee":"呃呃","imgUrl":"http://www.xxxx.cn/test/yuanzhao/controls/image/IMG_20160621_163048.jpg",\
"isPaid":"'+str(ispaid)+'","receivableMoney":"'+str(rece)+'","waybillNO":"'+str(yundanhao)+'","method":"get_receive_detail","opFrom":"0","psd_v":"0","userName":"testy"}' shpost=post(values)
the_page=shpost.qinqiu() if '"errorCode":"0"' in the_page: tanchu('收货成功',100) ######################################查询运单状态函数********************************'''
def chaxunyundanzt():
global ydzte
global cxydh cxydh1=cxydh.get()
if cxydh1=='':
print u'请输入运单号' tanchu(u'请输入运单号',100)
return #print cxydh1
sql='select status,ID from bus_waybill where waybillCode='+str(cxydh1)
cxydh2=select(sql)
data=cxydh2.chaxun() print data data3=data[0][1]
print data3,'*********' data1='' if data[0][0]==1:
data1=u'待调度'
elif data[0][0]==2:
data1=u'待发货'
elif data[0][0]==3:
data1=u'在途'
elif data[0][0]==4:
data1=u'待签收'
elif data[0][0]==5:
data1=u'待结算'
elif data[0][0]==6:
data1=u'完成' ydzte.set(data1)
yddid.set(data3) '''***********************************************tkinter**********************************************************************************************************************************************************************************************'''
root = Tk()
root.geometry('580x680+200+100')
root.resizable(width = False, height = False)
root.title("远兆")
root.iconbitmap('yuanzhao.ico') #创建运单 ***********************************************创建运单*********************************************************************
lb=Label(root,text = u'创建运单',fg = 'purple',bg = 'green',width=77)
lb.pack(side = LEFT,padx = 0,pady = 0)
lb.place(x=20,y=5) lb=Label(root,text = u'创建',fg = 'red')
lb.pack()
lb.place(x=20,y=40) e = StringVar() #运单个数
ydgs = Entry(root,width = 5,textvariable = e,bg='#FFD700')
ydgs.pack()
e.set('')
ydgs.place(x = 50, y = 40) lb=Label(root,text = u'个运单类型为',fg = 'red')
lb.pack()
lb.place(x=90,y=40) ydnx = StringVar(root) #运单类型,
ydnx.set(u'吊装受理开单证')
om = OptionMenu(root,ydnx,u'货物托运凭证',u'吊装受理开单',u'其他受理开单')
om.pack()
om.place(x=170,y=30) lb=Label(root,text = u'结算方式为',fg = 'red')
lb.pack()
lb.place(x=300,y=40) jsfs = StringVar(root) #结算方式
jsfs.set('回单付')
om = OptionMenu(root,jsfs,u'现付',u'提付',u'回单付',u'月付')
om.pack()
om.place(x=370,y=30) lb=Label(root,text = u'发货客户id为',fg = 'red')
lb.pack()
lb.place(x=450,y=40) e = StringVar() #发货客户id
fhkhid = Entry(root,width = 5,textvariable = e,bg='#FFD700')
fhkhid.pack()
e.set('')
fhkhid.place(x = 525, y = 38) lb=Label(root,text = u'的运单',fg = 'red')
lb.pack()
lb.place(x=20,y=70) button = Button(root,text = u"确定创建",fg = 'blue',bg = '#00bfff',width = 8,height = 1,command = fun2)
button.pack(side = RIGHT)
button.place(x = 500,y = 62) #调度 ***********************************************调度*********************************************************************
lb=Label(root,text = u'调度运单',fg = 'purple',bg = 'green',width=77)
lb.pack(side = LEFT,padx = 0,pady = 0)
lb.place(x=20,y=90) lb=Label(root,text = u'调度运单的id从',fg = 'red')
lb.pack()
lb.place(x=20,y=120) e = StringVar() #运单开始id
ydksid = Entry(root,width = 6,textvariable = e)
ydksid.pack()
#ydgs.set('10')
ydksid.place(x = 110, y = 120) lb=Label(root,text = u'至',fg = 'red')
lb.pack()
lb.place(x=150,y=120) e = StringVar() #运单结束始id
ydjsid = Entry(root,width = 6,textvariable = e)
ydjsid.pack()
#ydgs.set('10')
ydjsid.place(x = 170, y = 120) lb=Label(root,text = u'调度的供应商id为',fg = 'red')
lb.pack()
lb.place(x=250,y=120) e = StringVar() #供应商id
gysid = Entry(root,width = 4,textvariable = e,bg='#FFD700')
gysid.pack()
e.set('')
gysid.place(x = 360, y = 120) lb=Label(root,text = u'调度的结算方式为',fg = 'red')
lb.pack()
lb.place(x=20,y=150) ddjsfs = StringVar(root) #调度结算方式
ddjsfs.set('现付')
om = OptionMenu(root,ddjsfs,'现付','回单付','月付')
om.pack()
om.place(x=120,y=140) lb=Label(root,text = u'驾驶员id为',fg = 'red')
lb.pack()
lb.place(x=220,y=150) e = StringVar() #驾驶员id
jsyid = Entry(root,width = 4,textvariable = e,bg='#FFD700')
jsyid.pack()
e.set('')
jsyid.place(x = 290, y = 150) lb=Label(root,text = u'车牌号id为',fg = 'red')
lb.pack()
lb.place(x=350,y=150) e = StringVar() #车牌号id
cphid = Entry(root,width = 4,textvariable = e,bg='#FFD700')
cphid.pack()
e.set('')
cphid.place(x = 420, y = 150) button = Button(root,text = u"确定调度",fg = 'blue',bg = '#00bfff',width = 8,height = 1,command = diaodu)
button.pack(side = RIGHT)
button.place(x = 500,y = 145) #################################id查运单号#################################################
lb=Label(root,text = u'id为',fg = 'purple')
lb.pack(side = LEFT,padx = 0,pady = 0)
lb.place(x=20,y=180) e = StringVar() #运单id
cxdydid= Entry(root,width = 6,textvariable = e)
cxdydid.pack()
#ydgs.set('10')
cxdydid.place(x = 50, y = 180) lb=Label(root,text = u'的运单号是',fg = 'purple')
lb.pack(side = LEFT,padx = 0,pady = 0)
lb.place(x=90,y=180) dyydhe = StringVar() #对应运单号
dyydh= Entry(root,width = 16,textvariable = dyydhe,fg='#006400',bg='tan')
dyydh.pack()
#ydgs.set('10')
dyydh.place(x = 155, y = 180) '''
ydzte = StringVar() #运单号的运单状态
ydzt = Entry(root,width = 8,textvariable = ydzte,fg='green')
ydzt.pack()
#ydgs.set('10')
ydzt.place(x = 410, y = 490)
''' button = Button(root,text = u"点击查询",fg = 'blue',bg = '#00ffff',width = 8,height = 1,command = idchayundan)
button.pack(side = RIGHT)
button.place(x = 273,y = 175) ##############################################################发货##############################################
lb=Label(root,text = u'发货',fg = 'purple',bg = 'green',width=77)
lb.pack(side = LEFT,padx = 0,pady = 0)
lb.place(x=20,y=230) lb=Label(root,text = u'发货的运单号',fg = 'red')
lb.pack()
lb.place(x=20,y=260) e = StringVar() #发货的运单号
fhydh= Entry(root,width = 16,textvariable = e)
fhydh.pack()
#ydgs.set('10')
fhydh.place(x = 110, y = 260) lb=Label(root,text = u'发货手机的imei',fg = 'red')
lb.pack()
lb.place(x=250,y=260) e = StringVar() #发货的手机imei
imei = Entry(root,width = 19,textvariable = e,bg='#FFD700')
imei.pack()
e.set('imei861483035137583')
imei.place(x = 350, y = 260) button = Button(root,text = u"确定发货",fg = 'blue',bg = '#00bfff',width = 8,height = 1,command = fahuo)
button.pack(side = RIGHT)
button.place(x = 500,y = 255) ####################################################发短信###################################################
lb=Label(root,text = u'发短信',fg = 'purple',bg = 'green',width=77)
lb.pack(side = LEFT,padx = 0,pady = 0)
lb.place(x=20,y=310) lb=Label(root,text = u'对',fg = 'red')
lb.pack()
lb.place(x=20,y=340) e = StringVar() #发短信的运单号
fdxydh = Entry(root,width = 16,textvariable = e)
fdxydh.pack()
#ydgs.set('10')
fdxydh.place(x = 50, y = 340) lb=Label(root,text = u'这个运单发短信',fg = 'red')
lb.pack()
lb.place(x=170,y=340) button = Button(root,text = u"确认发短信",fg = 'blue',bg = '#00bfff',width = 8,height = 1,command = faduanxin)
button.pack(side = RIGHT)
button.place(x = 500,y = 340) #################################################收货###############################################################
lb=Label(root,text = u'收货',fg = 'purple',bg = 'green',width=77)
lb.pack(side = LEFT,padx = 0,pady = 0)
lb.place(x=20,y=390) lb=Label(root,text = u'收货的运单号:',fg = 'red')
lb.pack()
lb.place(x=20,y=420) e = StringVar() #收货的运单号
shydh = Entry(root,width = 16,textvariable = e)
shydh.pack()
#ydgs.set('10')
shydh.place(x = 110, y = 420) button = Button(root,text = u"确认收货",fg = 'blue',bg = '#00bfff',width = 8,height = 1,command = shouhuo)
button.pack(side = RIGHT)
button.place(x = 500,y = 420) ##########'''********************************查询运单的状态*******************************'''
lb=Label(root,text = u'查询运单状态',fg = 'purple',bg = 'green',width=77)
lb.pack(side = LEFT,padx = 0,pady = 0)
lb.place(x=20,y=460) lb=Label(root,text = u'请输入运单号:',fg = 'purple')
lb.pack()
lb.place(x=20,y=490) e = StringVar() #查询的运单号
cxydh = Entry(root,width = 16,textvariable = e)
cxydh.pack()
#ydgs.set('10')
cxydh.place(x = 110, y = 490) button = Button(root,text = u"点击查询",fg = 'blue',bg = '#00ffff',width = 8,height = 1,command = chaxunyundanzt)
button.pack(side = RIGHT)
button.place(x = 273,y = 485) lb=Label(root,text = u'运单状态为:',fg = 'purple')
lb.pack()
lb.place(x=340,y=490) ydzte = StringVar() #运单号的运单状态
ydzt = Entry(root,width = 8,textvariable = ydzte,fg='#006400',bg='tan')
ydzt.pack()
#ydgs.set('10')
ydzt.place(x = 410, y = 490) lb=Label(root,text = u'id为:',fg = 'purple')
lb.pack()
lb.place(x=470,y=490) yddid = StringVar() #运单号id
et = Entry(root,width = 8,textvariable = yddid,fg='#006400',bg='tan')
et.pack()
#ydgs.set('10')
et.place(x = 505, y = 490) '''*********************底部图片**********************'''
img = ImageTk.PhotoImage(file='yuanzhao.png')
lb=Label(root, text="abc", image=img)
lb.pack(side="top")
lb.place(x=0,y=540) root.mainloop()
最后随便上两个运行时的截图。

python使用tkinter写带界面的工具-LMLPHP

python使用tkinter写带界面的工具-LMLPHP

此工具的接口使用项目的抓包接口或者参考协议文档。做这个工具的目的是,把系统主流程功能全部都放在一个界面上,因为系统分为web erp管理端和app扫码发货 收货端,每个端都是十几个页面,来回的切换页面测试,特别是web端的运单和调度需要填写大量的页面表单是很废时间和令人狂躁的一件事情。

从代码中可以看到,tkinter的代码并不难就那几句话,后台脚本写出来了后,构建工具界面大概要多花半天时间。功能的触发是通过按钮与函数绑定,点击什么按钮就执行什么函数功能,传统的脚本方式操作想执行一个函数时候,需要把调用另一个函数的代码注释掉,这也是界面工具的一个优势吧,点什么执行什么。
上面的界面工具是直接运行代码弹出来的,脚本的文件名称是yuanzhao.py,如果想做成exe客户端,需要写另一个文件,我们取名为setup.py,其中代码如下。

 from distutils.core import setup
import py2exe #setup(windows=['yuanzhao.py']) setup(
# targets to build
windows = [{"script":"yuanzhao.py", "icon_resources": [(0, "yuanzhao.ico")]} ]
)
运行  python setup.py py2exe,就能打包成功了,此时路径下会生成两个文件夹,

python使用tkinter写带界面的工具-LMLPHP

我们来看看dist文件夹就可以了。此文件就是工具的使用路径,

python使用tkinter写带界面的工具-LMLPHP

这样就能把dist文件夹放到任意没安装py脚本中第三方包或者没安装python的电脑运行测试工具了。

05-11 09:36
查看更多