Advertisement
Guest User

13_text_editor_challenge.py

a guest
Aug 17th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.82 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box, Combo, Slider, MenuBar, Text, yesno, CheckBox
  2.  
  3. # function for reading files
  4. def open_file():
  5.     if (lbl_needSave.visible):
  6.         answer = yesno("Warning!!!","The editor has unsaved changes; open the other file anyway?")
  7.         if (answer):
  8.             open_file_confirmed()
  9.     else:
  10.         open_file_confirmed()
  11.  
  12. def open_file_confirmed():
  13.     with open(file_name.value, "r") as f:
  14.         editor.value = f.read()
  15.     # Disable the save_button
  16.     save_button.disable()
  17.     #save_button.hide()
  18.     lbl_needSave.visible = False
  19.  
  20. # function for writing files
  21. def save_file():
  22.     with open(file_name.value, "w") as f:
  23.         f.write(editor.value)
  24.     # Disable the save_button
  25.     save_button.disable()
  26.     #save_button.hide()
  27.     lbl_needSave.visible = False
  28.  
  29. # A new function that closes the app
  30. def exit_app():
  31.     if (lbl_needSave.visible):
  32.         answer = yesno("Warning!!!","The editor has unsaved changes; exit anyway?")
  33.         if (answer):
  34.             exit_app_confirmed()
  35.     else:
  36.         exit_app_confirmed()
  37.  
  38. def exit_app_confirmed():
  39.     app.destroy()
  40.    
  41. def enable_save():
  42.     save_button.enable()
  43.     #save_button.show()
  44.     lbl_needSave.visible = True
  45.  
  46. def change_font():
  47.     print(editor.font)
  48.     editor.font = font.value
  49.  
  50. def change_text_size():
  51.     editor.text_size = size.value
  52.     # resize the widget because if the text is made bigger, this might affect the size of the TextBox so guizero needs to know how to maintain the intended layout
  53.     editor.resize(1, 1)
  54.     editor.resize("fill", "fill")
  55.  
  56. def change_text_color():
  57.     editor.text_color = text_col.value
  58.    
  59. def change_bg_color(col):
  60.     editor.bg = col
  61.  
  62. def toggle_mode():
  63.     app.bg = "black" if checkbox_mode.value == 1 else "light grey"
  64.     app.text_color = "white" if checkbox_mode.value == 1 else "black"
  65.     text_col.value = app.text_color
  66.  
  67. app = App(title="texteditor")
  68.  
  69. # The new MenuBar
  70. menubar = MenuBar(app,
  71.                   toplevel=["File", "Background color"],
  72.                   options=[
  73.                       [["open",open_file], ["save",save_file], ["exit",exit_app]],
  74.                       [["red",lambda: change_bg_color('red')], ["blue",lambda: change_bg_color('blue')], ["cyan",lambda: change_bg_color('cyan')], ["magenta",lambda: change_bg_color('magenta')]]
  75.                       ])
  76.  
  77. # create a box to house the controls, we want the box to span the entire width of the app
  78. file_controls = Box(app, align="top", width="fill")
  79.  
  80. # create a TextBox for the file name
  81. file_name = TextBox(file_controls, text="10_text_file.txt", width=50, align="left")
  82.  
  83. # label that contains an asterisk, used to show wheter the save is needed or not
  84. lbl_needSave = Text(file_controls, text="*", size=16, align="left", visible=False)
  85.  
  86. # create a save button which uses the save_file function
  87. save_button = PushButton(file_controls, text="Save",  align="right", command=save_file, enabled=False)
  88.  
  89. # create an open button which uses the open_file function
  90. open_button = PushButton(file_controls, text="Open",  align="right", command=open_file)
  91.  
  92. # create a TextBox which is not in the box and fills the rest of the GUI
  93. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  94.  
  95. preferences_controls = Box(app, align="bottom", width="fill", border=True)
  96. font = Combo(preferences_controls, options=["courier new", "times new roman", "verdana"], align="left", command=change_font)
  97. size = Slider(preferences_controls,  align="left", command=change_text_size, start=10, end=60)
  98. text_col = Combo(preferences_controls, options=["black", "blue", "green", "gray", "magenta", "white"], align="left", command=change_text_color)
  99. checkbox_mode = CheckBox(preferences_controls, text="Dark mode", align="right", command=toggle_mode)
  100.  
  101. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement