1导入需要使用到的库
import tkinter as tk
import tkinter
from tkinter import Menu #导入菜单栏
import configparser
from tkinter import messagebox
import os
from tkinter.filedialog import *
import pyautogui, time
import pyautogui as pag
2 设置ini以便可以适应不同的电脑
#读取配置文件
config = configparser.ConfigParser()
config.read("zbconfig.ini")
#写入坐标配置文件
try:
config.add_section("check")
config.set("check","DJ_X","258")
config.set("check","DJ_Y","386")
config.add_section("Form type")
config.set("Form type","BD_X","920")
config.set("Form type","BD_Y","274")
config.add_section("cycle time")
config.set("cycle time","ZQ_X","594")
config.set("cycle time","ZQ_Y","340")
config.add_section("Check item")
config.set("Check item","DY_X","935")
config.set("Check item","DY_Y","430")
config.set("Check item","DE_X","936")
config.set("Check item","DE_Y","474")
config.set("Check item","DS_X","935")
config.set("Check item","DS_Y","518")
config.set("Check item","DSI_X","935")
config.set("Check item","DSI_Y","565")
config.set("Check item","DW_X","935")
config.set("Check item","DW_Y","614")
config.set("Check item","DWBZ_X","1110")
config.set("Check item","DWBZ_Y","610")
config.set("Check item","DL_X","934")
config.set("Check item","DL_Y","662")
config.set("Check item","DQ_X","935")
config.set("Check item","DQ_Y","710")
config.set("Check item","DQBZ_X","1120")
config.set("Check item","DQBZ_Y","707")
config.set("Check item","DB_X","930")
config.set("Check item","DB_Y","760")
config.set("Check item","DJIU_X","932")
config.set("Check item","DJIU_Y","810")
config.set("Check item","DH_X","934")
config.set("Check item","DH_Y","853")
config.add_section("Send out an audit")
config.set("Send out an audit","SCJH_X","736")
config.set("Send out an audit","SCJH_Y","899")
config.add_section("determine")
config.set("determine","QD_X","747")
config.set("determine","QD_Y","375")
config.add_section("daycheck")
config.set("determine", "XY_X", "930")
config.set("determine", "XY_Y", "426")
config.set("determine", "X1_X", "935")
config.set("determine", "X1_Y", "474")
config.set("determine", "X2_X", "935")
config.set("determine", "X2_Y", "474")
config.set("determine", "X3_X", "935")
config.set("determine", "X3_Y", "474")
except configparser.DuplicateSectionError:
print("Section 'zbconfig' already exists")
config.write(open("config.ini", "w"))
dj_x=config.getint("check","DJ_X")
dj_y=config.getint("check","DJ_Y")
bd_x=config.getint("Form type","BD_X")
bd_y=config.getint("Form type","BD_Y")
zq_x=config.getint("cycle time","ZQ_X")
zq_y=config.getint("cycle time","ZQ_Y")
dy_x=config.getint("Check item","DY_X")
dy_y=config.getint("Check item","DY_Y")
de_x=config.getint("Check item","DE_X")
de_y=config.getint("Check item","DE_Y")
ds_x=config.getint("Check item","DS_X")
ds_y=config.getint("Check item","DS_Y")
dsi_x=config.getint("Check item","DSI_X")
dsi_y=config.getint("Check item","DSI_Y")
dw_x=config.getint("Check item","DW_X")
dw_y=config.getint("Check item","DW_Y")
dwbz_x=config.getint("Check item","DWBZ_X")
dwbz_y=config.getint("Check item","DWBZ_Y")
dl_x=config.getint("Check item","DL_X")
dl_y=config.getint("Check item","DL_Y")
dq_x=config.getint("Check item","DQ_X")
dq_y=config.getint("Check item","DQ_Y")
dqbz_x=config.getint("Check item","DQBZ_X")
dqbz_y=config.getint("Check item","DQBZ_Y")
db_x=config.getint("Check item","DB_X")
db_y=config.getint("Check item","DB_Y")
djiu_x=config.getint("Check item","DJIU_X")
djiu_y=config.getint("Check item","DJIU_Y")
dh_x=config.getint("Check item","DH_X")
dh_y=config.getint("Check item","DH_Y")
scjh_x=config.getint("Send out an audit","SCJH_X")
scjh_y=config.getint("Send out an audit","SCJH_Y")
qd_x=config.getint("determine","QD_X")
qd_y=config.getint("determine","QD_Y")
xy_x=config.getint("daycheck","XY_X")
xy_y=config.getint("daycheck","XY_Y")
x1_x=config.getint("daycheck","X1_X")
x1_y=config.getint("daycheck","X1_Y")
x2_x=config.getint("daycheck","X2_X")
x2_y=config.getint("daycheck","X2_Y")
x3_x=config.getint("daycheck","X3_X")
x3_y=config.getint("daycheck","X3_Y")
3 #获取鼠标位置
def get():
po.delete(0,tkinter.END)
time.sleep(2) #几秒后返回位置
x, y = pag.position()
po.insert(0,str(x)+ ',' +str(y))
4 写个点检项目的函数
#周点检
def dianjian_1():
n = int(kaishi_input.get())
m = int(jieshu_input.get())
for i in list(range(n, m+1)):
time.sleep(0.5)
pyautogui.click(dj_x, dj_y)
time.sleep(1)
if i <= 9:
pyautogui.typewrite('KTB0000%d'%(i))
elif i <= 99:
pyautogui.typewrite('KTB000%d'%(i))
elif i >= 100:
pyautogui.typewrite('KTB00%d'%(i))
pyautogui.press('enter')
pyautogui.press('enter')
time.sleep(1)
pyautogui.doubleClick(bd_x, bd_y)
time.sleep(0.5)
pyautogui.press('enter')
time.sleep(0.5)
pyautogui.keyDown('down')
time.sleep(0.5)
pyautogui.doubleClick(zq_x, zq_y)
pyautogui.keyDown('enter')
time.sleep(0.5)
pyautogui.keyDown('down')
pyautogui.keyDown('down')
pyautogui.keyDown('enter')
time.sleep(2.5)
pyautogui.click(dy_x, dy_y)
pyautogui.click(de_x, de_y)
pyautogui.click(ds_x, ds_y)
pyautogui.click(dsi_x, dsi_y)
pyautogui.click(dw_x, dw_y)
time.sleep(0.3)
pyautogui.click(dwbz_x, dwbz_y)
time.sleep(0.3)
pyautogui.typewrite('5V')
time.sleep(0.3)
pyautogui.click(dl_x, dl_y)
pyautogui.click(dq_x, dq_y)
time.sleep(0.3)
pyautogui.click(dqbz_x, dqbz_y)
time.sleep(0.3)
pyautogui.typewrite('NA')
time.sleep(0.3)
pyautogui.click(db_x, db_y)
time.sleep(0.1)
pyautogui.click(djiu_x, djiu_y)
time.sleep(0.1)
pyautogui.click(dh_x, dh_y)
time.sleep(0.1)
pyautogui.click(scjh_x, scjh_y)
time.sleep(1.5)
pyautogui.click(qd_x, qd_y)
time.sleep(3)
if i == m:
tk.messagebox.showinfo("提示", "已点检完成")
#日点检
def dianjian_2():
n = int(kaishi_input.get())
m = int(jieshu_input.get())
for i in list(range(n, m + 1)):
time.sleep(0.5)
pyautogui.click(dj_x, dj_y)
time.sleep(1)
if i <= 9:
pyautogui.typewrite('KTB0000%d' % (i))
elif i <= 99:
pyautogui.typewrite('KTB000%d' % (i))
elif i >= 100:
pyautogui.typewrite('KTB00%d' % (i))
pyautogui.press('enter')
pyautogui.press('enter')
time.sleep(1)
pyautogui.doubleClick(bd_x, bd_y)
time.sleep(0.5)
pyautogui.press('enter')
time.sleep(0.5)
pyautogui.keyDown('down')
time.sleep(0.5)
pyautogui.doubleClick(zq_x, zq_y)
pyautogui.keyDown('enter')
time.sleep(0.5)
pyautogui.keyDown('down')
pyautogui.keyDown('enter')
time.sleep(2.5)
pyautogui.click(xy_x, xy_y)
pyautogui.click(x1_x, x1_y)
time.sleep(1)
pyautogui.click(x2_x, x2_y)
time.sleep(1)
pyautogui.click(x3_x, x3_y)
time.sleep(2.5)
if i == m:
tk.messagebox.showinfo("提示", "已点检完成")
5 配置GUI界面
win = tk.Tk()
win.title(“EMS Automatic Check”)
win.geometry(‘450x350’)
def _quit():
“”“结束主事件循环”""
win.quit()
win.destroy()
exit()
#创建菜单栏工单
menuBar = Menu(win)
win.config(menu=menuBar)
在菜单栏中创建一个名为Help的菜单项
def bangzhu():
tk.messagebox.showinfo(“help”, “1 不同显示器需要更新坐标\n2 坐标值填在zbconfig文件里面\n获取坐标方法\n先点击获取,然后点击需要获取坐标的地方”)
helpMenu = Menu(menuBar, tearoff=0)
menuBar.add_command(label=“Help”, command=bangzhu)
在菜单栏中创建一个名为About的菜单项
def About():
tk.messagebox.showinfo(“关于”, “version:0.0.03\nAuthor:kevin_li”)
aboutMenu = Menu(menuBar, tearoff=0)
menuBar.add_command(label=“About”, command=About)
canvas = tk.Canvas(win, height=200, width=500)#创建画布
image_file = tk.PhotoImage(file=‘welcome.gif’)#加载图片文件
image = canvas.create_image(0,0, anchor=‘nw’, image=image_file)#将图片置于画布上
canvas.pack(side=‘top’)#放置画布(为上端)
kaishi_text = IntVar
kaishi_lb = Label(win,text=‘开始ID:’,font=(‘微软雅黑’,13))
kaishi_lb.place(x=50,y=150)
kaishi_input = Entry(win,width=15,textvariable=kaishi_text)
kaishi_input.place(x=110,y=155)
jieshu_text = IntVar
jieshu_lb = Label(win,text=‘结束ID:’,font=(‘微软雅黑’,13))
jieshu_lb.place(x=50,y=190)
jieshu_input = Entry(win,width=15,textvariable=jieshu_text)
jieshu_input.place(x=110,y=195)
#创建开始点检按钮
b3 = tk.Button(win,text=“周點檢”,width=12,height=3,command=dianjian_1)
b3.place(x=230,y=150)
b4 = tk.Button(win,text=“日點檢”,width=12,height=3,command=dianjian_2)
b4.place(x=330,y=150)
tip = tk.Label(win, text=“返回点击2s后的关标位置:”)
tip.place(x=30,y=240)
po = tk.Entry(win)
po.place(x=170,y=240)
do = tk.Button(win,text=“获取”,command=get) #点击获取位置
do.place(x=320,y=240)
win.mainloop()
6 成品界面