Advertisement
Mori007

MenuBarChalange2

Feb 11th, 2021
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. from guizero import App, Text, PushButton, TextBox, Box, Combo, CheckBox, MenuBar
  2.  
  3. # function to save and open file
  4. def open_file():
  5.     with open(file_name.value, "r") as f:
  6.         editor.value = f.read()
  7.  
  8. def save_file():
  9.     with open(file_name.value, "w") as f:
  10.         f.write(editor.value)
  11.         save_button.disable()
  12.  
  13. def enable_save():
  14.     save_button.enable()
  15.  
  16. # A new function that closes the app and warning appears
  17. def exit_app():
  18.     if app.yesno("Warning", "Are you quit without saving"):
  19.         app.destroy()
  20.     else:
  21.         app.display()
  22.  
  23. def change_font():
  24.     editor.font = font.value
  25.  
  26. #function to resize text
  27. def change_text_size():
  28.     editor.text_size = size.value
  29.     # resize the widget because if the text is made bigger, this might affect the size of the TextBox so guizero needs to know how to maintain the intended layout
  30.     editor.resize(1, 1)
  31.     editor.resize("fill", "fill")
  32.  
  33. # function to change font color and theme
  34. def change_color():
  35.     editor.text_color = color.value
  36.  
  37. def change_theme():
  38.     if theme.value == 1:
  39.         app.bg = "black"
  40.         app.text_color = "white"
  41.     else:
  42.         app.bg = None
  43.         app.text_color = None
  44.  
  45.  
  46. # function to make menubar preferences show
  47. def pref_fontis():
  48.     font_box.show()
  49.  
  50. def pref_sizes():
  51.     size_box.show()
  52.  
  53. def pref_colour():
  54.     color_box.show()
  55.  
  56. def pref_theme():
  57.     theme_box.show()
  58.    
  59. def hide_pref():
  60.     font_box.hide()
  61.     size_box.hide()
  62.     color_box.hide()
  63.     theme_box.hide()
  64.  
  65.  
  66. app = App(title="TextZero Editor")
  67.  
  68. # The new MenuBar
  69. menubar = MenuBar(app,
  70.                   toplevel=["File", "Prefs"],
  71.                   options=[
  72.                     [ ["Open",open_file],["Save",save_file],["Exit",exit_app] ],
  73.                     [ ["Font",pref_fontis],["Size",pref_sizes],["Colour",pref_colour],["Theme",pref_theme],["Close pref",hide_pref] ]
  74.                     ])
  75.  
  76. # Box control on the bottom with save and open file
  77. file_controls = Box(app, align="bottom", width="fill", border=True)
  78. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  79.  
  80. save_button = PushButton(file_controls, text="Save", command=save_file, align="right")
  81. open_button = PushButton(file_controls, text="Open", command=open_file, align="right")
  82.  
  83.  
  84. # Box contain preferences control
  85. preferences_control = Box(app, layout='auto', align="top", width="fill")
  86. font_box = Box(preferences_control, align="left", width="fill",border=True, visible=False)
  87. size_box= Box(preferences_control, align="left", width="fill",border=True, visible=False)
  88. color_box= Box(preferences_control, align="left", width="fill",border=True, visible=False)
  89. theme_box = Box(preferences_control, align="left", width="fill",border=True, visible=False)
  90.  
  91. # Editor of the text that multiline writing
  92. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  93.  
  94. # Widget that use on the editor
  95. label_font = Text(font_box,  align="left", text="Font: ")
  96. font = Combo(font_box, options=["courier", "times new roman", "verdana"], align="left", command=change_font)
  97.  
  98. label_size = Text(size_box,  align="left", text="Size: ")
  99. size = Combo(size_box,  options=["8", "10", "12","16"], align="left", command=change_text_size)
  100.  
  101. label_color = Text(color_box,  align="left", text="Colour: ")
  102. color = Combo(color_box, options=["red","green","black"], align="left", command=change_color)
  103.  
  104. label_theme = Text(theme_box, text='Dark mode', align="left")
  105. theme = CheckBox(theme_box, align="left", command=change_theme)
  106.  
  107. # instruction to close app with right window close (x)  
  108. app.when_closed = exit_app
  109.  
  110. # Display on the screen
  111. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement