Python Forum
Button with Image Icon
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button with Image Icon
#1
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)
        self.label.pack(side=RIGHT)

        self.root.mainloop()

    def updatebanane(self):
            self.label.config(text='Banane')
            
    def updatemelone(self):
            self.label.config(text='Melone')
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
            self.label.config(text='Banane')
        if event.widget = self.b2
            self.label.config(text='Melone')
But i get the following error
Error:
TypeError: update() missing 1 required positional argument: 'event'
Reply
#2
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
Button(self.root,image=self.banane,width="30",height="30",
       command=partial(self.update, "b1")).pack(side=LEFT)
Button(self.root,image=self.melone,width="30",height="30",
       command=partial(self.update, "b2")).pack(side=LEFT)

    def update(self, button_num):
        if button_num=="b1":
            self.label.config(text='Banane')
        elif button_num=="b2":
            self.label.config(text='Melone')  
Reply
#3
It works. Thanks a lot
but i never heard of that "partial"...i have to find out more about it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation [PyQt] Setting icon on QAction from outside QGuiApplication gradlon93 3 1,677 Jan-04-2023, 11:37 AM
Last Post: gradlon93
  PyQt6 QAction with icon and string malonn 2 1,617 Sep-12-2022, 11:59 AM
Last Post: malonn
  [Tkinter] Why does the image for the button not appear? finndude 4 2,047 Oct-21-2021, 06:41 PM
Last Post: deanhystad
  how to remove icon ilyess68ysl 4 3,581 Oct-15-2021, 10:05 AM
Last Post: ilyess68ysl
  [Tkinter] image inside button rwahdan 4 6,883 Jul-12-2021, 08:49 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,516 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 3,954 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  Icon in tkinter menator01 8 4,842 May-03-2020, 02:01 PM
Last Post: wuf
  Problem about image and button scotesse 5 2,877 Apr-27-2020, 10:09 AM
Last Post: scotesse
  [Tkinter] Password Reveal Icon Evil_Patrick 2 4,584 Nov-29-2019, 02:20 PM
Last Post: Evil_Patrick

Forum Jump:

User Panel Messages

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