Home > Software design >  Space between Menus in Tkinter Menu
Space between Menus in Tkinter Menu

Time:11-11

I have the below tkinter menubar. It all works great, accept the menus within it are all bunched up close together. Can anyone please tell me if there's a way to add left and right padding between the menus. I don't mean the separator in the menu items, but padding between the top menus in the menubar, such as between "File", "Edit", "About".

Specifically: enter image description here

my_menubar = tk.Menu(root, background='#ffffff', foreground='#000000',relief=FLAT,bd=0)  # create menu widget



#-----------------Create dropdown menu "file" in menubar-----------------------------
#ddmenu_file = tk.Menu(my_menubar, tearoff=0, background='#111111', foreground='#ffffff', relief=FLAT, activeborderwidth=0, activeforeground='black',activebackground='#ADD8E6')    # Create dropdown menu (file menu) and assocaite it with menubar. trearoff=0 removes dashed line first item (prevents menu tearoff)
ddmenu_file = tk.Menu(my_menubar, tearoff=0)    # Create dropdown menu (file menu) and assocaite it with menubar. trearoff=0 removes dashed line first item (prevents menu tearoff)
ddmenu_file.add_command(label="New File", command=ddmenu_file_new_file)
ddmenu_file.add_separator()
ddmenu_file.add_command(label="Exit", command=ddmenu_file_exit) # create sub menu item
my_menubar.add_cascade(label="File", menu=ddmenu_file, font=("",menu_text_size, 'bold'))    #Attach the dropdown menu (File menu) to the menubar


-----------------Create dropdown menu "edit" in menubar---------------------------
#ddmenu_edit = tk.Menu(my_menubar, tearoff=0, background='#111111', foreground='#ffffff', relief=FLAT, activeborderwidth=0, activeforeground='black',activebackground='#ADD8E6')     # Create dropdown menu (file menu) and assocaite it with menubar. trearoff=0 removes dashed line first item (prevents menu tearoff)
ddmenu_edit = tk.Menu(my_menubar, tearoff=0)     # Create dropdown menu (file menu) and assocaite it with menubar. trearoff=0 removes dashed line first item (prevents menu tearoff)
ddmenu_edit.add_command(label="Cut", command=ddmenu_edit_cut)   # create sub menu item
ddmenu_edit.add_command(label="Copy", command=ddmenu_edit_copy) # create sub menu item
my_menubar.add_cascade(label="Edit", menu=ddmenu_edit, font=("",menu_text_size, 'bold'))    #Attach the dd menu (Edit menu) to the menubar


#---------------Create dropdown menu "options" in menubar-------------------------------
#ddmenu_option = tk.Menu(my_menubar, tearoff=0, background='#111111', foreground='#ffffff', relief=FLAT, activeborderwidth=0, activeforeground='black',activebackground='#ADD8E6')  # Create dropdown menu (file menu) and assocaite it with menubar. trearoff=0 removes dashed line first item (prevents menu tearoff)
ddmenu_option = tk.Menu(my_menubar, tearoff=0)  # Create dropdown menu (file menu) and assocaite it with menubar. trearoff=0 removes dashed line first item (prevents menu tearoff)
ddmenu_option.add_command(label="Find", command=ddmenu_option_find) # create sub menu item
ddmenu_option.add_command(label="Find Next", command=ddmenu_option_findnext)    # create sub menu item
my_menubar.add_cascade(label="Options", menu=ddmenu_option, font=("",menu_text_size, 'bold'))   #Attach the dropdown menu (Option menu) to the menubar



CodePudding user response:

Generally speaking, no, you don't have control over this. There are no options to modify the space between items on a menubar. On Windows and OSX menus are handled by the OS and tkinter has very little control over them. For unix-based systems, it might be possible to dig into the tk source code and see how the menubar is created, and then set some option database values that might be able to change it.

If all you want is some separation, you can try adding spaces before and/or after the menu names, or use the compound and image options to add an invisible image with each menu item. However, from a usability point of view you should consider just letting them stay however the OS renders them.

  • Related