I'm trying to simply add an image to a tkinter button. I tried everything:
import tkinter as tk
root = tk.Tk()
root.geometry('300x300+300+150')
photo = tk.PhotoImage('home.gif')
btn = tk.Button(root, image = photo, width = 100, height = 100)
btn.image = photo # even with this does not work
btn.pack()
root.mainloop()
I also tried with PIL setting the photo variable equal to ImageTk.PhotoImage(Image.open('home.gif')), I tried easly the open function, the absolute path of the photo (and yes, the photo is inside the same directory of my script), but anything works. The window just pop up with a big button, without image inside.
UPDATE:
I tried with other images, and I noticed that some images are shown while others no. This is because the images with transparent background cause a bug or a problem to tkinter... so, I do not know if there's a way to solve this. On google I find out that some people use canvas but I actually need the image to be inside the button so I do not know how to do.
Please change your code as below
photo = tk.PhotoImage(file='home.gif')
because i changed the above code and it worked....
Related
I know this question has been asked quite often, but I still can't figure out what's the problem:
I'm trying to make a simple Tkinter window with 4 Buttons. Each one should have a background Image. When I try to set image=path+'image.png' I get this: _tkinter.TclError: image "C:/Users/.../image.png" doesn't exist. The strange thing is that when I copy the path from the message and paste it into the Explorer adress line it opens the image i want to have as background image.
Here's the necessary code:
import tkinter
global path
path = 'C:/Users/Michael Hofmann/.../kahoot'
root = tkinter.Tk()
Button_red = tkinter.Button(root, image=path+'/images/red_small.png', command= pressed('red'))
Thanks in advance!
The error is telling you an image object doesn't exist by that name, not that a file doesn't exist by that name. The image option requires an object of type tkinter.PhotoImage. You can't just give it a path to an image.
image = tkinter.PhotoImage(file=path+'/images/red_small.png')
Button_red = tkitner.Button(..., image=image)
I'm currently working as an undergrad intern engineer and am looking to streamline the file sorting process for those at my workplace. I am attempting to make a python program that displays an image and has buttons below than when pressed move that photo into a specific folder.
The biggest difficulty so far is that I have no experience with python but that is what the company uses so im locked into it. I am able to create a messy program that displays a window with a photo and am yet to add buttons but that should be alright. My current concern is that it opens a new window every time it sorts a photo, as if the window is the photo rather than the photo being a part of the window. I will post my messy code below but any help would be appreciated. Please keep in mind im a complete python beginner, my only experience similar to this is in C#.
import os
import shutil
import tkinter as tk
import PIL
from PIL import ImageTk, Image
source = 'C:\\Source\\'
for file in os.listdir(source):
root = tk.Tk()
root.geometry("1920x1080")
photo = Image.open(source+file).resize((750,500), Image.ANTIALIAS)
img = ImageTk.PhotoImage(photo)
panel = tk.Label(root, image = img)
panel.image = img
panel.pack(side = "top", fill = "both")
print('Enter Destination Directory')
dest = input()
shutil.move(source+file, dest)
root.destroy()
I think it may be opening new windows because you are destroying root.
You need to define and destroy your root variable outside of your main for loop. Each time you loop, you create a new tk.Tk object and then, destroy it.
I'm working on a program using tkinter for python. It is an MIDI file editor. I inserted some gif files into it, but I need the code for them to turn transparent when touching the mousepointer.
I've already asked a similar question, and I got a very helpful result, but it isn't exactly working as expected.
def on_mouse_enter(event):
print("enter...", event.widget)
def on_mouse_leave(event):
print("leave...", event.widget)
root = tk.Tk()
for i in range(10):
label = tk.Label(root, text="Item #{}".format(i), name='label-{}'.format(i))
label.pack()
label.bind("<Enter>", on_mouse_enter)
label.bind("<Leave>", on_mouse_leave)
tk.mainloop()
I tried replacing tk.label with tk.PhotoImage but it resulted into an attribute error.
I'm an entry level python coder looking to create a Guess Who styled game. At university, I've yet to learn how to import images and bind them to fixed places on a screen (resembling the game board). Is there any way to click on a specific image and have an onClickEvent to occur, where that specific character(image) is chosen.
The majority of my coding abilities are in python, but I am skeptical if this is the best possible language to do a project like this in.
Every GUI has Button widget which is clickable and (mostly) can display image.
But mostly in GUI you can assign click event to every object ie. Label with Image.
ie. Tkinter
import tkinter as tk
from PIL import Image, ImageTk
# --- functions ---
def on_click(event=None):
# `command=` calls function without argument
# `bind` calls function with one argument
print("image clicked")
# --- main ---
# init
root = tk.Tk()
# load image
image = Image.open("image.png")
photo = ImageTk.PhotoImage(image)
# label with image
l = tk.Label(root, image=photo)
l.pack()
# bind click event to image
l.bind('<Button-1>', on_click)
# button with image binded to the same function
b = tk.Button(root, image=photo, command=on_click)
b.pack()
# button with text closing window
b = tk.Button(root, text="Close", command=root.destroy)
b.pack()
# "start the engine"
root.mainloop()
Graphic modules like PyGame can display image too, and have click event but sometimes you have to check manually if you clicked in area with image (and you have to create mainloop manually)
I'd say TkInter is your best bet. A little bit cumbersome at first but it's good for beginners. You should be able to make a nice Graphical User Interface with it which will open a window that holds your pictures, menus, buttons, etc...
Take a look at useful docs and examples here.
If Python is not a requirement, I too would also recommend JS, HTML and CSS (you'll have to use all three together. Sounds scarier than it is :P)
I'm trying to make a script which will enable me to dynamically update an image object and then post the updated image to a Tkinter Canvas widget. The code here is prototype code, just to get the basics down. The aim here is to put a blue pixel on the image being displayed by the canvas, at the click location.
Something very strange is going on here. I'm using the Wing IDE, and if I run this code through the debugger, with a breakpoint at any line in the woohoo function, and then continue execution after hitting the breakpoint, the code works exactly as expected- putting a blue pixel on the image. If I run the code normally, or through the debugger with no breakpoints, the image is never updated. This leads me to the conclusion that there is some internal wizardry going on which I haven't got much hope of understanding without aid.
I'd really like to know the best way to go about this (or any way, I guess), and if someone could explain to me what's going on under the hood that'd be really cool. Thanks.
from Tkinter import *
from PIL import Image, ImageTk
def woohoo(event):
original.putpixel((event.x,event.y),(0,0,255))
newpic = ImageTk.PhotoImage(original)
c.create_image((0,0),image=newpic, anchor="nw")
main = Tk()
c = Canvas(main, width=300, height=300)
main.geometry("300x300+0+0")
c.pack()
original = Image.open("asc.bmp")
picture = ImageTk.PhotoImage(original)
c.create_image((0,0),image=picture, anchor="nw")
c.bind("<Button-1>", woohoo)
main.mainloop()
My guess is, you're creating a new image in a function. The reference to the image is a local variable. When the function exits, the reference is garbage collected which causes the new image to be destroyed. Most likely, running interactively causes the garbage collector to run differently (perhaps more lazily?)
Changed a little of the other post to work with Python 3+ :
from tkinter import *
def stuff(event):
global picture3
picture3 = PhotoImage(file='picture2.png')
c.itemconfigure(picture2, image = picture3)
main = Tk()
c = Canvas(main, width=300, height=300)
c.pack()
picture = PhotoImage(file='picture1.png')
picture2 = c.create_image(150,150,image=picture)
c.bind("<Button-1>", stuff)
main.mainloop()
try it like this:
from Tkinter import *
from PIL import Image, ImageTk
def woohoo(event):
global picture #
original.putpixel((event.x,event.y),(0,0,255))
picture = ImageTk.PhotoImage(original)#
c.itemconfigure(myimg, image=picture)#
main = Tk()
c = Canvas(main, width=300, height=300)
main.geometry("300x300+0+0")
c.pack()
original = Image.open("asc.bmp")
picture = ImageTk.PhotoImage(original)
myimg = c.create_image((0,0),image=picture, anchor="nw")#
c.bind("<Button-1>", woohoo)
main.mainloop()