Home > Software design >  I'm trying to make an image a button, using python tkinter. However, the image button only show
I'm trying to make an image a button, using python tkinter. However, the image button only show

Time:10-01

I have created an image as a button. Upon opening a pop put window, I want the image button to be in a specific place (I've used the grid method). However, the button only appears when I include a character (any character) in the line below the button code. When the character is included, the image appears and the function called by the pressing of it works perfectly. I do get an error message when the pop out window initially appears though.

Here is my code:

def eastereggtxt1():
    function works fine so don't worry about this 
    function

img = PhotoImage(file = r'pathtoimage.png')

imgbutn = Button(EntryWindow,
image = img, borderwidth = 0,
command = eastereggtxt1)
imgbutn.grid(row = 18, column = 7)

r

The image/button only shows up if I run it with the above character "r" included. However, the character can be any letter really. without it, the image doesn't show up.

Any idea why this is happening?

CodePudding user response:

Assuming that your code is this:

from tkinter import *
from tkinter.ttk import *

root = Tk()

def eastereggtxt1():
    print("Easter Eggs!")

image = PhotoImage(file = r'easter_eggs.png') # I use easter_eggs as an example

image_button = Button(root, image = image, command = eastereggtxt1)
image_button.grid(row = 18, column = 7)
mainloop() # I just know that you can just do mainloop(), but it's not effective

I tryed the code, it works fine, but when I try to replace r with something else, (Example: l,p,s), an error occured:

PS C:\Users\user#1> python -u "D:\Test\test1.py"
  File "D:\Test\test1.py", line 9
    img = PhotoImage(file = s'easter_eggs')
                             ^
SyntaxError: invalid syntax

It refers that the code can't use any other character than r.

Also you mentioned:

The image/button only shows up if I run it with the above character "r" included.

I think your OS doesn't support without the character r? If this is wrong, please tell me in the comments.

Peace :D

CodePudding user response:

You can use a label instead of a button. That could be a good option. But you'll need to change a few lines of code.

In the eastereggtext1() function, pass an argument event. Example:

def eastereggtext(event):
    # function code here...

Then, remove the button and add an image.

imgbutn = Label(root, image=img)
imgbutn.grid(row = 18, column = 7)

Then, bind the label to mouse click.

imgbutn.bind("<Button-1>", eastereggtext1)

For more info about tkinter bindings: Python | Binding function in Tkinter

So here's the full code:

from tkinter import Tk, Button, PhotoImage
# other modules you need to import

root = Tk()
root.title("Title of the window")

def eastereggtext1(event):
    # function code here....

img = PhotoImage(file = 'image_path.png')

imgbutn = Label(root, image=img)
imgbutn.grid(row = 18, column = 7)
imgbutn.bind("<Button-1>", eastereggtext1)

root.mainloop()
  • Related