Advertisement
davidhellam

Python: Text Editor

Aug 11th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. from guizero import App, TextBox,Text, PushButton, Box, Slider, ButtonGroup
  2.  
  3. def fontsize():
  4.     editor.text_size = sl_fontsize.value
  5.  
  6. def fontcolour():
  7.     editor.text_color = bg_fontcolour.value
  8.  
  9. def open_file():
  10.     with open(file_name.value, "r") as f:
  11.         editor.value = f.read()
  12.  
  13. # function for writing files
  14. def save_file():
  15.     with open(file_name.value, "w") as f:
  16.         f.write(editor.value)
  17.  
  18.  
  19. app = App(title="My Text Editor in Python", width=640, height=480, layout="grid")
  20. app.bg=(204,204,255)
  21.  
  22. # create a box to house the controls, we want the box to span the entire width of the app
  23. file_controls = Box(app, align="left", height=20, width=560, grid=[0,0])
  24.  
  25. # create a TextBox for the file name
  26. txt_filelabel=Text(file_controls,text="File Name: ",size=8, align="left")
  27. file_name = TextBox(file_controls, text="example.txt", width=50, align="left")
  28. file_name.bg=(255,255,255)
  29.  
  30. # create a TextBox which is not in the box and fills the rest of the GUI
  31. editspace = Box(app, align="top", height=460, width=560, grid=[0,1])
  32. editor = TextBox(editspace, multiline=True, height="fill", width="fill", scrollbar=True)
  33. editspace.bg=(255,255,255)
  34.  
  35. # create a save button which uses the save_file function
  36. buttonspace = Box(app, align="top", width=80, height=480, grid=[1,0,1,2])
  37. save_button = PushButton(buttonspace, width=6, text="Save", command=save_file)
  38. save_button.bg=(255,204,204)
  39.  
  40. # create an open button which uses the open_file function
  41. open_button = PushButton(buttonspace, text="Open", width=6, command=open_file)
  42. open_button.bg=(204,255,204)
  43.  
  44. # create a slider for font size
  45. sl_fontsize = Slider(buttonspace,start=6,end=36,horizontal=False, command=fontsize)
  46.  
  47. # create a button group for text colour
  48. bg_fontcolour = ButtonGroup(buttonspace, options=["Black", "Red", "Green", "Blue"], selected="Black", command=fontcolour)
  49.  
  50.  
  51. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement