Tkinter & pyinstaller

1.Tkinter学习链接
2.打包成exe程序
3.main程序
4.methods代码



1.Tkinter学习链接

点我传送门

2.打包成exe程序
1.需要使用 python3.6版本
2.安装第三方库:  pip install pyinstaller
3.使用方法:
    * 打开cmd窗口
    * 来到要打包的 .py文件下
    * 输入 pyinstaller.exe 的路径,然后 -F 指定打包的文件,等待程序跑完
    eg:
    D:\Python3.6\Scripts\pyinstaller.exe -F demo.py
    
    
3.main程序(此程序无法运行,只供参考,代码还需优化)
本程序主要用到部件:

Label:显示不可编辑的内容
Text: 显示多行文本,用来收集或显示用户的输入
Entry:  只显示单行文本,用来收集  用户的输入
Button:一个简单的按钮,用来执行一个命令或别的操作。
Combobox: 下拉框
 
 # -*-coding:utf-8-*-
"""
@author: weisheng.wang
"""

import re
import threading
import tkinter as tk
from tkinter import ttk
from methods import *

# 默认是: 不展开
is_open_flag = False
# 默认不打印info信息
display_info = False
# 默认不打印text信息
display_text = False

get_page_content_method = "方式:resourceId"
compare_method = "1.判断标准值是否都在实际值列表中"

window = tk.Tk()
window.title("百宝箱V3.6.2.480")
window.geometry("500x550")


def get_box_ip_address():
    """获取输入的IP地址"""

    get_ip_address = ip_address_input.get()  # 获取输入的内容

    try:  # 判断输入内容是否 符合 IP格式
        re.match("(\\d{1,3}\\D){4}", get_ip_address).group()
        result_display.insert("end", "输入的内容是:%s \n" % get_ip_address)
        write_ip_in_config(get_ip_address)
        result_display.insert("end", "请等待...:\n")
        content_list = os.popen("adb connect %s" % get_ip_address).readlines()
        for line in content_list:
            result_display.insert("end", line + '\n')

    except AttributeError:
        result_display.insert("end", "您输入格式有误\n刚才输入的IP是:%s\n" % get_ip_address)
        result_display.insert("end", "请按照 xxx.xxx.xxx.xxx:xxxx 或 xxx.xxx.xxx.xxx 格式输入\n")
    except Exception as e:
        result_display.insert('end', "输入的IP有误\n")
        result_display.insert("end", "报错原因:%s\n" % e)
    finally:
        result_display.insert('end', '\n\n')


def set_chroma():
    """设置chroma"""
    try:
        result_display.insert("end", "请稍等,正在播放...\n")
        TPeripheral.release_chroma()

        # 获取输入的值
        timing = timing_input.get()
        pattern = pattern_input.get()
        if len(timing) == 0 or len(pattern) == 0:
            result_display.insert("end", str("timing或pattern值为空,请填入内容") + '\n')
            return

        # 播放Chroma
        if play_chroma(timing, pattern):
            result_display.insert("end", "播放成功\n")
            write_data_chroma(timing, pattern)
        else:
            result_display.insert("end", "播放失败\n")
    except Exception as e:
        result_display.insert("end", str(e) + '\n')
    finally:
        result_display.insert('end', '\n')


def set_stream():
    """设置码流"""

    try:
        result_display.insert("end", "请稍等,正在播放...\n")
        TPeripheral.release_stream()

        # 获取输入的值
        stream_file = stream_path_input.get()
        play_time = stream_time_input.get()
        stream_type = stream_type_input.get()
        channel_point = stream_freq_input.get()

        # 判断输入的值是否为空
        list1 = [stream_file, play_time, stream_type, channel_point]
        for list2 in list1:
            if len(list2) == 0 or list2 is None:
                result_display.insert("end", "请输入所有内容" + '\n')
                return

        result_display.insert("end", '当前码流地址为:\n%s\n' % stream_file)
        if not os.path.exists(stream_file):
            result_display.insert("end", "码流文件不存在\n")
            return

        # 播放码流
        if play_stream(stream_file, play_time, stream_type, channel_point):
            result_display.insert("end", "播放成功\n")
            write_data_stream(stream_file, play_time, stream_type, channel_point)
        else:
            result_display.insert("end", "播放失败\n")
            result_display.insert("end", '当前码流地址为:\n%s\n' % stream_file)
    except Exception as e:
        result_display.insert("end", str(e) + '\n')
    finally:
        result_display.insert("end", "\n")


