python 开发IEnglish (四) pysimplegui创建窗口

main.py:

import PySimpleGUI as sg
import uuid
import playsound
import random
from tkinter.messagebox import showinfo
from wordio import read_words, read_theme, write_theme
from speak import get_sound
from recording import record
from compare import compare_sounds
from gettime import get_sound_time
from views.theme import get_theme
def re_english():
    word_data = read_words(words)
    english, chinese = random.choice(word_data)
    window.find_element('-ENGLISH-').update(english)
    window.find_element('-CHINESE-').update(chinese)
def speak():
    english = window.find_element('-ENGLISH-').get()
    return get_sound(english, f'resources/sounds/{uuid.uuid4()}.wav')
def show_restart():
    showinfo('提示', '设置成功,重启后生效')
theme = read_theme('resources/theme/color.txt')
words = read_theme('resources/theme/word-data.txt')
sg.theme(theme)
sg.set_global_icon('resources/icon/a4.ico')
menu = [
    ['主题', ['窗口主题']]
]
layout = [
    [sg.Menu(menu)],
    [sg.Text(' ' * 180, key='-ENGLISH-')],
    [sg.Text(' ' * 180, key='-CHINESE-')],
    [sg.Button('播放音频', key='-SPEAK-', tooltip='窗口无响应正常'),
     sg.Button('录音', key='-RECORD-', tooltip='窗口无响应正常'),
     sg.Button('刷新', key='-RE_ENGLISH-', tooltip='请执行以初始化')],
    [sg.Quit('退出', key='-QUIT-')]
]
window = sg.Window('Seed English', layout, resizable=True)
sound = None
while True:
    if not window.find_element('-ENGLISH-').get():
        re_english()
    event, values = window.read()
    if event in (None, '-QUIT-'):
        break
    if event == '-RE_ENGLISH-':
        re_english()
        sound = speak()
    elif event == '-SPEAK-':
        playsound.playsound(get_sound('你将听到范例,窗口无响应正常',
                                      f'resources/sounds/{uuid.uuid4()}.wav'))
        if sound:
            playsound.playsound(sound)
    elif event == '-RECORD-':
        if sound:
            time = get_sound_time(sound) + 1
            playsound.playsound(get_sound(f'你将被录音{time}秒,窗口无响应正常,开始录音',
                                          f'resources/sounds/{uuid.uuid4()}.wav'))
            me = record(f'resources/sounds/records/{uuid.uuid4()}.wav', time)
            key = compare_sounds(sound, me)
            key = round(key, 1) * 10
            if key >= 25:
                showinfo('提示', f'已通过测试 得分: {key}/100')
                re_english()
            else:
                showinfo('提示', f'未通过测试 得分: {key}/100')
    elif event == '窗口主题':
        write_theme('resources/theme/color.txt', get_theme())
        show_restart()
window.close()

views/theme.py:

import PySimpleGUI as sg

themes = [
    'Black', 'BlueMono', 'BluePurple', 'BrightColors', 'BrownBlue',
    'Dark', 'Dark2', 'DarkAmber', 'DarkBlack', 'DarkBlack1', 'DarkBlue',
    'DarkBlue1', 'DarkBlue10', 'DarkBlue11', 'DarkBlue12', 'DarkBlue13',
    'DarkBlue14', 'DarkBlue15', 'DarkBlue16', 'DarkBlue17', 'DarkBlue2',
    'DarkBlue3', 'DarkBlue4', 'DarkBlue5', 'DarkBlue6', 'DarkBlue7', 'DarkBlue8',
    'DarkBlue9', 'DarkBrown', 'DarkBrown1', 'DarkBrown2', 'DarkBrown3',
    'DarkBrown4', 'DarkBrown5', 'DarkBrown6', 'DarkBrown7',
    'DarkGreen', 'DarkGreen1', 'DarkGreen2', 'DarkGreen3', 'DarkGreen4',
    'DarkGreen5', 'DarkGreen6', 'DarkGreen7', 'DarkGrey', 'DarkGrey1', 'DarkGrey10',
    'DarkGrey11', 'DarkGrey12', 'DarkGrey13', 'DarkGrey14', 'DarkGrey2',
    'DarkGrey3', 'DarkGrey4', 'DarkGrey5', 'DarkGrey6', 'DarkGrey7',
    'DarkGrey8', 'DarkGrey9', 'DarkPurple', 'DarkPurple1', 'DarkPurple2',
    'DarkPurple3', 'DarkPurple4', 'DarkPurple5', 'DarkPurple6', 'DarkPurple7',
    'DarkRed', 'DarkRed1', 'DarkRed2', 'DarkTanBlue', 'DarkTeal', 'DarkTeal1',
    'DarkTeal10', 'DarkTeal11', 'DarkTeal12', 'DarkTeal2', 'DarkTeal3', 'DarkTeal4',
    'DarkTeal5', 'DarkTeal6', 'DarkTeal7', 'DarkTeal8', 'DarkTeal9', 'Default',
    'Default1', 'DefaultNoMoreNagging', 'Green', 'GreenMono',
    'GreenTan', 'HotDogStand', 'Kayak', 'LightBlue', 'LightBlue1', 'LightBlue2',
    'LightBlue3', 'LightBlue4', 'LightBlue5', 'LightBlue6', 'LightBlue7', 'LightBrown',
    'LightBrown1', 'LightBrown10', 'LightBrown11', 'LightBrown12', 'LightBrown13',
    'LightBrown2', 'LightBrown3', 'LightBrown4', 'LightBrown5', 'LightBrown6',
    'LightBrown7', 'LightBrown8', 'LightBrown9', 'LightGray1', 'LightGreen',
    'LightGreen1', 'LightGreen10', 'LightGreen2', 'LightGreen3', 'LightGreen4',
    'LightGreen5', 'LightGreen6', 'LightGreen7', 'LightGreen8', 'LightGreen9',
    'LightGrey', 'LightGrey1', 'LightGrey2', 'LightGrey3', 'LightGrey4', 'LightGrey5',
    'LightGrey6', 'LightPurple', 'LightTeal', 'LightYellow', 'Material1', 'Material2',
    'NeutralBlue', 'Purple', 'Python', 'Reddit', 'Reds', 'SandyBeach', 'SystemDefault',
    'SystemDefault1', 'SystemDefaultForReal', 'Tan', 'TanBlue', 'TealMono', 'Topanga'
]

def get_theme():
    layout = [
        [sg.Text('选择窗口主题: '), sg.Combo(themes, 'SystemDefault', readonly=True,
                                       key='-THEME-')],
        [sg.Submit('确定')]
    ]
    window = sg.Window('选择窗口主题', layout, disable_close=True, disable_minimize=True)
    while True:
        event, values = window.read()
        if event == '确定':
            result = values['-THEME-']
            break
            
    window.close()
    return result

你可能感兴趣的:(python,开发IEnglish,python,python,gui,themes)