Advertisement
davidhellam

Python: Text Editor 2

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