Advertisement
Mori007

mymenubar

Feb 11th, 2021
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. rom 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
  17. def exit_app():
  18.     app.destroy()
  19.  
  20. def change_font():
  21.     editor.font = font.value
  22.  
  23. #function to resize text
  24. def change_text_size():
  25.     editor.text_size = size.value
  26.     # 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
  27.     editor.resize(1, 1)
  28.     editor.resize("fill", "fill")
  29.  
  30. # function to change font color and theme
  31. def change_color():
  32.     editor.text_color = color.value
  33.  
  34. def change_theme():
  35.     if theme.value == 1:
  36.         app.bg = "black"
  37.         app.text_color = "white"
  38.     else:
  39.         app.bg = None
  40.         app.text_color = None
  41.  
  42.  
  43. # function to make menubar preferences show
  44. def pref_fontis():
  45.     font_box.show()
  46.  
  47. def pref_sizes():
  48.     size_box.show()
  49.  
  50. def pref_colour():
  51.     color_box.show()
  52.  
  53. def pref_theme():
  54.     theme_box.show()
  55.    
  56. def hide_pref():
  57.     font_box.hide()
  58.     size_box.hide()
  59.     color_box.hide()
  60.     theme_box.hide()
  61.  
  62.  
  63. app = App(title="TextZero Editor")
  64.  
  65. # The new MenuBar
  66. menubar = MenuBar(app,
  67.                   toplevel=["File", "Prefs"],
  68.                   options=[
  69.                     [ ["Open",open_file],["Save",save_file],["Exit",exit_app] ],
  70.                     [ ["Font",pref_fontis],["Size",pref_sizes],["Colour",pref_colour],["Theme",pref_theme],["Close pref",hide_pref] ]
  71.                     ])
  72.  
  73. # Box control on the bottom with save and open file
  74. file_controls = Box(app, align="bottom", width="fill", border=True)
  75. file_name = TextBox(file_controls, text="text_file.txt", width=50, align="left")
  76.  
  77. save_button = PushButton(file_controls, text="Save", command=save_file, align="right")
  78. open_button = PushButton(file_controls, text="Open", command=open_file, align="right")
  79.  
  80.  
  81. # Box contain preferences control
  82. preferences_control = Box(app, layout='auto', align="top", width="fill")
  83. font_box = Box(preferences_control, align="left", width="fill",border=True, visible=False)
  84. size_box= Box(preferences_control, align="left", width="fill",border=True, visible=False)
  85. color_box= Box(preferences_control, align="left", width="fill",border=True, visible=False)
  86. theme_box = Box(preferences_control, align="left", width="fill",border=True, visible=False)
  87.  
  88. # Editor of the text that multiline writing
  89. editor = TextBox(app, multiline=True, height="fill", width="fill", command=enable_save)
  90.  
  91. # Widget that use on the editor
  92. label_font = Text(font_box,  align="left", text="Font: ")
  93. font = Combo(font_box, options=["courier", "times new roman", "verdana"], align="left", command=change_font)
  94.  
  95. label_size = Text(size_box,  align="left", text="Size: ")
  96. size = Combo(size_box,  options=["8", "10", "12","16"], align="left", command=change_text_size)
  97.  
  98. label_color = Text(color_box,  align="left", text="Colour: ")
  99. color = Combo(color_box, options=["red","green","black"], align="left", command=change_color)
  100.  
  101. label_theme = Text(theme_box, text='Dark mode', align="left")
  102. theme = CheckBox(theme_box, align="left", command=change_theme)
  103.  
  104. # Display on the screen
  105. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement