Arion Sintes Sintes :
I have this code, and the problem is I can't open the images. I did a random election to open a random image. Thanks!
Code:
import tkinter as tk
from tkinter import *
import random
from PIL import Image
window=Tk()
window.geometry('500x550')
window.resizable(False, False)
f=tk.Frame()
f.config(bg='blue', height='500', width='500')
f.pack()
cat=tk.Button(window, text='Cat')
cat.config()
cat.pack(fill=X)
dog=tk.Button(window, text='Dog')
dog.config()
dog.pack(fill=X)
images=['cat1.jpg', 'cat2.jpg', 'cat3.jpg', 'cube1.jpg', 'cube2.jpg', 'cube3.jpg']
imgimport = open(random.sample(images, 1))
img = PIL.Image.open(imgimport)
img.show()
Just learned it :
random.sample()
returns a particular length list of items chosen from the sequence.
So even random.sample(list, length=1)
returns a list, not the item.
You can use random.sample()[0]
as @Axe also said.
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=408197&siteId=1