0

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

  • 1
    Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – salilnaik Aug 01 '20 at 09:49
  • Unfortunately it does not. I read that post before posting this question and as you can see i did add a reference for the image. Inside the loop I'm a beginner in tkinter so maybe i might have interpreted it as incorrect. – Nakshatra Aich Aug 01 '20 at 11:04
  • No you did not keep the image reference assigned to the button, you just keep a reference of another instance of image. – acw1668 Aug 01 '20 at 11:18
  • Can you explain this point more elaborately? These terms are quite alien to me. Even though one of the answer given has clarified my point, I would like to get a better idea. – Nakshatra Aich Aug 02 '20 at 07:05

2 Answers2

2

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)
acw1668
  • 40,144
  • 5
  • 22
  • 34
1

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

Ujjwal Dash
  • 767
  • 1
  • 4
  • 8