Search code examples
tkintericonstoolbar

Toolbar with icons instead of text


I just want to put icons instead of "Measure Distance", "Calculate Angles", .... How can I do it?

This is part of the code

    menubar = Menu(win)
    menubar.add_command(label = "Measure Distance", command = MeasureDistance)
    menubar.add_command(label = "Calculate Angles", command = CalculateAngles)
    menubar.add_command(label = "Create a Circle", command = CreateCircle)
    menubar.add_command(label = "Undo", command = Undo)
    menubar.add_command(label = "Save and Close", command = save_close)
    menubar.add_command(label = "Exit", command = win.destroy)  
    win.config(menu=menubar)

Solution

  • If you visit this page and scroll down a bit you may find what you have been searching for. What you want will look something like this:

    menubar = Menu(self.master)
            self.fileMenu = Menu(self.master, tearoff=0)
            self.fileMenu.add_command(label="Exit", command=self.onExit)
            menubar.add_cascade(label="File", menu=self.fileMenu)
    
            toolbar = Frame(self.master, bd=1, relief=RAISED)
    
            self.img = Image.open("exit.png")
            eimg = ImageTk.PhotoImage(self.img)
    
            exitButton = Button(toolbar, image=eimg, relief=FLAT,
                command=self.quit)
            exitButton.image = eimg
            exitButton.pack(side=LEFT, padx=2, pady=2)
    
            toolbar.pack(side=TOP, fill=X)
            self.master.config(menu=menubar)
            self.pack()
    

    You just have to change some variable names and that should be it. It will look like this: Result. Image by zetcode.com