|
|

楼主 |
发表于 2018-7-26 00:29:40
|
显示全部楼层
本帖最后由 sotmaomao 于 2018-7-27 00:45 编辑
#!/usr/bin/env python
import sys, os
from tkinter import *
import subprocess, threading, time
from tkinter import messagebox
register_app = []
def add_app(code=()):
global register_app
register_app.append(code)
add_app(('11', 'Safari.app'))
add_app(('22', 'Google Chrome.app'))
def callback(a):
scan_input=str(product.get())
idx = -2
for idy, app in enumerate(register_app):
if app[0].find(scan_input) == 0 and idx == -2:
idx = -1
if app[0] == scan_input:
idx = idy
if idx == -2:
product.delete(0, END)
product.focus_set()
messagebox.showinfo(message='Incorrect !')
elif idx >= 0:
product.delete(0, END)
product.focus_set()
p = subprocess.call(
["/usr/bin/open", "-g", "-a", register_app[idx][1]],) # open app
os.system("osascript -e 'activate application \"{}\"'".format(register_app[idx][1])) # show in front
for app in register_app: # close the other application or not close
if app[0] != scan_input:
os.system('kill $(ps -ax | grep "{}" | head -n 1 | awk \'{{print $1}}\')'.format(app[1]))
window = Tk()
window.geometry('450x300+400+200')
window.eval('tk::PlaceWindow %s center' % window.winfo_pathname(window.winfo_id()))
window.wm_attributes("-topmost",1)
window.title("123456")
window.config(bg='white')
label=Label(window,text='Please scan Product ID')
label.pack(side=TOP,padx=100,pady=90)
label.config(font=("Arial", 14))
# scan input value
product=Entry(window)
product.focus_set()
product.pack()
product.bind('<KeyRelease>', callback)
# window.focus_set()
def raise_app(root, product):
while True:
root.lift()
root.attributes("-topmost", True)
root.after(0, lambda: root.attributes("-topmost", False))
root.focus()
product.lift()
product.focus()
product.focus_force()
product.grab_set()
product.grab_release()
time.sleep(1)
threading.Thread(target=raise_app, args=(window,product)).start()
window.mainloop()
------------------------------------------------------------------------------------------
还请版主帮忙看看 万分感谢!
|
|