Advertisement
john_feleppa

GUIs 2.12

Aug 20th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. from guizero import App, TextBox, PushButton, Box, Combo, CheckBox, Slider, MenuBar
  2.  
  3. def open_file():
  4. with open(file_name.value, "r") as f:
  5. editor.value = f.read()
  6.  
  7. def save_file():
  8. with open(file_name.value, "w") as f:
  9. f.write(editor.value)
  10. # Disable the save_button
  11. save_button.disable()
  12. # Enable the open_button
  13. open_button.enable()
  14.  
  15. def change_font():
  16. editor.font = font.value
  17.  
  18. def change_text_size():
  19. editor.text_size = size.value
  20. editor.resize(1, 1)
  21. editor.resize("fill", "fill")
  22.  
  23. def change_font_colour():
  24. editor.text_color = font_colour.value
  25.  
  26. def change_bg_color(col):
  27. editor.bg = col
  28.  
  29. def enable_save():
  30. save_button.enable()
  31. open_button.disable()
  32.  
  33. def preference_control_toggle():
  34. if preference_chkbox.value == True:
  35. preferences_controls.show()
  36. else:
  37. preferences_controls.hide()
  38.  
  39. def dark_mode():
  40. if dark_chkbox.value == True:
  41. editor.bg = "black"
  42. editor.text_color = "white"
  43. dark_chkbox.value = False
  44. else:
  45. editor.bg = "white"
  46. editor.text_color = "black"
  47. dark_chkbox.value = True
  48.  
  49. def exit_app():
  50. if save_button.enabled == True:
  51. app.warn("Warning!", "You have not saved the changes")
  52. else:
  53. app.destroy()
  54.  
  55. app = App(title="textzero")
  56.  
  57. # Menu bar
  58. menubar = MenuBar(app,
  59. # These are the menu options
  60. toplevel=["File", "Edit", "Background"],
  61. # The options are recorded in a nested lists, one list for each menu option
  62. # Each option is a list containing a name and a function
  63. options=[[ ["Open file", open_file], ["Save file", save_file], ["Exit",exit_app] ],
  64. [ ["Change font", change_font], ["Change text size", change_text_size], ["Change font colour", change_font_colour] ],
  65. [["Orange",lambda: change_bg_color('orange')], ["Yellow",lambda: change_bg_color('yellow')],
  66. ["magenta",lambda: change_bg_color('magenta')], ["Reset",lambda: change_bg_color('white')]]
  67. ])
  68.  
  69. # One box to house the controls
  70. file_controls = Box(app, align="bottom", width="fill", border=True)
  71. preferences_controls = Box(app, align="top", width="fill", border=True, visible=False)
  72. preference_chkbox = CheckBox(file_controls, text="View options", align="left", command=preference_control_toggle)
  73. dark_chkbox = CheckBox(file_controls, text="Dark", align="right", command=dark_mode)
  74.  
  75. # One slider for font sizes
  76. size = Slider(preferences_controls, align="left", start = 8, end = 18, command=change_text_size)
  77.  
  78. # Font controls
  79. font = Combo(preferences_controls, options=["Courier", "Century Gothic", "Verdana"], align="left", command=change_font)
  80. font_colour = Combo(preferences_controls, options=["black", "orange", "purple"], align="right", command=change_font_colour)
  81.  
  82. # File name text box
  83. file_name = TextBox(file_controls, text="text_file.txt", width=24, align="left")
  84.  
  85. # Buttons
  86. save_button = PushButton(file_controls, text="Save", command=save_file, align="right", enabled=False)
  87. open_button = PushButton(file_controls, text="Open", command=open_file, align="right")
  88.  
  89. # TextBox to fill remainder of GUI
  90. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  91.  
  92. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement