Here we are providing basic tutorial in Tkinter on how to create a toolbar in a Tkinter GUI application.

Sample code

The following Tkinter code will demonstrate the same:

from tkinter import *

def doNothing():
    print("Ok Ok I wont")

root = Tk()

# Main Menu

menu = Menu(root)
root.config(menu=menu)

subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)

editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing)

# Toolbar

toolbar = Frame(root, bg="blue")

insertButt = Button(toolbar, text="Insert Image", command=doNothing)
insertButt.pack(side=LEFT, padx=2, pady=2)
printButt = Button(toolbar, text="Print", command=doNothing)
printButt.pack(side=LEFT, padx=2, pady=2)

toolbar.pack(side=TOP, fill=X)

root.mainloop()


In the above code, the code for the Main menu had been explained already in the topic of Creating a dropdown menu. In the next few lines of code, we created a toolbar called toolbar using the pre-defined method in Tkinter known as Frame which takes root window as a parameter. Then we created two buttons in the created toolbar called as insertButt and printButt using the pre-defined method in Tkinter known as Button which takes toolbar as a parameter. In the GUI application, we had created two buttons called as Insert image and Print as shown below. These buttons at present call the method doNothing as the command parameter of both the buttons take doNothing as value of the parameter. At last, both buttons and the toolbar are packed using the function pack() at required positions.

Output

output