def write_content():
    """读取配置 写进表格"""
    data = get_config()

    # 往方框中填入内容
    ip_address_input.insert('insert', data["ip_address"])

    timing_input.insert("insert", data["timing"])
    pattern_input.insert("insert", data["pattern"])

    stream_path_input.insert("insert", data["stream_path"])
    stream_time_input.insert("insert", data["stream_play_time"])
    stream_type_input.insert("insert", data["stream_type"])
    stream_freq_input.insert("insert", data["stream_freq"])

    remote_input.insert("insert", data["remote_keyword"])
    remote_time_input.insert("insert", data["remote_cnt_time"])
    adb_input.insert("insert", data["adb_keyword"])
    adb_time_input.insert("insert", data["adb_cnt_time"])
    continue_send_key_input.insert("insert", data["continue_send_key_list"])
    get_page_content_input.insert("insert", data["element_id"])


def send_remote():
    """发送 红外码值"""
    from Package import TPeripheral
    try:
        # 获取输入的值
        remote = TPeripheral.get_remote()
        keyword = remote_input.get()
        cnt_time = remote_time_input.get()

        cnt, timeouts = str(cnt_time).split(',')
        write_data_remote(keyword, cnt_time)
        remote.send_key(keyword, int(cnt), int(timeouts))
        result_display.insert('end', "发送成功\n")
    except Exception as e:
        result_display.insert("end", str(e) + '\n')
    finally:
        TPeripheral.release_remote()


def send_adb():
    """发送 adb按键"""
    import os
    import time
    try:
        keyword = adb_input.get()
        cnt_time = adb_time_input.get()
        cnt, timeouts = str(cnt_time).split(',')
        result_display.insert("end", '请稍等...\n')
        write_data_adb(keyword, cnt_time)
        for _ in range(int(cnt)):
            os.system("adb shell input keyevent %s" % keyword)
            time.sleep(int(timeouts))
        result_display.insert("end", '发送成功\n')
    except Exception as e:
        result_display.insert("end", str(e) + '\n')


def screen_picture1():
    """采集卡截图"""
    from Package import TMenu
    result_display.insert("end", '截图中,请稍等...\n')
    img = TMenu.screen_shoots()
    result_display.insert('end', '截图路径:\n%s\n' % img)
    TPeripheral.release()


def is_open():
    """展开/折叠"""
    global is_open_flag, open_button

    # 展开后的内容
    if not is_open_flag:
        is_open_flag = True
        window.geometry("1100x600")

        result_display.place(x=20, y=400, anchor='nw', height=190, width=1060)
        open_button = tk.Button(window, text="折叠", bg="white", command=lambda: fun_thread(is_open))
        open_button.place(x=380, y=40, anchor='nw', height=40, width=70)

    # 折叠后的内容
    else:
        is_open_flag = False
        window.geometry("500x550")
        result_display.place(x=10, y=400, anchor='nw', height=140, width=480)
        open_button = tk.Button(window, text="展开", bg="white", command=lambda: fun_thread(is_open))
        open_button.place(x=380, y=40, anchor='nw', height=40, width=70)


def get_page_content(*args):
    """获取下拉框 选择的元素"""

    global get_page_content_method
    get_page_content_method = get_page_content_box_list.get()


