Advertisement
timber101

advanced text editor

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