Python在GUI方面并不强,相比wxpython而言,Tkinter内置于python库中,无需另外安装,同时基本的控件也满足日常需求,本文主要罗列Tkinter的基本控件用法,同时文末放出用Tkinter改写的monkey脚本供大家参考。
Tkinter 组件
Tkinter的提供各种控件,如按钮,标签和文本框,一个GUI应用程序中使用。这些控件通常被称为控件或者部件。
目前有15种Tkinter的部件。我们提出这些部件以及一个简短的介绍,在下面的表:
标准属性
标准属性也就是所有控件的共同属性,如大小,字体和颜色等等。
几何管理
Tkinter控件有特定的几何状态管理方法,管理整个控件区域组织,一下是Tkinter公开的几何管理类:包、网格、位置
各控件的详细用法
http://blog.csdn.net/aa1049372051/article/category/6302208
贴上用Tkinter改造的monkey脚本,mac环境编译通过
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter import *
import os
import time
delayDefault = "2"
seedDefault = "5000000"
executionFrequencyDefault = "60000000"
logDir = "./"
package_default = "com.genyannetwork.qiyuesuo.debug "
def read_click():
clear_list()
os.system("adb shell ls data/data > ~/log.log")
home = os.path.expanduser('~')
f = open(home + "/log.log", 'r')
line = f.readline()
while line:
line = f.readline()
if (line != ""):
print "====" + line
package_list_box.insert(END, line)
f.close()
def select_all_click():
package_list_box.select_set(0, package_list_box.size())
def un_select_click():
package_list_box.select_clear(0, package_list_box.size())
def clear_list():
package_list_box.delete(0, package_list_box.size())
def reset():
list_files("/sdcard/mtklog")
seed_var.set(seedDefault)
execute_num_value.set(executionFrequencyDefault)
delay_num_var.set(delayDefault)
log_value.set(2)
execute_mode_value.set(5)
def quick_start_click():
monkeyCmd = "adb shell monkey" + " -p " + package_default + " --throttle 2 -s 5000000 -v -v -v --ignore-timeouts 60000000 > trace.log"
clear_list()
package_list_box.insert(END, "执行命令:")
package_list_box.insert(END, monkeyCmd)
os.system(monkeyCmd)
print '-----------monkey finish----------'
def start_click():
start_cmd()
def start_cmd():
date = time.strftime('%Y%m%d%H%m%s', time.localtime(time.time()))
package_section = ""
print "select package count:" + str(len(package_list_box.curselection()))
for i in package_list_box.curselection():
package = package_list_box.get(i)
print package
pack = package.strip('\r\n')
package_section += (" -p " + pack)
print package_section
seed_section = " -s " + seed_var.get()
delay_section = " --throttle " + delay_num_var.get()
log_section = ""
mode_section = ""
log_id = log_value.get()
if (log_id == 0):
log_section += " -v"
elif (log_id == 1):
log_section += " -v -v"
elif (log_id == 2):
log_section += " -v -v -v"
mode_id = execute_mode_value.get()
mode_id = 1
mode = [" --ignore-crashes ",
" --ignore-timeouts ",
" --ignore-security-exceptions ",
" --ignore-native-crashes ",
" --monitor-native-crashes "]
if (mode_id == 0):
mode_section = mode[0]
elif (mode_id == 1):
mode_section = mode[1]
elif (mode_id == 2):
mode_section = mode[2]
elif (mode_id == 3):
mode_section = mode[3]
elif (mode_id == 4):
mode_section = mode[4]
else:
mode_section = mode[0] + mode[1] + mode[2] + mode[3] + mode[4]
############## create monkey log dir ###############
usr_home = os.path.expanduser('~')
os.chdir(usr_home)
logDir = "MonkeyLog_" + date
os.system("mkdir " + logDir)
logDir = os.path.join(usr_home, logDir)
print logDir
os.chdir(logDir)
############### record monkey trace ################
monkeyCmd = "adb shell monkey "
monkeyCmd = monkeyCmd + delay_section + seed_section + package_section + log_section + mode_section
monkeyCmd = monkeyCmd + " " + execute_num_value.get() + " > trace.log"
print monkeyCmd
os.system(monkeyCmd)
print '-----------monkey finish----------'
def build_log():
os.chdir(logDir)
print logDir
date = time.strftime('%Y-%m-%d-%H-%M', time.localtime(time.time()))
dir_m = "Monkey_Log_" + date.replace("-", "")
dir0 = "sdcard0_mtklog"
dir1 = "sdcard1_mtklog"
if (os.path.exists(dir_m + "/" + dir0)):
print "already exists"
else:
os.system("mkdir -p " + dir_m + "/" + dir0)
if (os.path.exists(dir_m + "/" + dir1)):
print "already exists"
else:
os.system("mkdir -p " + dir_m + "/" + dir1)
os.chdir(dir_m)
os.system("adb pull /storage/sdcard0/mtklog/ " + dir0)
os.system("adb pull /storage/sdcard1/mtklog/ " + dir1)
build_fatal_log(os.getcwd())
def build_fatal_log(path):
list_files(path)
def list_files(path):
for root, dirs, files in os.walk(path):
log_f = ""
for f in files:
if (f.find("main") == 0):
log_f = f.strip()
os.chdir(root)
if (log_f != ""):
grep_cmd = "grep -Eni -B20 -A20 'FATAL|error|exception|system.err|androidruntime' " + log_f + " > " + log_f + "_fatal.log"
os.system(grep_cmd)
print "--------------finish build log-----------------"
if __name__ == '__main__':
app = Tk()
frame = LabelFrame(app, height=800, width=500).pack()
xPos = 10
xPos1 = 100
yPos = 12
yDelta = 40
execute_mode = ["忽略程序崩溃",
"忽略程序无响应",
"忽略安全异常",
"出错中断程序",
"本地代码导致的崩溃",
"默认"
]
log_mode = ["简单", "普通", "详细"]
menuBar = Menu(app)
menu1 = Menu(menuBar, tearoff=0)
menuBar.add_cascade(label="File1", menu=menu1)
Label(frame, text="种子数:").place(x=xPos, y=yPos)
seed_var = StringVar()
seedCtrl = Entry(frame, textvariable=seed_var).place(x=xPos1, y=yPos)
Label(frame, text="执行次数:").place(x=xPos, y=yPos + yDelta)
execute_num_value = StringVar()
execute_num_ctrl = Entry(frame, textvariable=execute_num_value).place(x=xPos1, y=yPos + yDelta)
Label(frame, text="延时:").place(x=xPos, y=yPos + 2 * yDelta)
delay_num_var = StringVar()
delay_num_ctrl = Entry(frame, textvariable=delay_num_var).place(x=xPos1, y=yPos + 2 * yDelta)
Label(frame, text="执行方式:").place(x=xPos, y=yPos + 3 * yDelta)
execute_mode_value = IntVar()
execute_mode_value.set(1)
for i in range(len(execute_mode)):
Radiobutton(frame,
variable=execute_mode_value,
indicatoron=0,
text=execute_mode[i],
value=i
).place(x=xPos1, y=yPos + (3 + i) * yDelta)
Label(frame, text="日志输出等级:").place(x=xPos1 + 180, y=yPos + 3 * yDelta)
log_value = IntVar()
log_value.set(1)
for i in range(len(log_mode)):
Radiobutton(frame,
variable=log_value,
indicatoron=0,
text=log_mode[i],
value=i).place(x=xPos1 + 280, y=yPos + (3 + i) * yDelta)
button_text = ["读取程序", "全部选择", "全部取消", "默认参数", "一键开始", "开始执行", "生成日志"]
Button(frame, text=button_text[0], command=read_click).place(x=xPos, y=yPos + 9 * yDelta)
Button(frame, text=button_text[1], command=select_all_click).place(x=xPos + 100, y=yPos + 9 * yDelta)
Button(frame, text=button_text[2], command=un_select_click).place(x=xPos + 100 * 2, y=yPos + 9 * yDelta)
Button(frame, text=button_text[3], command=reset).place(x=xPos + 100 * 3, y=yPos + 9 * yDelta)
Button(frame, text=button_text[4], command=quick_start_click).place(x=xPos, y=yPos + 10 * yDelta)
Button(frame, text=button_text[5], command=start_click).place(x=xPos + 100, y=yPos + 10 * yDelta)
Button(frame, text=button_text[6], command=build_log).place(x=xPos + 100 * 2, y=yPos + 10 * yDelta)
check_list_box_value = StringVar()
package_list_box = Listbox(frame, height=20, width=50, selectmode=MULTIPLE,
listvariable=check_list_box_value)
package_list_box.place(x=xPos, y=yPos + 11 * yDelta)
for item in ['欢迎使用monkey自动测试工具', '请连接好手机后,点击读取程序开始']:
package_list_box.insert(END, item)
app.mainloop()