[Python] 纯文本查看 复制代码
import os
from tkinter import *
import tkinter
from tkinter import filedialog
master =Tk()
master.geometry("400x400+10+10")
f =Frame()
f.pack()
l = Label(f, text="显示的内容在这里展示")
l.pack(side=LEFT)
jieguo = Entry(f)
jieguo.pack()
f1 = Frame()
f1.pack()
etPath = StringVar()
f2 = Frame()
f2.pack()
f3 = Frame()
f3.pack()
#全局变量
device = []
path = ''
# package = "com.js.pharmacy2"
# activity = "com.js.pharmacy2.activity.LoginActivity'
package = "你的包名"
activity = "你启动的Activity名"
def callback(type): #必须跟一个参数
if type == 1: #输出的序列号中有\n\t
f = os.popen(r"adb devices", "r")
devices = f.read()
shuchu = devices.replace('List of devices attached', '')
f.close()
patter = re.compile('(.*?)' + 'device', re.S)
global device
device = patter.findall(shuchu)
# result 为值
jieguo.delete(0, END)
print(device)
if type == 2: #获取顶层Activity
for dName in device:
try:
dName = dName.replace('\n', '').replace('\t', '')
out = os.popen('adb -s ' + dName + ' shell dumpsys activity | grep mFocusedActivity', 'r').read()
print(dName + "的当前顶层应用是:" + out)
except:
print(dName + "关闭失败")
if type == 3:
for dName in device:
try:
dName = dName.replace('\n', '').replace('\t','')
# os.popen('adb -s ' + dName + ' shell am kill ' + package)
os.popen("adb -s" + dName + " shell am start -n " + package + '/' + activity)
print("启动成功")
except:
print(dName + "打开失败")
if type == 4:
for dName in device:
try:
dName = dName.replace('\n', '').replace('\t', '')
os.popen("adb -s" + dName + " shell am start -D -n " + package + '/' + activity)
print("启动成功")
except:
print(dName + "打开失败")
if type == 5:
for dName in device:
try:
dName = dName.replace('\n', '').replace('\t', '')
os.popen("adb -s" + dName + " shell am force-stop " + package)
print("关闭成功")
except:
print(dName + "关闭失败")
if type == 6:
for dName in device:
try:
dName = dName.replace('\n', '').replace('\t', '')
f = os.popen(r"adb -s" + dName + " install " + path, 'r')
result = f.read()
print("安装成功")
except:
print(dName + "安装失败")
if type == 7:
for dName in device:
try:
dName = dName.replace('\n', '').replace('\t', '')
os.popen("adb -s" + dName + " uninstall " + package)
print("卸载成功")
except:
print(dName + "卸载失败")
def selectPath():
global path
path = tkinter.filedialog.askopenfilename()
etPath.set(path)
class Application(Frame):
b = Button(f, text='查询手机序号', command=lambda: callback(1))
b.pack(side=LEFT)
b = Button(f, text='获取最上层Activity', command=lambda: callback(2))
b.pack(side=LEFT)
b = Button(f1, text='打开app', command=lambda: callback(3))
b.pack(side=LEFT)
b = Button(f1, text='调试app', command=lambda: callback(4))
b.pack(side=LEFT)
b = Button(f1, text='关闭app', command=lambda: callback(5))
b.pack(side=LEFT)
Label(f2, text='目标路径:').grid(row=0, column=0)
Entry(f2, textvariable=etPath).grid(row=0, column=1)
Button(f2, text='路径选择', command=selectPath).grid(row=0, column=2)
b = Button(f1, text='关闭app', command=lambda: callback(5))
b.pack(side=LEFT)
Label(f2, text='目标路径:').grid(row=0, column=0)
Entry(f2, textvariable=etPath).grid(row=0, column=1)
Button(f2, text='路径选择', command=selectPath).grid(row=0, column=2)
b = Button(f3, text='安装apk', command=lambda: callback(6))
b.pack(side=LEFT)
b = Button(f3, text='卸载应用', command=lambda: callback(7))
b.pack(side=LEFT)
app = Application()
# 设置窗口标题
app.master.title('Hello World')
# 主消息循环
app.mainloop()