PC语音识别GUI设计(接上篇)

复制就行

不多逼逼

import tkinter as tk
from tkinter import *
from tkinter import messagebox
import os
import codecs

检查并创建命令文件
key_file = "./API_KEY.txt"
def order_file():
    if not os.path.exists(key_file):
        with codecs.open(key_file, 'a+', encoding='utf-8') as f:
            f.write("")
检查并创建热键
button_file = "./button_KEY.txt"
def button():
    if not os.path.exists(button_file):
        with codecs.open(button_file, 'a+', encoding='utf-8') as f:
            f.write("")
执行
order_file()
button()
运行标识

hf = {'hf1':1,'hf2':1,'hf3':1,'hf4':1}
主窗口函数
def window_0():
    #退出主窗口函数
    def close():
        window_0.destroy()

    # 主窗口创建
    window_0 = tk.Tk()
    # 窗口大小
    window_0.geometry('450x280+450+200')
    # 设置GUI标题
    window_0.title('PC语音助手')
    # 设置GUI透明度(0.0~1.0)
    window_0.wm_attributes("-alpha", 1.0)
    # 设置为工具窗口(没有放大和缩小按钮)
    window_0.wm_attributes("-toolwindow", True)
    # 设置GUI置顶
    # window.wm_attributes("-topmost", True)
    # 去除GUI边框(GUI标题、放大缩小和关闭按钮都会消失)
    # window.overrideredirect(-1)
    # 图片
    photo = tk.PhotoImage(file=r".\fox.gif")
    imgLabel = tk.Label(window_0, image=photo)
    imgLabel.pack(side=tk.RIGHT)
    welcome_word_1 = tk.Label(text="欢迎使用", font=("黑体", 25)).pack(
        side="top")
    welcome_word_2 = tk.Label(text="—02.2.7版",
                              font=("黑体", 12)).pack(
        side="top")
    line_1 = tk.Label(text="--------------------",
                      font=("黑体", 12)).pack(side="top")
    # 主窗口按键布局

    button_01 = tk.Button(window_0, text='语音|地址设置', font=("黑体", 15),
                          bg="#f5f5f5", fg="#000000",
                          width=15, command=win_1).pack(side="top")
    linell1 = tk.Label(text="     ",
                      font=("黑体", 1)).pack(side="top")
    button_02 = tk.Button(window_0, text='API更新', font=("黑体", 15),
                          bg="#f5f5f5", fg="#000000",
                          width=15, command=win_2).pack(side="top")
    linell2 = tk.Label(text="     ",
                       font=("黑体", 1)).pack(side="top")
    button_03 = tk.Button(window_0, text='唤醒热键设置', font=("黑体", 15),
                          bg="#f5f5f5", fg="#000000",
                          width=15, command=win_3).pack(side="top")
    linell3 = tk.Label(text="     ",
                       font=("黑体", 1)).pack(side="top")
    button_04 = tk.Button(window_0, text='退出设置导航', font=("黑体", 15),
                          bg="#f5f5f5", fg="#000000",
                          width=15, command=close).pack(side="top")

    # 主窗口循环

    window_0.mainloop()

窗体三

def win_3():
    def hf4():
        hf["hf4"] = 1
        win_3.destroy()

    # 存入热键
    def hot_button():
        hot_but = button_entry.get()
        print(hot_but)
        with open(button_file,'w',encoding='utf-8') as f:
            f.write(hot_but)
        button_old = tk.Label(win_3, text="\n{}".format(hot_but),
                              font=("黑体", 15)).grid(row=1, column=2)

        # 窗体创建
    if hf["hf4"] == 1:
        hf["hf4"] = 0
        win_3 = tk.Tk()
        win_3.wm_attributes("-toolwindow", True)
        win_3.title('热键设置')
        win_3.geometry('300x150+600+300')
        win_3.protocol('WM_DELETE_WINDOW', hf4)
        w_old = tk.Label(win_3, text="\n当前热键为:", font=("黑体", 15))
        w_old.grid(row=1, column=1)
        button_value = open(button_file, 'r', encoding='utf-8')
        lines_b = button_value.readlines()
        for line in lines_b:
            if line == 0:
                print("故障")
            else:
                button_old = tk.Label(win_3, text="\n{}".format(line),
                                      font=("黑体", 15))
                button_old.grid(row=1, column=2)
        w_new = tk.Label(win_3, text="\n新热键为:", font=("黑体", 15))
        w_new.grid(row=2, column=1)
        button_entry = tk.Entry(win_3, highlightcolor='red',
                                highlightthickness=1)
        button_entry.grid(row=2, column=2)
        ok_button = tk.Button(win_3, text="OK",
                              font=("黑体", 15), command=hot_button)
        ok_button.grid(row=3, column=2)

窗体二