def get_list_content(*args):
    """获取列表比较下拉框 选择的元素"""

    global compare_method
    compare_method = list_compare_method_box_list.get()


def get_page_value1():
    """ 获取页面元素 """

    global get_page_content_method, display_info, display_text
    value = get_page_content_input.get()
    get_page_content_method = str(get_page_content_method).replace("方式:", "")
    get_page_value(get_page_content_method, value, result_display, display_text, display_info)


def set_print_info_value():
    """设置 是否显示info内容"""

    global display_info, print_info_button, display_text

    if not display_info:
        display_info = True
        print_info_button = tk.Button(window, text="True", bg="white", command=lambda: fun_thread(set_print_info_value))
        print_info_button.place(x=660, y=340, anchor='nw', height=40, width=90)

    elif display_info:
        display_info = False
        print_info_button = tk.Button(window, text="False", bg="white",
                                      command=lambda: fun_thread(set_print_info_value))
        print_info_button.place(x=660, y=340, anchor='nw', height=40, width=90)


def set_print_text_value():
    """设置 是否显示text内容"""

    global print_text_button, display_text

    if not display_text:
        display_text = True
        print_text_button = tk.Button(window, text="True", bg="white", command=lambda: fun_thread(set_print_text_value))
        print_text_button.place(x=900, y=340, anchor='nw', height=40, width=90)

    elif display_text:
        display_text = False
        print_text_button = tk.Button(window, text="False", bg="white",
                                      command=lambda: fun_thread(set_print_text_value))
        print_text_button.place(x=900, y=340, anchor='nw', height=40, width=90)


def continue_send_key_fun():
    """持续发键"""

    continue_key_list1 = continue_send_key_input.get()
    comma_num = int(continue_key_list1.count(','))
    if comma_num != 3 and comma_num != 1:
        result_display.insert("end", "输入有误,请输入两个(或四个) 内容\n")
        return

    try:
        continue_send_key_adb(continue_key_list1, result_display)
    except Exception as e:
        result_display.insert("end", str(e) + "\n")


def compare_list_fun():
    """列表比较"""

    global compare_method, result_display
    method = str(compare_method[0])
    standard_value = str(standard_value_input.get())
    actual_value = str(actual_value_input.get())

    # 去除掉原有的左右括号
    if standard_value.startswith("[") and standard_value.endswith("]"):
        standard_value = standard_value.replace("[", "").replace("]", "")
    if actual_value.startswith("[") and actual_value.endswith("]"):
        actual_value = actual_value.replace("[", "").replace("]", "")

    # 去除掉原有的单双引号
    standard_value = standard_value.replace('"', '').replace("'", "")
    actual_value = actual_value.replace('"', '').replace("'", "")

    # 转成列表
    standard_value = standard_value.split(',')
    actual_value = actual_value.split(',')

    result_display.insert("end", str(standard_value) + "\n")
    result_display.insert("end", str(actual_value) + "\n")
    two_list_compare_method(standard_value, actual_value, method, result_display)


def fun_thread(func, *args):
    # 创建线程
    t = threading.Thread(target=func, args=args)
    t.setDaemon(True)  # 开启守护
    t.start()


# 放置"当前IP"显示框
ip_tip = tk.Label(window, text="当前IP:", bg="white")
ip_tip.place(x=30, y=40, anchor='nw', height=40, width=70)

# 放置"IP"输入框
ip_address_input = tk.Entry(window, bg="white")
ip_address_input.place(x=120, y=40, anchor='nw', height=40, width=150)

# 放置"连接"按钮框
ip_connect = tk.Button(window, text="连接", bg="white", command=lambda: fun_thread(get_box_ip_address))
ip_connect.place(x=300, y=40, anchor='nw', height=40, width=70)

# 放置"展开"按钮框
if not is_open_flag:
    # open_button = tk.Button(window, text="展开", bg="white", command=lambda: fun_thread(is_open))
    open_button = tk.Button(window, text="展开", bg="white", command=is_open)
    open_button.place(x=380, y=40, anchor='nw', height=40, width=70)

