python3 一个小小的随机成语app

2020-06-07  本文已影响0人  fanchuang

成语.json 提取码: 9ws3

Screenshot from 2020-06-03 17-56-44.png
# author: fanchuangwater@gmail.com
# date: 2020/6/3 下午4:19
# 目的: 

import pathlib
import json
import random
import PySimpleGUI as sg


def read_file():
    p = pathlib.Path("./idiom-dirty.json")
    # 从 pathlib 直接读取文件
    f = p.open('r')
    data = json.load(f)
    # print(len(d))       # 31648
    item = random.choice(data)
    return item


def gui():
    c = read_file()
    sg.theme('Dark Purple')

    layout = [
        [sg.Text("成语: "), sg.Text(c["word"], key="core")],
        [sg.Button('解释'), sg.Button('出处'), sg.Button('例子')],
        [sg.Button('下一个'), sg.Button('退出')]
    ]
    window = sg.Window('随机成语', layout)
    while True:
        event, values = window.read()
        if event in (None, '退出'):
            break
        if event == "解释":
            sg.popup(c["explanation"])
        if event == "出处":
            sg.popup(c["derivation"])
        if event == "例子":
            sg.popup(c["example"])
        if event == "下一个":
            c = read_file()
            window['core'].update(c['word'])

    window.close()


if __name__ == '__main__':
    gui()

上一篇下一篇

猜你喜欢

热点阅读