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()