python3 一个小小的随机成语app
成语.json 提取码: 9ws3
# author: [email protected]
# 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()