def win_2():
    def hf3():
        hf["hf3"] = 1
        win_2.destroy()
    # getAPI数据
    def api_save():
        ad = ad_e.get()
        ak = ak_e.get()
        sk = sk_e.get()
        if len(ad) == 0 or len(ak) == 0 or len(sk) == 0:
            tk.messagebox.showinfo('警告', '输入不能为空,返回上一级')
            hf3()
            print("输入为空")
        else:
            # api_dir = {"ad": "None", "ak": "None",  "sk": "None"}
            # api_dir["ad"] = ad
            # api_dir["ak"] = ak
            # api_dir["sk"] = sk
            # import json
            # print(api_dir)
            # json_str = json.dumps(api_dir)  # dumps
            # 打开KEY.txt写入
            with open(key_file, 'w', encoding='utf-8') as f:
                f.write(ad)
                f.write("\n{}".format(ak))
                f.write("\n{}".format(sk))
            hf3()

    # 窗体创建

    if hf["hf3"] == 1:
        hf["hf3"] = 0
        win_2 = tk.Tk()
        win_2.wm_attributes("-toolwindow", True)
        win_2.title('API填写')
        win_2.geometry('300x200+600+300')
        win_2.protocol('WM_DELETE_WINDOW', hf3)
        instructions = tk.Label(win_2, text="以下API参数通过百度智能云免费获取",
                                font=("黑体", 12))
        instructions.pack(side="top")
        APP_ID_value = tk.Label(win_2,text="APP ID:",
                                font=("黑体", 12)).pack(side="top")
        ad_e = tk.Entry(win_2)
        ad_e.pack(side="top")
        API_KEY_value = tk.Label(win_2, text="API KEY:",
                                font=("黑体", 12)).pack(side="top")
        ak_e = tk.Entry(win_2)
        ak_e.pack(side="top")
        SECRET_KEY_value = tk.Label(win_2, text="SECRET KEY:",
                                font=("黑体", 12)).pack(side="top")
        sk_e = tk.Entry(win_2)
        sk_e.pack(side="top")
        line_21 = tk.Label(win_2, text="---------------------------------------",
                           font=("黑体", 2))
        line_21.pack(side="top")
        ok_save_b = tk.Button(win_2, text='保存', font=("黑体", 12),
                              bg="#f5f5f5", fg="#000000",
                              width=5, command=api_save)
        ok_save_b.pack(side="top")

窗体一

def win_1():
    def hf1():
        hf["hf1"] = 1
        window_1.destroy()
    # 写入命令
    def go():
        app_id = e1.get()
        app_url = e2.get()
        print(e1.get())
        print(e2.get())
        if len(app_id) == 0 or len(app_url) == 0:
            tk.messagebox.showinfo('警告', '输入不能为空,返回上一级')
            hf1()
            print("输入为空")
        else:
            with open('./audio_order.txt', 'a', encoding='utf-8') as f:
                f.write('\n{},{}'.format(app_id, app_url))

        # 输入后清空
        e1.delete(0,END)
        e2.delete(0,END)
    if hf["hf1"] == 1:
        hf["hf1"] = 0
        #  #############################################################################
        def thelb():
            #  ########################################
            def save_order():
                NUM = theLB.size()
                print(NUM)
                tuple_1 = theLB.get(0, NUM)
                print(tuple_1)
                val = list(tuple_1)
                print(val)
                NUM = theLB.size()
                print(NUM)
                tuple_1 = theLB.get(0, NUM)
                print(tuple_1)
                val = list(tuple_1)
                print(val)
                with open('./audio_order.txt', 'w',
                          encoding='utf-8') as f:
                    f.write('')
                for item in val:
                    if len(item) > 0:
                        print("写入:",item)
                        with open('./audio_order.txt', 'a+',
                                  encoding='utf-8') as f:
                            f.write('\n{}'.format(item))
            # #######################################################

            # 滑块
            sb = Scrollbar(window_1)
            sb.grid(row=3, column=2)
            # 列表
            theLB = Listbox(window_1,yscrollcommand=sb.set)
            theLB.grid(row=3, column=1)
            window_1.geometry('300x340')
            # 文件读取
            audio_order_file = './audio_order.txt'
            fileRead = open(audio_order_file, 'r', encoding='utf-8')
            lines = fileRead.readlines()
            print(lines)
            for item in lines:
                if item == "\n":
                    lines.remove(item)
                else:
                    print("数据:", item)
            for item in lines:
                theLB.insert(0, item)
                # (0,END)表示每插入一个都是在第一,最后一个位置

            sb.config(command=theLB.yview)
                # 根据索引位置删除元素,如果是theLB.delete(0,END)表示删除全部
            theLB.delete(-1)
            # window_12.protocol('WM_DELETE_WINDOW', handle_flag_12)
            # 使用删除按钮删除任意的元素
            Button_21 = Button(window_1, text='删除',
                               command=lambda x=theLB: x.delete(ACTIVE))
            Button_21.grid(row=4, column=1)
            Button_21 = Button(window_1, text='保存',
                               command=save_order)
            Button_21.grid(row=5, column=1)

        # ####################################################################
        window_1 = tk.Tk()
        window_1.geometry('300x100+600+300')
        window_1.title('语音|地址设置')
        window_1.wm_attributes("-toolwindow", True)
        tk.Label(window_1, text="   语音内容:").grid(row=0)
        tk.Label(window_1, text="   访问地址:").grid(row=1)
        e1 = tk.Entry(window_1)
        e2 = tk.Entry(window_1)
        e1.grid(row=0, column=1, padx=8, pady=5)
        e2.grid(row=1, column=1, padx=8, pady=5)
        window_1.protocol('WM_DELETE_WINDOW', hf1)
    # window_1按键
        button_12 = tk.Button(window_1, text="查看命令",command=thelb,
                              state=NORMAL).grid(row=2, column=1)
        button_13 = tk.Button(window_1, text="退出",
                              command=hf1,
                              state=NORMAL).grid(row=2, column=2)
        button_11 = tk.Button(window_1, text="添加", command=go,
                              state=NORMAL).grid(row=2, column=0)
        window_1.mainloop()
    else:
        print("当前,重复无效点击")
window_0()

Original: https://blog.csdn.net/qq_62253883/article/details/122867953
Author: 菠萝仙人
Title: PC语音识别GUI设计(接上篇)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/515089/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球