xavicano

Untitled

Aug 18th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box, Combo, MenuBar, info,CheckBox
  2.  
  3.  
  4. # function for reading files
  5. def open_file():
  6.     with open(file_name.value, "r") as f:
  7.         editor.value = f.read()
  8.  
  9. # function for writing files
  10. def save_file():
  11.     with open(file_name.value, "w") as f:
  12.         f.write(editor.value)
  13.         # Disable the save_button
  14.     save_button.hide()
  15.    
  16. # A new function that closes the app
  17. def exit_app():
  18.     if save_button.enabled == True:
  19.         app.warn("Warning!", "You have not saved the changes")
  20.     else:
  21.         app.destroy()
  22.  
  23. # function for change size font
  24. def font_button():
  25.     editor.font=font_combo.value
  26.        
  27. # function for change align DOES NOT WORK
  28. def align_button():
  29.     editor.align=align_combo.value
  30.    
  31. # function for change color  
  32. def color_button():
  33.     editor.text_color=color_combo.value
  34.     #if color_combo.value=="red":
  35.         #size_combo.disable()
  36.     #else:
  37.         #size_combo.enable()   
  38.    
  39. # function for change font
  40. def size_button():
  41.     editor.text_size=size_combo.value  
  42.     editor.resize(1, 1)
  43.     editor.resize("fill", "fill")    
  44.  
  45. def enable_save():
  46.     save_button.show()
  47.    
  48. def dark_mode():
  49.     if dark_button.value == True:
  50.         editor.bg = "black"
  51.         editor.text_color = "white"
  52.         dark_button.value = False
  53.     else:
  54.         editor.bg = "white"
  55.         editor.text_color = "black"
  56.         dark_button.value = True
  57.  
  58.  
  59. app = App(title="texteditor")
  60.  
  61.  
  62. # The new MenuBar
  63. menubar = MenuBar(app,
  64.                   toplevel=["File","Edit"],
  65.                   options=[
  66.                     [["open",open_file],["save",save_file],["exit",exit_app]],
  67.                     [["Font",font_button],["Size",size_button],["Align",align_button],["Color",color_button], ]
  68.                     ])
  69.  
  70.  
  71. # create a box to house the controls, we want the box to span the entire width of the app
  72. file_controls = Box(app, align="top", width="fill", border=True)
  73. format_controls=Box(app, align="top", width="fill")
  74.  
  75. # create a TextBox for the file name
  76. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  77.  
  78. # create a save button which uses the save_file function
  79. save_button = PushButton(file_controls, text="Save", command=save_file, align="right")
  80.  
  81. # create an open button which uses the open_file function
  82. open_button = PushButton(file_controls, text="Open", command=open_file,  align="right")
  83.  
  84. # create an open button which uses the open_file function
  85. dark_button = CheckBox(format_controls, text="Dark", align="right", command=dark_mode)
  86.  
  87.  
  88. # create an open button which uses the open_file function
  89. color_combo = Combo(format_controls, options=["red","green","blue","cyan","black"], selected="black", align="right", command=color_button)
  90.  
  91. # create an open button which uses the open_file function
  92. font_combo = Combo(format_controls, options=["Arial", "Helevetica","Courier"], selected="Courier", align="right", command=font_button)
  93.  
  94. # create an open button which uses the open_file function
  95. size_combo = Combo(format_controls, options=[6,8,10,12,14,16,18,20], selected="10", align="right", command=size_button)
  96.  
  97. # create an open button which uses the open_file function
  98. align_combo = Combo(format_controls, options=["left","right","top","bottom"], width=40,selected="left", align="right", command=align_button)
  99.  
  100.  
  101.  
  102. # create a TextBox which is not in the box and fills the rest of the GUI
  103. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  104.  
  105. app.display()
Advertisement
Add Comment
Please, Sign In to add comment