Advertisement
Guest User

editor4.py

a guest
Dec 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from guizero import App,TextBox,PushButton,Box,Combo,Slider,MenuBar,CheckBox
  4. from tkinter import filedialog
  5.  
  6. revertText = ""
  7.  
  8. def OpenFile():
  9.     global revertText
  10.     filepath = filedialog.askopenfilename()
  11.     file_name.value = filepath
  12.     f = open(filepath,"r")
  13.     editor.value = f.read()
  14.     revertText = editor.value
  15.     save_button.hide()
  16.     f.close()
  17.    
  18. def SaveFile():
  19.     global revertText
  20.     filepath = filedialog.asksaveasfile()
  21.     if filepath:
  22.         f = open(filepath.name,"w")
  23.         f.write(editor.value)
  24.         revertText = editor.value
  25.         save_button.hide()
  26.         open_button.enable()
  27.         revert_button.disable()
  28.         f.close()
  29.        
  30. def ChangeFont():
  31.     editor.font = font.value
  32.    
  33. def ChangeTextSize():
  34.     editor.text_size = size.value
  35.     editor.resize(1,1)
  36.     editor.resize("fill","fill")
  37.  
  38. def ChangeColor():
  39.     editor.text_color = color.value
  40.    
  41. def TextChanged():
  42.     global revertText
  43.  
  44.     editText = editor.value
  45.     compareText = revertText
  46.     if editText == compareText:
  47.         save_button.hide()
  48.         open_button.enable()
  49.         revert_button.disable()
  50.     else:
  51.         save_button.show()
  52.         open_button.disable()
  53.         revert_button.enable()
  54.  
  55. def Revert():
  56.     global revertText
  57.  
  58.     editor.value = revertText
  59.     revertText = editor.value # weird guizero artificat if not done
  60.     save_button.hide()
  61.     open_button.enable()
  62.     revert_button.disable()
  63.  
  64. # A new function that closes the app
  65. def ExitApp():
  66.     global revertText
  67.    
  68.     if revertText != editor.value:
  69.         quitNow = app.yesno('Warning!',
  70.                            'Quit without saving changes?')
  71.         if not quitNow:
  72.             return
  73.  
  74.     app.destroy()
  75.  
  76. def DarkMode():
  77.     if darkMode.value:
  78.         app.bg = "black"
  79.         editor.saved_text_color = editor.text_color
  80.         app.text_color = "white"
  81.         editor.text_color = editor.saved_text_color
  82.     else:
  83.         app.bg = "white"
  84.         editor.saved_text_color = editor.text_color
  85.         app.text_color = "black"
  86.         editor.text_color = editor.saved_text_color
  87.        
  88. app = App(title="texteditor")
  89.  
  90. # The new MenuBar
  91. menubar = MenuBar(app,
  92.                   toplevel=["File","Edit"],
  93.                   options=[[["open",OpenFile],["save",SaveFile],
  94.                             ["exit",ExitApp]],
  95.                            [["revert",Revert]]])
  96.  
  97. # create a box to house the controls, we want the box to span the entire width of the app
  98. file_controls = Box(app, align="top", width="fill")
  99.  
  100. # create a TextBox for the file name
  101. file_name = TextBox(file_controls, text="text_file.txt", width=32, align="left")
  102.  
  103. # create a save button which uses the save_file function
  104. save_button = PushButton(file_controls,text="Save",align="right",
  105.                          visible=False,command=SaveFile)
  106.  
  107. # create an open button which uses the open_file function
  108. open_button = PushButton(file_controls,text="Open",align="right",
  109.                          command=OpenFile)
  110.  
  111. revert_button = PushButton(file_controls,text="Revert",align="right",
  112.                            enabled=False,command=Revert)
  113.  
  114. preferences_controls = Box(app, align="top", width="fill", border=True)
  115. font = Combo(preferences_controls, options=["courier", "times new roman", "verdana"],
  116.              align="left", command=ChangeFont)
  117. color = Combo(preferences_controls, options=["black", "red", "blue", "green"],
  118.              align="left", command=ChangeColor)
  119. size = Slider(preferences_controls,  align="left",
  120.               start = 8, end = 42, command=ChangeTextSize)
  121. darkMode = CheckBox(preferences_controls,text="Dark Mode",
  122.                     align="left",command=DarkMode)
  123.  
  124. # create a TextBox which is not in the box and fills the rest of the GUI
  125. editor = TextBox(app, multiline=True, height="fill", width="fill",command=TextChanged)
  126.  
  127.  
  128. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement