【问题标题】:How to make image as a variable in python?如何在python中将图像作为变量?
【发布时间】:2021-05-12 04:03:21
【问题描述】:

是否可以将图像作为变量并将其用作参数?如果我运行代码,单击my_button 时不会收到任何错误消息。图像每次都会更改或更新,但我画布上的文本不会更新。

from tkinter import *
from PIL import Image, ImageTk
import random


root = Tk()
root.geometry('700x700')
root.title('Dice Rolling Simulation')

bg = PhotoImage(file ="bg.png")
label = Label(root, image=bg)
label.place(x=0, y=0, relwidth =1, relheight = 1)



l0 = Label(root, text="")
l0.pack()

l1 = Label(root, text="DICE SIMULATOR", fg="white",
               bg='#000009',
               font="Helvetica 30 bold italic")
l1.pack()
#images
d1 = 'die1.png'
d2 = 'die2.png'
d3 = 'die3.png'
d4 = 'die4.png'
d5 = 'die5.png'
d6 = 'die5.png'

dice = [d1, d2, d3 ,d4 ,d5, d6]
image1 = ImageTk.PhotoImage(Image.open(random.choice(dice)))
label1 =Label(root, image=image1)
label1.image = image1
label1.pack(expand=True)

global result
canvas= Canvas(root, width = 200, height = 50, bg = "red")
canvas.pack(pady = 5)
result = canvas.create_text(100,25, font = ('Helvetica', 24), text = "ONE")

def rolling_dice():
    image1 = ImageTk.PhotoImage(Image.open(random.choice(dice)))
    label1.configure(image=image1)
    label1.image = image1
    if image1 == d1:
        canvas.itemconfig(result, text = "ONE")
    elif image1 == d2:
       canvas.itemconfig(result, text="TWO")
elif image1 == d3:
    canvas.itemconfig(result, text="THREE")

elif image1 == d4:
    canvas.itemconfig(result, text="FOUR")
elif image1 == d5:
    canvas.itemconfig(result, text="FIVE")
elif image1 ==d6:
    canvas.itemconfig(result, text="SIX")


my_button = Button(root, text = "ROLL THE DICE", command = rolling_dice, font = ("Helvetica",24), 
 fg="blue")
my_button.pack(pady=20)

root.mainloop()

【问题讨论】:

    标签: python tkinter canvas random


    【解决方案1】:

    你需要把random.choice(dice)的结果保存在rolling_dice()里面,然后用这个结果来更新图片和文字,而不是用image1

    def rolling_dice():
        choice = random.choice(dice)
        image1 = ImageTk.PhotoImage(Image.open(choice))
        label1.configure(image=image1)
        label1.image = image1
        if choice == d1:
            canvas.itemconfig(result, text = "ONE")
        elif choice == d2:
           canvas.itemconfig(result, text="TWO")
        elif choice == d3:
            canvas.itemconfig(result, text="THREE")
        elif choice == d4:
            canvas.itemconfig(result, text="FOUR")
        elif choice == d5:
            canvas.itemconfig(result, text="FIVE")
        elif choice ==d6:
            canvas.itemconfig(result, text="SIX")
    

    另一种简单的方法是获取一个介于 0 和 5 之间的随机数,并使用该数字来更新图像和文本:

    def rolling_dice():
        idx = random.randrange(len(dice)) # random number between 0 and 5
        image1 = ImageTk.PhotoImage(Image.open(dice[idx]))
        label1.configure(image=image1)
        label1.image = image1
        number = ("ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX")
        canvas.itemconfigure(result, text=number[idx])
    

    【讨论】:

    • 谢谢你UUUUUUU!
    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 2022-11-17
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多