1
votes

I'm trying to create a tkinter button class to add a resized image with text.

The button renders ok on the mainwindow as expected, but I cannot pass a command to the button. The additional **kwargs to the BTN_IMG_TXT create errors with the original tk.button. Hence I used following in class definition, which I believe the problematic part of the code.

def __init__(self, master, command, *args, **kwargs):
    tk.Button.__init__(self,master=master, command=command)

Following is the full code for testing:

import tkinter as tk
from PIL import  Image, ImageTk

window = tk.Tk()
window.geometry("300x200")

def remove_item(event = None):
    print('Delete button pressed')  #debug

# custom button widget
class BTN_IMG_TXT(tk.Button):
    def __init__(self, master, command, *args, **kwargs):
        tk.Button.__init__(self,master=master, command=command)
        self.fname = kwargs.get('fname')
        self.btn_txt = kwargs.get('btn_txt')
        img = Image.open(self.fname)
        image = img.resize((40,40), Image.ANTIALIAS)  # make static size
        self.photo1=  ImageTk.PhotoImage(image)
        b = tk.Button(self, text=self.btn_txt, image=self.photo1, 
                            compound="top", width=70, height=70,)
        b.pack(side="top")

frm_Main = tk.Frame(master=window, bg='#faeaea', borderwidth=2)
frm_Main.pack()
stp_btn = BTN_IMG_TXT(master=frm_Main,fname="stop_b.png",btn_txt='STOP',command=remove_item)
del_btn= BTN_IMG_TXT(master=frm_Main,fname="delete_b.png",btn_txt='DELETE', command=remove_item)
del_btn.pack(side="left", fill="both", expand=False)
stp_btn.pack(side="left", fill="both", expand=False)


window.mainloop()
1

1 Answers

2
votes

Tkinter Button can display text as well as image together and by using compound parameter we can tell the button widget where to align the text with the image read more about the compound parameter in the config(**options) of Tkinter Button.

You are using a button inside a button that's why the command is not working because it is linking with the BTN_IMG_TXT class, not with the b which is inside of the class.

Here is what I did,

# custom button widget
class BTN_IMG_TXT(tk.Button):
    def __init__(self, master=None, cnf={}, **kw):
        # Merged both cnf and kw dictionaries.
        kw = tk._cnfmerge((kw, cnf)) 
        # make static size
        if kw.get('image'):
            img = Image.open(kw['image'])
            image = img.resize((40,40), Image.ANTIALIAS)  
            self.photo1 = ImageTk.PhotoImage(image)
            kw['image'] = self.photo1
        # Aligns the image on top and text under it
        kw['compound'] = kw.get('compound', 'top') 
        super().__init__(master=master, **kw)


frm_Main = tk.Frame(master=window, bg='#faeaea', borderwidth=2)
frm_Main.pack()
stp_btn = BTN_IMG_TXT(master=frm_Main, image="stop_b.png", text='STOP', command=remove_item)
del_btn= BTN_IMG_TXT(master=frm_Main, image="delete_b.png", text='DELETE', command=remove_item)
del_btn.pack(side="left", fill="both", expand=False)
stp_btn.pack(side="left", fill="both", expand=False)

I Hope, this solves your issue.