Advertisement
davidhellam

Python: Text Editor 3

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