# 放置"Timing"显示框
timing_tip_text = tk.Label(window, text="Timing值:", bg="white")
timing_tip_text.place(x=30, y=90, anchor='nw', height=40, width=60)

# # 放置"Timing"输入框
timing_input = tk.Entry(window, bg="white")
timing_input.place(x=100, y=90, anchor='nw', height=40, width=60)

# 放置"pattern"显示框
pattern_tip = tk.Label(window, text="Pattern值:", bg="white")
pattern_tip.place(x=170, y=90, anchor='nw', height=40, width=60)

# 放置"pattern"输入框
pattern_input = tk.Entry(window, bg="white")
pattern_input.place(x=240, y=90, anchor='nw', height=40, width=60)

# 放置"chroma 连接"按钮框
chroma_connect = tk.Button(window, text="chroma连接", bg="white", command=lambda: fun_thread(set_chroma))
chroma_connect.place(x=320, y=90, anchor='nw', height=40, width=90)

# 放置"码流地址"显示框
stream_path_tip = tk.Label(window, text="码流地址:", bg="white")
stream_path_tip.place(x=30, y=140, anchor='nw', height=40, width=60)

# 放置"码流地址"输入框
stream_path_input = tk.Entry(window, bg="white")
stream_path_input.place(x=100, y=140, anchor='nw', height=40, width=60)

# 放置"码流 时间"显示框
stream_time_tip = tk.Label(window, text="播放时间", bg="white")
stream_time_tip.place(x=170, y=140, anchor='nw', height=40, width=60)

# 放置"码流 时间"输入框
stream_time_input = tk.Entry(window, bg="white")
stream_time_input.place(x=240, y=140, anchor='nw', height=40, width=60)

# 放置"码流 类型"显示框
stream_type_tip = tk.Label(window, text="播放类型", bg="white")
stream_type_tip.place(x=30, y=190, anchor='nw', height=40, width=60)

# 放置"码流 类型"输入框
stream_type_input = tk.Entry(window, bg="white")
stream_type_input.place(x=100, y=190, anchor='nw', height=40, width=60)

# 放置"码流 频点"显示框
stream_freq_tip = tk.Label(window, text="播放频点", bg="white")
stream_freq_tip.place(x=170, y=190, anchor='nw', height=40, width=60)

# 放置"码流 频点"输入框
stream_freq_input = tk.Entry(window, bg="white")
stream_freq_input.place(x=240, y=190, anchor='nw', height=40, width=60)

# 放置"码流 播放"按钮框
stream_play_button = tk.Button(window, text="码流播放", bg="white", command=lambda: fun_thread(set_stream))
stream_play_button.place(x=320, y=150, anchor='nw', height=70, width=100)

# 放置"红外发码"显示框
remote_tip = tk.Label(window, text="红外发码", bg='white')
remote_tip.place(x=30, y=240, anchor='nw', height=40, width=60)

# 放置"红外发码"输入框
remote_input = tk.Entry(window, bg="white")
remote_input.place(x=100, y=240, anchor='nw', height=40, width=60)

# 放置"次数和时间"显示框
remote_time_tip = tk.Label(window, text="次数和时间", bg='white')
remote_time_tip.place(x=170, y=240, anchor='nw', height=40, width=60)

# 放置"次数和时间"输入框
remote_time_input = tk.Entry(window, bg="white")
remote_time_input.place(x=240, y=240, anchor='nw', height=40, width=60)

# 放置"发送"按钮框
remote_play_button = tk.Button(window, text="红外码值发送", bg="white", command=lambda: fun_thread(send_remote))
remote_play_button.place(x=320, y=240, anchor='nw', height=40, width=90)

