Nakshatra Aich
Nakshatra Aich

Reputation: 43

Images not getting applied on the button in Tkinter

The following project is supposed to show a message when clicking a certain colored button. But, whenever I execute the program it shows blank(white) buttons in the correct alignment, etc. Due to some reason the images are not loaded. In future, I plan to add different images hence testing with colored image created in Paint and not in-built commands to show the color. I will add the result below after the code.

Edit: All images are 100x100 pixels created in Microsoft Paint.I have tried other modules like PIL but to no avail.

# importing the module
import tkinter
import tkinter.messagebox
from tkinter import *
# importing the module

# initialising tkinter
class window(Frame):

    def __init__(self,master = None):
        Frame.__init__(self,master)
        self.master = master
# initialising tkinter

# creating the window
root = Tk()
app = window(root)
root.geometry("350x350")
# creating the window

# colours
WHITE = (255,255,255)
BLACK = (0,0,0)
BLUE  = (0,0,255)
RED   = (255,0,0)
# colours

# image


red_image = "red.png"
blue_image = "blue.png"
yellow_image = "yellow.png"
green_image = "green.png"

# image

# creating a button function
def create_button(x,y,color,color2,picture):
    click = Button(root, image = PhotoImage(picture), width= 150, height=150, command = lambda : tkinter.messagebox.showinfo( "Hello Python", "This is " + color))
    click.image = PhotoImage(picture)
    click.grid( row = x, column = y)
# creating a button function

create_button(0,0,'red','pink',red_image)
create_button(0,2,'blue','lightblue',blue_image)
create_button(2,0,'green','lightgreen',green_image)
create_button(2,2,'yellow','lightyellow',yellow_image)

# starting the widget
root.mainloop()
# starting the widget

Directory

Result

Upvotes: 0

Views: 113

Answers (2)

Ujjwal Dash
Ujjwal Dash

Reputation: 823

For adding image in Button you have not use appropriate keywords.

Here is a simple example for you to add image in button

from tkinter import * 
from tkinter.ttk import *
# creating tkinter window 
root = Tk() 
# Adding widgets to the root window 
Label(root, text = 'Image adding', font =( 'Verdana',15)).pack(side = TOP, pady = 10) 

  # Creating a photoimage object to use image 

photo = PhotoImage(file = "C:\Gfg\circle.png") 
# here, image option is used to 
# set image on button 

Button(root, text = 'Click Me !', image = photo).pack(side = TOP) 
root.mainloop() 

I think it may help you

Upvotes: 1

acw1668
acw1668

Reputation: 46678

There are two issues in your code:

  • You passed filename to PhotoImage() without using file keyword: PhotoImage(picture) should be PhotoImage(file=picture)

  • You did not keep the reference of the image assigned to button, but another instance of image

Below is the updated create_button() function that fixes the issues:

def create_button(x, y, color, color2, picture):
    image = PhotoImage(file=picture)
    click = Button(root, image=image, width=150, height=150, command=lambda: tkinter.messagebox.showinfo("Hello Python", "This is "+color))
    click.image = image
    click.grid(row=x, column=y)

Upvotes: 2

Related Questions