Python Forum
Button with Image Icon
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button with Image Icon
Hi again,

i am trying a simple gui, 2 buttons with an image as icon, in this exaple (banane, melone)...
if i press one of the button, the text will be displayed..
it is almost working, however the image icons are tooo big (zoomed out)...what should i do, so that the images fit into the buttons..

class Fruit:
    def __init__(self):
        self.root = Tk()
        self.banane = PhotoImage(file="banane.jpg")
        self.melone = PhotoImage(file="melone.jpg")
        self.b1 = Button(self.root,image=self.banane,width="30",height="30", command=self.updatebanane).pack(side=LEFT)
        self.b2 = Button(self.root,image=self.melone,width="30",height="30", command=self.updatemelone).pack(side=LEFT)
        self.label = Label(self.root, font=('Arial', 14), width=20)


    def updatebanane(self):
    def updatemelone(self):
f = Fruit()
2. Question... is there a way to use events so that i need only one method to update the text instead of 2 ?
i tried the following, it didn't work (i just learned about events yesterday)

self.b1 = Button(self.root,image=self.banane,width="30",height="30", command=self.update).pack(side=LEFT)
self.b2 = Button(self.root,image=self.melone,width="30",height="30", command=self.update).pack(side=LEFT)
    def update(self, event):
        if event.widget = self.b1
        if event.widget = self.b2
But i get the following error
TypeError: update() missing 1 required positional argument: 'event'
You have not sent any events to the update function. Generally it is done like this, i.e. sending some identifier to the function
from functools import partial
## rest of the code

## pack returns None so self.b1 and self.b2 are None
       command=partial(self.update, "b1")).pack(side=LEFT)
       command=partial(self.update, "b2")).pack(side=LEFT)

    def update(self, button_num):
        if button_num=="b1":
        elif button_num=="b2":
It works. Thanks a lot
but i never heard of that "partial"...i have to find out more about it

Possibly Related Threads…
Thread Author Replies Views Last Post
  Icon in tkinter menator01 8 667 May-03-2020, 02:01 PM
Last Post: wuf
  Problem about image and button scotesse 5 614 Apr-27-2020, 10:09 AM
Last Post: scotesse
  [Tkinter] Password Reveal Icon Evil_Patrick 2 804 Nov-29-2019, 02:20 PM
Last Post: Evil_Patrick
  [Tkinter] Window Icon Evil_Patrick 6 2,835 Oct-18-2019, 11:26 AM
Last Post: Evil_Patrick
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 1,294 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Simple Button click on image file to create action? jpezz 4 1,509 Mar-27-2019, 10:08 PM
Last Post: jpezz
  [PyQt] How is this tray icon throbber done? JackDinn 7 1,792 Mar-05-2018, 02:19 PM
Last Post: JackDinn
  set button background with image of window background gray 3 10,342 Oct-18-2017, 08:22 AM
Last Post: gray
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 8,416 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020