gpt4 book ai didi

python - 导入 .gif 的最简单代码

转载 作者:行者123 更新时间:2023-12-01 04:45:32 24 4
gpt4 key购买 nike

我正在寻找最简单的代码来在我的程序中运行 .gif。该文件名为“vault_ext.gif”。到目前为止我已经:

from tkinter import *

def vault():
photo = PhotoImage(file="vault_ext.gif")

vault()

最佳答案

>>> import tkinter as tk
>>> root = tk.Tk()
>>> w = 500 # replace 500 with the width of your photo
>>> h = 600 # replace 600 with the height of your photo
>>> canvas = tk.Canvas(root, width=w, height=h)
>>> canvas.pack()
>>> img = tk.PhotoImage(file='vault_ext.gif')
>>> img_ref = canvas.create_image(w//2, h//2, image=img)

这是显示图像的基本方法。我建议寻找使用类的 tkinter 应用程序示例,以便您可以更轻松地实现交互性。 Effbot's tkinterbook是一个很好的资源。

让我们添加一个绑定(bind):

>>> def printer(event):
... print(event.x, event.y)
...
>>> canvas.bind("<Button-1>", printer)

每次您左键单击图片时,它都会打印您单击位置的 x,y 坐标(坐标从左上角开始)。

关于python - 导入 .gif 的最简单代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com