# 放置"adb按键"显示框
adb_tip = tk.Label(window, text="adb按键", bg='white')
adb_tip.place(x=30, y=290, anchor='nw', height=40, width=60)

# 放置"adb按键"输入框
adb_input = tk.Entry(window, bg="white")
adb_input.place(x=100, y=290, anchor='nw', height=40, width=60)

# 放置"次数和时间"显示框
adb_time_tip = tk.Label(window, text="次数和时间", bg='white')
adb_time_tip.place(x=170, y=290, anchor='nw', height=40, width=60)

# 放置"次数和时间"输入框
adb_time_input = tk.Entry(window, bg="white")
adb_time_input.place(x=240, y=290, anchor='nw', height=40, width=60)

# 放置"发送"按钮框
adb_play_button = tk.Button(window, text="adb按键发送", bg="white", command=lambda: fun_thread(send_adb))
adb_play_button.place(x=320, y=290, anchor='nw', height=40, width=90)

# 放置"电源 上电"按钮框
button_ac_on = tk.Button(window, text="上电", bg="white", command=lambda: fun_thread(ac_on, result_display))
button_ac_on.place(x=30, y=340, anchor='nw', height=40, width=70)

# 放置"电源 断电"按钮框
button_ac_off = tk.Button(window, text="断电", bg="white", command=lambda: fun_thread(ac_off, result_display))
button_ac_off.place(x=120, y=340, anchor='nw', height=40, width=70)

# 放置"Power"按钮框
button_power = tk.Button(window, text="Power", bg="white", command=lambda: fun_thread(send_power, result_display))
button_power.place(x=210, y=340, anchor='nw', height=40, width=70)

# 放置"TPeripheral 释放"按钮框
button_release = tk.Button(window, text="释放", bg="white", command=lambda: fun_thread(tp_release, result_display))
button_release.place(x=300, y=340, anchor='nw', height=40, width=80)

# 放置"采集卡 截图"按钮框
button_ac_on = tk.Button(window, text="采集卡 截图", bg="white", command=lambda: fun_thread(screen_picture1))
button_ac_on.place(x=420, y=245, anchor='nw', height=80, width=70)

# 放置"清空 终端 内容"按钮框
button_ac_on = tk.Button(window, text="清空终端内容", bg="white", command=lambda: fun_thread(clear_terminal, result_display))
button_ac_on.place(x=400, y=340, anchor='nw', height=40, width=80)

# 放置 "分界线"
tk.Label(window, text='', bg='black').place(x=525, y=0, anchor='nw', height=400, width=20)

# 放置"持续发键" 显示框
tk.Label(window, text="持续发键", bg="white").place(x=560, y=50, anchor='nw', height=40, width=80)

# 放置"发键方式:adb"显示框
tk.Label(window, text="发键方式:adb", bg="white").place(x=650, y=50, anchor='nw', height=40, width=120)

# # 放置"持续发键"输入框
continue_send_key_input = tk.Entry(window, bg="white")
continue_send_key_input.place(x=780, y=50, anchor='nw', height=40, width=140)

# 放置"持续发键  发送"按钮框
continue_send_key_button = tk.Button(window, text="发送", bg="white", command=lambda: fun_thread(continue_send_key_fun))
continue_send_key_button.place(x=930, y=50, anchor='nw', height=40, width=70)

# 放置"持续发键  停止"按钮框
continue_send_key_button = tk.Button(window, text="停止", bg="white",
                                     command=lambda: fun_thread(stop_continue_send_key, result_display))
continue_send_key_button.place(x=1010, y=50, anchor='nw', height=40, width=70)

# 放置"页面内容" 显示框
tk.Label(window, text="打印页面内容", bg="white").place(x=560, y=110, anchor='nw', height=40, width=80)

