Need help making a random image choser in tkinter Python - python

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.

Related

How to fix Tkinter dead frezzing or crashing because of huge data?

I am trying to make a program to display one single image (.png extension) at once but giving a button to the user to change the picture.
What I have done is:
Reading the Image from my directory with help of Pillow module
Appended it to a list
With a button I increase or decrease the index of the list.
(Note I have to read 600 images approx.)
Here's the code:
import os
from tkinter import *
from PIL import ImageTk,Image
import threading,time
#Define the tkinter instance
x=0
win= Tk()
dir_path= os.path.dirname(os.path.realpath(__file__))
print(dir_path)
l1=[]
#Define the size of the tkinter frame
win.geometry("700x400")
def start():
threading.Thread(target=bg).start()
win.after(5000,threading.Thread(target=fg).start())
#Define the function to start the thread
def bg():
print("bg")
for i in range(1,604):
a=Image.open(f"{dir_path}\\{i}.png")
a=a.resize((500,700), Image.ANTIALIAS)
b=ImageTk.PhotoImage(a)
l1.append(b)
print(b)
print(len(l1))
def fg():
def add():
global x
x+=1
img2=l1[x]
d.configure(image=img2)
d.image = img2
d.update()
global d
d=Label(win,image=l1[x])
d.pack()
Button(win,text="add",command=add).place(x=0,y=0)
label= Label(win)
label.pack(pady=20)
#Create button
b1= Button(win,text= "Start", command=start)
b1.pack(pady=20)
win.mainloop()
But the problem is that the Tkinter gets dead freeze and laggy to an extent that the GUI is not operable.
So my question is,
How to fix Tkinter dead Frezzes and if there is any way to read the images as fast as possible?
The freezes of Tkinter depends on reading speed of the interpreter, because:
continuously reading and Showing the pictures are a huge job for Python.
reading images using opencv or any other image processing module wont help as reading part can become faster, but showing the image in tkinter is done using python only, so rapid changes in Label will cause tkinter to crash.
Solution:
Switch to a different compiler based language for example c++.
this solution is specifically for a image slideshow,
The solution is to use selenium with python. You can specify the image location in driver.get(image_path) and it will open the image for you, and if you want to change the image with a button, then just rename all your images with the numbers and make a button to add +1 to the index.

Why can't my image load on my tkinter window?

I'm trying to add an image to my program but it's not working out, the code is correct as shown below, and the image i'm trying to open is in the same folder as the saved .py file.
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Balance 0-21")
root.configure(width=400, height=200)
root.iconbitmap("C:/Users/user/Desktop/Projects/Balance 0-21/LogoCon.ico")
MasterCard = ImageTk.PhotoImage(Image.open("ten.png"))
MasterCardIMG = Label(image=MasterCard)
MasterCardIMG.grid(row=2, column=3)
root.mainloop()
try adding this after the MasterCardIMG Label creation (also for convention make the first letter lowercase as these aren't classes), I was having the same issue getting my images to show, I believe it has something to do with the label instance creation not being able to assign the image.
MasterCardIMG.image = MasterCard
(recommendation for convention)
masterCard = ImageTk.PhotoImage(Image.open("ten.png"))
masterCardIMG = Label(image=masterCard)
masterCardIMG.image = masterCard
masterCardIMG.grid(row=2, column=3)

I need a little help wth using tkinter and pillow(Python)

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)

How to hide / show a GIF image in Tkinter

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()

Python Insert Image to Tkinter Text Widget

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

Categories

Resources