I am new to coding so problem might be very simple but I just dont get it.
So I am trying to upload a gif image to tkinter label. Here is my code:
from PIL import *
import PIL.Image
import tkinter as tk
from tkinter import *
root = Tk()
img = PIL.Image.open ("10.gif")
tkimage = PhotoImage(img)
label = Label(root, image=tkimage)
label.pack()
root.mainloop()
And here is the error that I am getting:
__str__ returned non-string (type GifImageFile)
I dont seem to find answer from google for this particular problem(I have tried)
Related
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 3 months ago.
I created a button with an image in Tkinter, but the image isn't showing, i can just see a blank space in the button. Maybe my OS is the problem. I have Linux Mint 21 Xfce Edition.
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import dialog
from tkinter import *
from PIL import ImageTk, Image
from modules import fonts, buttons
import shutil
import os
virusscan_win = Tk()
virusscan_win.title("Virus scan")
virusscan_win.geometry('400x200+60+60')
virusscan_win.configure(background="white")
Message(text="The virus scanner gives them the ability to scan files, folders or their system for viruses and remove threats automatically.", width=250, font=fonts.sansserif_small, background = "white").place(x=10, y=10)
safe_image=PhotoImage(file='./media/button.png')
Button(master=virusscan_win, image=safe_image, text="Scan file").place(x=10, y=90)
Can anyone explain, what was wrong with my code? I already checked, is the file in the correct folder. the debugger also shows nothing.
Python is notorious for aggressively garbage collecting image references, which leads to them being destroyed before the UI gets drawn. Something like this should work for getting an image onto a Button
from PIL import Image, ImageTk
from pathlib import Path
img_path = Path('C:/<path>/<to>/media/button.png') # use the full path to your img file
with Image.open(img_path) as img: # open the image file
img_tk = ImageTk.PhotoImage(img) # get the image as a Tk object
button = Button(virusscan_win, image=img_tk) # add image to button
Note that if your image is the wrong size for your button, you can resize it like so
img_tk = ImageTk.PhotoImage(img.resize(32, 32)) # or whatever size you need
hello so im trying to make a random image chooser , my images are in the same folder as the script and what im trying to do is get a number from 0-6 then chose that image, all the images are titled 0.png, 1.png,2.png etc, btw im fairly new to python
this is the code
from tkinter import *
import random
from PIL import ImageTk,Image
root = Tk()
root.geometry("400x400")
gen1=random.randint(0,6)
my_img = ImageTk.PhotoImage(Image.open(gen1".png"))
my_label = Label(image=my_img)
my_label.pack()
root.mainloop()
gen1 is generating a random number and then a bit lower putting .png to it
it is simply not working can somebody explain?
open(gen1".png")
This won't work. Perhaps try open(str(gen1) + ".png")) to stringify your random integer and concatenate it with the extension.
In using Python's Tkinter application, I have come across a little problem. How do I hide and show GIF images that I have put into the window using PhotoImage?
I can make the images appear in the first place, but am unable to do anything with them. I have tried using canvas.itemconfig and canvas.update, but to no avail. Is anyone able to solve this problem?
picture = PhotoImage(file='C:\\Users\\ZecFamily5\\Downloads\\Island.gif')
c.create_image(250, 250, image=picture)
The above code cannot be hidden or shown using c.itemconfig(image, state=HIDDEN).
You should do .itemconfig(Your Item Name, state=HIDDEN) and to show, change HIDDEN to NORMAL to show.
I would recommend using the ImageTK module:
img = ImageTk.PhotoImage(Image.open(path))
As it should allow you to make a TKinter compatible photo image.
For this to work though, do keep in mind that this will need to be at the beginning of your file:
import Tkinter as tk
from PIL import ImageTk, Image
Here is an example piece of code to show you what I mean:
import Tkinter as tk
from PIL import ImageTk, Image
path = 'C:/xxxx/xxxx.jpg'
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
im trying to create an arc with tkinter canvas widget, but whatever I do, I cant get the create_arc function to work. I tried assigning values to normal python variables in the main function body, but that doesnt work either. Here is my full code.
from tkinter import *
from tkinter import ttk
root = Tk()
emasektor = StringVar()
def arvutapalgad(*args):
try:
sissetulek.set(x+y+z)
emasektor.set(float((x/(x+y+z))*360))
isasektor = 90
lastesektor = (z/(x+y+z))*360
except ValueError:
**raam.create_arc(20, 20, 180, 180,start = 0,extent=emasektor.get(),fill='red')**
Here is a minimal example that works. Build up from this until a bit at a time until it does not work. That should tell you what is causing the problem.
import tkinter as tk
root = tk.Tk()
can = tk.Canvas(root)
can.pack()
arc = can.create_arc(100,100,200,300, fill='red')
root.mainloop()
Or start with your code and cut or comment out until it does not 'work'. Also, when posting a question, be more specific than 'does not work'. If there is a traceback, post it.
Im creating a simple chat box in Python and I want to insert an image(emoticons) to a TKinter text widget. I have tried it using this code:
img = Image.open("icon.jpg")
self.bigText.insert(END, img) # bigText is the text widget
Output of the code above is
<PIL.JpegImagePlugin.JpegImageFile instance at 0x01AB5A30>
instead of the image.
I'm not 100% sure on this, but I think you need to use image_create. Something like:
self.bigText.image_create(END, image=img)
should do the trick.
I have made it using:
from Tkinter import *
from PIL import Image, ImageTk
self.myEmoticons.append(self.smiley)
self.bigText.image_create(END,image = self.myEmoticons[self.myEmoticonsCtr])
self.myEmoticonsCtr=self.myEmoticonsCtr + 1