# 放置"打印内容"下拉框
get_page_content_box_list = ttk.Combobox(window, width=9)
get_page_content_box_list["values"] = ("方式:text", "方式:className", "方式:resourceId", "方式:packageName", "方式:textContains")
get_page_content_box_list.current([2])
get_page_content_box_list.grid(row=0, column=0, padx=660, pady=110, ipadx=20, ipady=9, sticky='W')
get_page_content_box_list.bind("<<ComboboxSelected>>", get_page_content)

# 放置"打印内容"输入框
get_page_content_input = tk.Entry(window, bg="white")
get_page_content_input.place(x=790, y=110, anchor='nw', height=40, width=190)

# 放置"打印内容"按钮框
get_page_content_button = tk.Button(window, text="发送", command=lambda: fun_thread(get_page_value1))
get_page_content_button.place(x=990, y=110, anchor='nw', height=40, width=90)

# 放置"标准值列表"显示框
standard_value_list = tk.Label(window, text="标准值列表", bg="white")
standard_value_list.place(x=560, y=170, anchor='nw', height=40, width=80)

# 放置"标准值列表"输入框
standard_value_input = tk.Entry(window, bg="white")
standard_value_input.place(x=660, y=170, anchor='nw', height=40, width=400)

# 放置"实际值列表"显示框
actual_value_list = tk.Label(window, text="实际值列表", bg="white")
actual_value_list.place(x=560, y=230, anchor='nw', height=40, width=80)

# 放置"实际值列表"输入框
actual_value_input = tk.Entry(window, bg="white")
actual_value_input.place(x=660, y=230, anchor='nw', height=40, width=400)

# 放置"列表对比方式"显示框
actual_value_list = tk.Label(window, text="实际值列表", bg="white")
actual_value_list.place(x=560, y=230, anchor='nw', height=40, width=80)

# 放置"列表比较"提示语
list_compare_method_tip = tk.Label(window, text="比较方式", bg='white')
list_compare_method_tip.place(x=560, y=290, anchor='nw', height=40, width=80)

# 放置"列表比较"下拉框
list_compare_method_box_list = ttk.Combobox(window, width=25)
list_compare_method_box_list["values"] = ("1.判断标准值是否都在实际值列表中", "2.通过相同下标比较", "3.判断实际列表值是否都在标准值中")
list_compare_method_box_list.current([0])  # padx pady 放置的位置
list_compare_method_box_list.grid(row=1, column=0, padx=660, pady=27, ipadx=20, ipady=9, sticky='W')
list_compare_method_box_list.bind("<<ComboboxSelected>>", get_list_content)

# 放置"列表比较"按钮框
list_compare_method_button = tk.Button(window, text="发送", command=lambda: fun_thread(compare_list_fun))
list_compare_method_button.place(x=920, y=288, anchor='nw', height=40, width=90)

# 放置"打印info信息"提示框
print_info_tip = tk.Label(window, text="打印info信息", bg="white")
print_info_tip.place(x=560, y=340, anchor='nw', height=40, width=80)

# 放置"打印info信息"按钮框
print_info_button = tk.Button(window, text="False", bg="white", command=lambda: fun_thread(set_print_info_value))
print_info_button.place(x=660, y=340, anchor='nw', height=40, width=90)

# 放置"打印text信息"提示框
print_text_tip = tk.Label(window, text="打印text信息", bg="white")
print_text_tip.place(x=800, y=340, anchor='nw', height=40, width=80)

# 放置"打印text信息"按钮框
print_text_button = tk.Button(window, text="False", bg="white", command=lambda: fun_thread(set_print_text_value))
print_text_button.place(x=900, y=340, anchor='nw', height=40, width=90)

# 放置 "显示结果" 输入框
result_display = tk.Text(window)
result_display.place(x=10, y=400, anchor='nw', height=140, width=480)

# 读取配置
write_content()

# 解决花屏问题
for _ in range(4):
    is_open()

# 运行窗口
window.mainloop()
4.methods代码
# -*-coding:utf-8-*-

import json
import os
import time
import uiautomator2
from Package import TPeripheral

