Advertisement
timber101

TextEditorwithMenus

Aug 14th, 2019
402
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None
  1. from guizero import App, TextBox, PushButton, Box, Combo, Slider, MenuBar
  2.  
  3. app = App(title="texteditor")
  4.  
  5.  
  6. # function for reading files
  7. def open_file():
  8.     with open(file_name.value, "r") as f:
  9.         editor.value = f.read()
  10.  
  11. # function for writing files
  12. def save_file():
  13.     with open(file_name.value, "w") as f:
  14.         f.write(editor.value)
  15.     save_button.disable() # disables the save button after saving
  16.     btn_ltr_cnt.disable() # enables the letter count button
  17.  
  18. def enable_save():
  19.     save_button.enable() # enables the save button
  20.     btn_ltr_cnt.enable() # enables the letter count button
  21.      
  22. def change_font():
  23.     editor.font = font.value
  24.    
  25. def exit_app ():
  26.     app.destroy()
  27.  
  28. def change_font_size():
  29.     editor.text_size = txtsize.value
  30.     # 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
  31.     editor.resize(1, 1)
  32.     editor.resize("fill", "fill")
  33.    
  34. def change_font_colour():
  35.     editor.text_color = txtcol.value
  36.  
  37. def count_letters():
  38.     ltr_cnt.value = len(editor.value)-1
  39.  
  40. menubar = MenuBar(app, toplevel = ["File","Font","Count"],
  41.             options = [
  42.             [["open", open_file], ["save", save_file], ["exit", exit_app]],
  43.             [["size", change_font_size],["colour", change_font_colour],["style",change_font]],
  44.             [["count", count_letters]]
  45.             ])
  46.  
  47.  
  48.  
  49. # create a box to house the controls, we want the box to span the entire width of the app
  50.  
  51. file_controls = Box(app, align="top", width="fill")
  52.  
  53. # a padder to pull it away form the edge
  54. pad = Box(file_controls,align = "left", width=5, height=5)
  55.  
  56. # preference controls
  57. pref_controls = Box(app,align="bottom",width = "fill", border = True)
  58. font = Combo(pref_controls,options=["courier", "times new roman", "verana","arial"], align="left", command=change_font)
  59. txtsize = Slider(pref_controls, start = 8, end = 30, command = change_font_size, align = "left")
  60. txtcol = Combo(pref_controls, options=["green","blue","back","grey"], command = change_font_colour, align = "left")
  61. ltr_cnt = TextBox(pref_controls, align ="left")
  62. btn_ltr_cnt = PushButton(pref_controls, command= count_letters, text = "Count", align ="left", enabled = False )
  63.  
  64.  
  65. # create a TextBox for the file name
  66. file_name = TextBox(file_controls, text="text_file.txt", width="fill", align="left")
  67.  
  68. # create a save button which uses the save_file function
  69. save_button = PushButton(file_controls, text="Save", command = save_file and count_letters,  align="right", enabled = False)
  70.  
  71. # create an open button which uses the open_file function
  72. open_button = PushButton(file_controls, text="Open", command = open_file, align="right")
  73.  
  74. # create a TextBox which is not in the box and fills the rest of the GUI
  75. editor = TextBox(app, multiline=True, command= enable_save, height="fill", width="fill")
  76.  
  77.  
  78.  
  79. app.display()
Advertisement
RAW Paste Data Copied
Advertisement