简单记录,备忘
- 创建一个名为“hello”,大小为“800x600”的主窗口
import tkinter
root = tkinter.Tk()
root.title('hello')
root.geometry('800x600')
root.mainloop()
效果图:
2.主窗体上创建一个输出窗口
log = tkinter.Text(root,width=78,height=26,bd=0.02,font = ('汉隶',15),fg = 'blue')
log.place(relx = 0.01,rely =0.02)
效果图:
3.添加一个按钮
logcatbt = tkinter.Button(root,text= '获取logcat',width = 50,bd =0.5,bg = 'blue')
logcatbt.place(relx = 0.28,rely =0.92)
效果图:
4.创建获取logcat的方法
def logcat():
path = 'C:\\Users\\新建文件夹' #logcat保存路径
nowtime = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) #保存时间
filename = nowtime + ".txt" #保存名称
if not os.path.isdir(path + "\\logcat"):
os.makedirs(path + "\\logcat") #查询是否有logcat文件夹,没有便创建该文件夹
os.popen('adb logcat -v time > '+path+'/'+filename) #调用adb命令抓取log并保存
time.sleep(10) #抓取log时间
f = open(path+'/'+filename,'rb')
lines = f.read()
log.insert(END,lines) #将log文件在输出窗口打印
os.popen('adb kill-server')
效果图:
完整代码:
import tkinter
import os,time
from tkinter import *
root = tkinter.Tk()
root.title('hello')
root.geometry('800x600')
def devices():
A='adb get-state'
adb_device =os.popen(A).read().split()[0]
return adb_device
def logcat():
path = 'C:\\Users\\新建文件夹\\新建文件夹'
status = devices()
if status == 'device':
nowtime = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
filename = nowtime + ".txt"
if not os.path.isdir(path + "\\logcat"):
os.makedirs(path + "\\logcat")
# os.popen('adb logcat -c')
os.popen('adb logcat -v time > '+path+'/'+filename)
time.sleep(10)
f = open(path+'/'+filename,'rb')
lines = f.read()
log.insert(END,lines)
os.popen('adb kill-server')
else:
log.insert(END,"请连接USB,或输入IP地址后重试")
log = tkinter.Text(root,width=78,height=26,bd=0.02,font = ('汉隶',15),fg = 'blue')
log.place(relx = 0.01,rely =0.02)
logbutton = tkinter.Button(root,text= '获取logcat',width = 50,bd =0.5,bg = 'blue',command = logcat)
logbutton.place(relx = 0.28,rely =0.92)
root.mainloop()