之前有个项目需要带多个不太懂代码的小伙伴测试,而且测试前置条件比较多,必须使用自动化,最终选择tkinter库做图形化界面。
一、大致了解tkinter
整理成思维导图
二、编写脚本,打包成exe文件
写了一个demo如下,点击按钮可以调用不同函数,并且打印日志到text框里
- mport time
- from tkinter import *
-
- LOG_LINE_NUM = 0
-
-
- class MigrateTest:
- def __init__(self, init_window):
- # 创建主窗口对象
- self.init_window = init_window
- # 定义主窗口属性
- self.init_window.title("云厂商迁移测试 developed by ronghuang")
-
- def layout(self):
- """控件布局"""
- lables = [
- {'用户信息': {'row': 0, 'column': 0}},
- {'手机号': {'row': 1, 'column': 0}},
- {'密码': {'row': 1, 'column': 2}},
- {'迁移的表': {'row': 1, 'column': 4}}
- ]
- for lable in lables:
- for key, value in lable.items():
- Label(master=self.init_window, text=key).grid(**value)
-
- # Entry
- entry_list = [
- {'info_phone': {'row': 1, 'column': 1}},
- {'info_pwd': {'row': 1, 'column': 3}},
- {'info_table': {'row': 1, 'column': 5}}
- ]
- for entry in entry_list:
- for key, value in entry.items():
- setattr(self, key, Entry(master=self.init_window))
- getattr(self, key).grid(**value)
-
- # text 控件:存放log
- setattr(self, 'log_text', Text(self.init_window, width=80, height=50))
- getattr(self, 'log_text').place(x=120, y=62)
-
- # button: command
- buttons = [{'堵塞io': 'self.prinf'}, {'删除堵塞': 'self.prinf'}, {'查询堵塞': 'self.prinf'},
- {'切换到七牛':'self.prinf'}, {'切换到阿里':'self.prinf'}, {'切换到自建存储':'self.prinf'},
- {'切换到腾讯':'self.prinf'}, {'查询服务端bucket':'self.prinf'}, {'查询服务端路由':'self.prinf'},
- {'重置账号密码':'self.prinf'}, {'关闭当前窗口':'self.init_window.destroy'}]
- for button in buttons:
- for text, command in button.items():
- button_bloke = Button(self.init_window, text=text, command=eval(command), width=15)
- row = (buttons.index(button) + 1) * 2 + 1
- button_bloke.grid(row=row, column=0) #0:3,1:5,2:7
-
- def write_log(self, msg):
- log_lines = 20
- global LOG_LINE_NUM
- current_time = time.strftime('%Y-%m-%d %H:%M:%S')
-
- logmsg_in = str(current_time) +" " + str(msg) + "\n" # 换行
- if LOG_LINE_NUM <= log_lines:
- getattr(self, 'log_text').insert(END, logmsg_in)
- LOG_LINE_NUM += 1
- else:
- getattr(self, 'log_text').delete(1.0, 2.0)
- getattr(self, 'log_text').insert(END, logmsg_in)
-
- # 功能函数
- def get_info(self):
- # for attr in ['info_phone', 'info_pwd', 'info_table']:
- # arrti_list = []
- # arrti_list.append(getattr(self, attr).get())
- arrti_list = [getattr(self, attr).get() for attr in ['info_phone', 'info_pwd', 'info_table']]
- self.write_log(tuple(arrti_list))
- return tuple(arrti_list)
-
- def prinf(self):
- # 输出到界面
- arrti_list = []
- for attr in ['info_phone', 'info_pwd', 'info_table']:
- arrti_list.append(getattr(self, attr).get())
- self.write_log(tuple(arrti_list))
- return tuple(arrti_list)
-
-
- def run_main():
- """运行主函数"""
- init_window = Tk()
- migrate = MigrateTest(init_window)
- migrate.layout()
- init_window.mainloop()
-
-
- if __name__ == '__main__':
- run_main()
打包exe文件:
依赖第三方库:pyinstaller
使用命令:pyinstaller -D filename.py