Guest User

editor.py

a guest
Dec 12th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from guizero import App, TextBox, PushButton, Box
  4. from tkinter import filedialog
  5.  
  6. def OpenFile():
  7.     filepath = filedialog.askopenfilename()
  8.     file_name.value = filepath
  9.     f = open(filepath,"r")
  10.     editor.value = f.read()
  11.     f.close()
  12.    
  13. def SaveFile():
  14.     filepath = filedialog.asksaveasfile()
  15.     if filepath:
  16.         f = open(filepath.name,"w")
  17.         f.write(editor.value)
  18.         f.close()
  19.    
  20.  
  21. app = App(title="texteditor")
  22.  
  23. # create a box to house the controls, we want the box to span the entire width of the app
  24. file_controls = Box(app, align="top", width="fill")
  25.  
  26. # create a TextBox for the file name
  27. file_name = TextBox(file_controls, text="text_file.txt", width=32, align="left")
  28.  
  29. # create a save button which uses the save_file function
  30. save_button = PushButton(file_controls,text="Save",align="right",command=SaveFile)
  31.  
  32. # create an open button which uses the open_file function
  33. open_button = PushButton(file_controls,text="Open",align="right",command=OpenFile)
  34.  
  35. # create a TextBox which is not in the box and fills the rest of the GUI
  36. editor = TextBox(app, multiline=True, height="fill", width="fill")
  37.  
  38. app.display()
Advertisement
Add Comment
Please, Sign In to add comment