用python写一个md记事本
时间: 2024-05-07 22:23:11 浏览: 136
以下是一个简单的 Python Markdown 记事本的示例代码,使用了 PySimpleGUI 和 markdown 库:
```python
import PySimpleGUI as sg
import markdown
sg.theme('DarkGrey3')
layout = [
[sg.Menu([['File', ['New', 'Open', 'Save', '---', 'Exit']]], tearoff=False)],
[sg.Multiline(size=(80, 20), font=('Helvetica', 12), key='-EDITOR-'),
sg.VerticalSeparator(),
sg.Multiline(size=(80, 20), font=('Helvetica', 12), key='-VIEWER-')],
]
window = sg.Window('Markdown Editor', layout)
filename = None
while True:
event, values = window.read()
if event in (None, 'Exit'):
break
if event == 'New':
filename = None
window['-EDITOR-'].update('')
window['-VIEWER-'].update('')
continue
if event == 'Open':
filename = sg.popup_get_file('Open File', no_window=True)
if filename:
with open(filename, 'r') as f:
content = f.read()
window['-EDITOR-'].update(content)
window['-VIEWER-'].update(markdown.markdown(content))
continue
if event == 'Save':
if filename:
with open(filename, 'w') as f:
f.write(values['-EDITOR-'])
sg.popup('File saved successfully!', no_titlebar=True)
else:
filename = sg.popup_get_file('Save As', save_as=True, no_window=True)
if filename:
with open(filename, 'w') as f:
f.write(values['-EDITOR-'])
sg.popup('File saved successfully!', no_titlebar=True)
continue
if event == '-EDITOR-':
window['-VIEWER-'].update(markdown.markdown(values['-EDITOR-']))
window.close()
```
这个记事本的界面分为两个部分,左边是编辑区,右边是预览区。你可以在编辑区输入 Markdown 语法的文本,右边的预览区会实时更新 Markdown 渲染后的效果。
程序使用了 PySimpleGUI 创建了一个简单的 GUI 界面,并使用 Markdown 库将编辑区的文本渲染成 HTML,然后显示在预览区中。菜单栏提供了新建、打开、保存和退出等基本功能。
阅读全文
相关推荐


















