Guest User

Untitled

a guest
Jun 27th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #~~~~imports~~~~
  2. import PySimpleGUI as sg
  3.  
  4. import pathlib
  5.  
  6. WIN_W = 90
  7. WIN_H = 25
  8. #~~~~layout~~~~
  9. # settings
  10. menu_def = [['File', ['New Ctrl+N', 'Open... Ctrl+O', 'Save Ctrl+S','Save As...', '---', 'Exit' ]],
  11. ['Edit', ['Undo Ctrl+Z', 'Paste Ctrl+V'], ],
  12. ['Help', ' About memopad '], ]
  13.  
  14. # the layout its self
  15. layout = [[sg.Menu(menu_def)],
  16. [sg.Multiline(font=('Consolas', 12), size=(WIN_W, WIN_H), key='_BODY_')]]
  17.  
  18. # reading the window
  19. window = sg.Window('untitled - memopad', layout=layout, element_padding=(0, 0), margins=(0, 0), resizable=True, return_keyboard_events=True, finalize=True)
  20. window['_BODY_'].expand(expand_x=True, expand_y=True)
  21.  
  22. #~~~~defs~~~~
  23. def new_file():
  24. '''Reset body and info bar, and clear filename variable'''
  25. window['_BODY_'].update(value='')
  26. window.TKroot.title('untitled - memopad')
  27. file = None
  28. return file
  29.  
  30. def open_file():
  31. '''Open file and update the infobar'''
  32. filepath = sg.popup_get_file('Open', no_window=True)
  33. filename = filepath.split('/')
  34. if filename:
  35. try:
  36. file = pathlib.Path(filepath)
  37. window['_BODY_'].update(value=file.read_text())
  38. window.TKroot.title(filename[-1] + '- memopad')
  39. return file
  40. except:
  41. pass
  42.  
  43. def save_file(file):
  44. '''Save file instantly if already open; otherwise use `save-as` popup'''
  45. try:
  46. if file:
  47. file.write_text(values.get('_BODY_'))
  48. else:
  49. save_file_as()
  50.  
  51. def save_file_as():
  52. '''Save new file or save existing file with another name'''
  53. filename = sg.popup_get_file('Save As', save_as=True, no_window=True)
  54. try:
  55. if filename:
  56. file = pathlib.Path(filepath)
  57. file.write_text(values.get('_BODY_'))
  58. filename = filepath.split('/')
  59. window.TKroot.title(filename[-1] + '- memopad')
  60. return file
  61. except:
  62. pass
  63.  
  64. def about_me():
  65. '''A short, pithy quote'''
  66. sg.popup_no_wait('"All great things have small beginnings" - Peter Senge')
  67.  
  68. #~~~event loop~~~~
  69. while True:
  70. event, values = window.read()
  71. if event in (None, 'Exit'):
  72. break
  73.  
  74. if event in ('New Ctrl+N', 'n:78'):
  75. file = new_file()
  76.  
  77. if event in ('Open... Ctrl+O', 'o:79'):
  78. file = open_file()
  79.  
  80. if event in ('Save Ctrl+S', 's:83'):
  81. save_file(file)
  82.  
  83. if event in ('Save As...',):
  84. file = save_file_as()
  85.  
  86. if event in (' About memopad ',):
  87. about_me()
Add Comment
Please, Sign In to add comment