content = ''
continue_send_key_flag = False


def get_config():
    """获取配置"""
    global content
    with open("config.json", 'r', encoding='utf-8') as f:
        content = json.load(f)
    return content


def write_data():
    """写入数据"""
    global content
    with open('config.json', 'w', encoding='utf-8') as f:
        json.dump(content, f)


def write_ip_in_config(ip_address):
    """将ip 写进配置文件中"""

    global content
    content["ip_address"] = ip_address
    with open('config.json', 'w', encoding='utf-8') as f:
        json.dump(content, f)


def ac_on(result_display):
    """上电"""
    try:
        remote = TPeripheral.get_remote()
        remote.ac_on()
        TPeripheral.release()
        result_display.insert('end', "上电成功\n")
    except Exception as e:
        result_display.insert('end', str(e) + '\n')


def ac_off(result_display):
    """断电"""

    try:
        remote = TPeripheral.get_remote()
        remote.ac_off()
        TPeripheral.release()
        result_display.insert('end', "断电成功\n")
    except Exception as e:
        result_display.insert('end', str(e) + '\n')


def send_power(result_display):
    """发送Power键"""
    try:
        remote = TPeripheral.get_remote()
        remote.send_key("power", 1, 3)
        TPeripheral.release()
        result_display.insert("end", "发送Power键成功\n")
    except Exception as e:
        result_display.insert("end", str(e) + '\n')


def play_chroma(timing, pattern):
    """播放 22293"""
    chroma = TPeripheral.get_chroma()
    if chroma.set_timing_pattern(timing, pattern):
        return True
    else:
        TPeripheral.release_chroma()
        return False


def play_stream(stream_file, play_time, stream_type, channel_point):
    """播放码流"""

    stream = TPeripheral.get_stream()
    stream.play_stream_at_thread(stream_file, play_time, stream_type, channel_point)
    time.sleep(2)
    if stream.is_playing():
        return True
    else:
        return False


def write_data_stream(stream_file, play_time, stream_type, channel_point):
    """将码流数据写进配置"""

    global content
    content["stream_path"] = stream_file
    content["stream_play_time"] = play_time
    content["stream_type"] = stream_type
    content["stream_freq"] = channel_point
    with open('config.json', 'w', encoding='utf-8') as f:
        json.dump(content, f)


def write_data_chroma(timing, pattern):
    """将chroma数据写进配置"""

    global content
    content["timing"] = timing
    content["pattern"] = pattern
    with open('config.json', 'w', encoding='utf-8') as f:
        json.dump(content, f)


def write_data_remote(remote_keyword, remote_cnt_time):
    """将 红外码值 数据写进配置"""

    global content
    content["remote_keyword"] = remote_keyword
    content["remote_cnt_time"] = remote_cnt_time
    with open('config.json', 'w', encoding='utf-8') as f:
        json.dump(content, f)


def write_data_adb(adb_keyword, adb_cnt_time):
    """将 adb按键 数据写进配置"""

    global content
    content["adb_keyword"] = adb_keyword
    content["adb_cnt_time"] = adb_cnt_time
    with open('config.json', 'w', encoding='utf-8') as f:
        json.dump(content, f)


def tp_release(result_display):
    """串口释放"""
    try:
        TPeripheral.release()
        result_display.insert("end", "释放成功\n")
    except Exception as e:
        result_display.insert("end", str(e) + '\n')


def stop_continue_send_key(result_display):
    """停止 持续发键"""
    global continue_send_key_flag
    continue_send_key_flag = False
    result_display.insert("end", "停止发键\n")


def continue_send_key_adb(list1, result_display):
    """通过adb持续发键"""

    global continue_send_key_flag, content
    continue_send_key_flag = True
    n = 0
    result_display.insert("end", "程序启动...\n")
    content["continue_send_key_list"] = list1
    write_data()

    list1 = list1.split(',')
    print(list1)
    while continue_send_key_flag:

        for i in range(len(list1)):
            if int(i) % 2 == 0:  # 交替执行

                os.system("adb shell input keyevent %s" % list1[i])
            else:
                time.sleep(int(list1[i]))
        n += 1
        if n % 5 == 0:
            result_display.insert("end", "已经执行%s轮\n" % n)


