导入 .gif 的最简单代码
Simplest code to import a .gif
我正在寻找在我的程序中 运行 .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 是很好的资源。
让我们添加一个绑定:
>>> def printer(event):
... print(event.x, event.y)
...
>>> canvas.bind("<Button-1>", printer)
每次您左键单击图片时,它都会打印您单击位置的 x、y 坐标(坐标从左上角开始)。
我正在寻找在我的程序中 运行 .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 是很好的资源。
让我们添加一个绑定:
>>> def printer(event):
... print(event.x, event.y)
...
>>> canvas.bind("<Button-1>", printer)
每次您左键单击图片时,它都会打印您单击位置的 x、y 坐标(坐标从左上角开始)。