Visitar URL original
feat: add PySimpleGUI calculator & notepad GUI demo scripts by zhouzihanzzz · Pull Request #115 · trekhleb/learn-python · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add PySimpleGUI calculator & notepad GUI demo scripts
  • Loading branch information
zhouzihanzzz committed Jun 8, 2026
commit 052b5e98da508e42e82202da0d7e44e1cfba34be
29 changes: 29 additions & 0 deletions src/Demo_Calculator_Simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import PySimpleGUI as sg

# 界面布局
layout = [
[sg.Input(key='input', size=(20,1)), sg.Button('C')],
[sg.Button('1'),sg.Button('2'),sg.Button('3'),sg.Button('+')],
[sg.Button('4'),sg.Button('5'),sg.Button('6'),sg.Button('-')],
[sg.Button('7'),sg.Button('8'),sg.Button('9'),sg.Button('*')],
[sg.Button('0'),sg.Button('.'),sg.Button('/'),sg.Button('=')]
]
window = sg.Window('简易计算器', layout)
expr = ''
while True:
event, val = window.read()
if event in (None, 'Exit'):
break
if event == 'C':
expr = ''
window['input'].update('')
elif event == '=':
try:
res = eval(expr)
window['input'].update(str(res))
except:
window['input'].update('Error')
else:
expr += event
window['input'].update(expr)
window.close()
36 changes: 36 additions & 0 deletions src/Demo_Notepad_Simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import PySimpleGUI as sg

# 窗口布局
layout = [
[sg.Text("简易记事本")],
[sg.Multiline(size=(60, 20), key="-TEXT-")],
[
sg.Button("保存文件"),
sg.Button("读取文件"),
sg.Button("清空"),
sg.Button("退出")
]
]

window = sg.Window("记事本", layout)

while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, "退出"):
break
if event == "清空":
window["-TEXT-"].update("")
elif event == "保存文件":
path = sg.popup_get_file("保存文件", save_as=True, file_types=(("文本文件", "*.txt"),))
if path:
with open(path, "w", encoding="utf-8") as f:
f.write(values["-TEXT-"])
sg.popup("保存成功!")
elif event == "读取文件":
path = sg.popup_get_file("读取文件", file_types=(("文本文件", "*.txt"),))
if path:
with open(path, "r", encoding="utf-8") as f:
content = f.read()
window["-TEXT-"].update(content)

window.close()