def get_page_value(element, value, result_display, display_text=False, display_info=False):
    """获取页面元素"""

    element_list = []
    driver = uiautomator2.connect()
    if element == "resourceId":
        if not driver(resourceId=value).exists:
            result_display.insert("end", "该元素不存在:%s\n" % value)
            return
        else:
            element_list = driver(resourceId=value)

    elif element == "text":
        if not driver(text=value).exists:
            result_display.insert("end", "该元素不存在:%s\n" % value)
            return
        else:
            element_list = driver(text=value)

    elif element == "textContains":
        if not driver(textContains=value).exists:
            result_display.insert("end", "该元素不存在:%s\n" % value)
            return
        else:
            element_list = driver(textContains=value)

    elif element == "packageName":
        if not driver(packageName=value).exists:
            result_display.insert("end", "该元素不存在:%s\n" % value)
            return
        else:
            element_list = driver(packageName=value)

    elif element == "className":
        if not driver(className=value).exists:
            result_display.insert("end", "该元素不存在:%s\n" % value)
            return
        else:
            element_list = driver(className=value)

    list1 = []
    print(element_list)
    for i in element_list:

        if display_info:  # 打印info信息
            result_display.insert("end", str(i.info) + '\n')
        if display_text:  # 只打印text信息
            result_display.insert("end", str(i.info["text"]) + '\n')
        if display_info or display_text:
            result_display.insert("end", "\n")

        list1.append(i.info['text'])

    result_display.insert("end", str(list1) + '\n\n')

    content["element_id"] = value
    write_data()


def two_list_compare_method(stand_list, actual_list, method, result_display):
    """两个列表比较方式"""

    global content
    error_list1 = []
    if method == "1":
        result_display.insert("end", "****判断标准值是否都在实际值列表中****\n")
        for value in stand_list:
            if value not in actual_list:
                error_list1.append(value)
        if error_list1:
            result_display.insert("end", "标准列表中以下值没有在实际列表中找到:\n")
            for value in error_list1:
                result_display.insert("end", "%s\n" % value)
        else:
            result_display.insert("end", "匹配成功\n")

    elif method == "2":
        result_display.insert("end", "****通过相同下标比较****\n")
        if len(stand_list) != len(actual_list):
            result_display.insert("end", "两个列表的长度不一致\n")
            return

        flag = True
        for i in range(len(stand_list)):
            if stand_list[i] != actual_list[i]:
                flag = False
                result_display.insert("end", stand_list[i] + "\n")
                result_display.insert("end", actual_list[i] + "\n")
                result_display.insert("end", "\n")

        if flag:
            result_display.insert("end", "匹配成功\n")

    elif method == "3":
        result_display.insert("end", "****判断实际列表值是否都在标准值中****\n")
        for value in actual_list:
            if value not in stand_list:
                error_list1.append(value)
        if error_list1:
            result_display.insert("end", "实际列表中以下值没有在标准值中找到:\n")
            for value in error_list1:
                result_display.insert("end", "%s\n" % value)
        else:
            result_display.insert("end", "匹配成功\n")
    result_display.insert("end", "\n")


def clear_terminal(result_display):
    """清空终端屏幕"""

    # 第一值必须是 1.0 ,第二值必须是小数,要删除多少行
    result_display.delete(1.0, 9999999999.0)

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。

文章标题:Tkinter & pyinstaller

本文作者:伟生

发布时间:2021-03-06, 17:26:10

最后更新:2021-05-09, 11:08:59

原始链接:http://yoursite.com/2021/03/06/basic_08_tinker/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