Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用canvas-Python实现汇总图像_Python_Image_Canvas_Tkinter - Fatal编程技术网

使用canvas-Python实现汇总图像

使用canvas-Python实现汇总图像,python,image,canvas,tkinter,Python,Image,Canvas,Tkinter,我想要一个图像在画布中向上滚动。但是我做不到 import tkinter def scroll_image(event): global roll roll = roll - 10 canvas1.itemconfig(canvas1.create_image(20, roll, image=i)) roll = 10 windows = tkinter.Tk() windows.title("My Application") # Adding canvas

我想要一个图像在画布中向上滚动。但是我做不到

import tkinter


def scroll_image(event):
    global roll
    roll = roll - 10
    canvas1.itemconfig(canvas1.create_image(20, roll, image=i))


roll = 10
windows = tkinter.Tk()
windows.title("My Application")


# Adding canvas to show image there
canvas1 = tkinter.Canvas(windows, width=200, height=100)
i = tkinter.PhotoImage(file="Capture.gif")
canvas1.create_image(20, 20, image=i)

# trying to implement roll-up image
canvas1.itemconfig(canvas1.create_image(20, 20, image=i))
canvas1.bind("<Configure>", scroll_image)

canvas1.grid(column=0, row=2)


windows.mainloop()
导入tkinter
def scroll_图像(事件):
全球滚动
滚转=滚转-10
canvas1.itemconfig(canvas1.create_image(20,roll,image=i))
滚动=10
windows=tkinter.Tk()
标题(“我的应用程序”)
#添加画布以在那里显示图像
canvas1=tkinter.Canvas(窗口,宽度=200,高度=100)
i=tkinter.PhotoImage(file=“Capture.gif”)
画布1.创建图片(20,20,图片=i)
#正在尝试实现卷取映像
canvas1.itemconfig(canvas1.create_image(20,20,image=i))
canvas1.bind(“,滚动图片)
canvas1.grid(列=0,行=2)
mainloop()

我尝试使用循环,但我注意到循环按预期运行,但不幸的是画布更新只进行了一次。所以我去掉了这个循环。但我需要找到一种方法来实现一个上卷映像。

映像正在上下移动

本例使用

  • 模块
    PIL/枕头
    加载
    jpg/png
    而不是
    gif
  • after(时间单位为毫秒,函数名称)
    重复移动图像的函数
  • img\u id
    仅使用一个图像(而不是使用
    create\u image
    创建多个图像)
  • canvas.move(ID,offset\x,offset\y)
    移动图像(或其他对象)
  • canvas.coords(ID)
    获取图像(或其他对象)的当前位置
  • canvas.pack(fill='both',expand=True)
    使用完整窗口。画布将使用完整窗口,即使在调整窗口大小时也是如此
代码:

将tkinter作为tk导入
从PIL导入图像,ImageTk
def scroll_image():
全局偏移量_y#通知函数要将值赋给外部变量而不是局部变量。
#移动图像
canvas.move(img\u id、offset\u x、offset\u y)
#获取当前位置
x、 y=canvas.coords(img_id)
打印(x,y)
#设置位置(如果不使用canvas.move)
#canvas.coords(img_id,x+offset_x,y+offset_y)
#x+=偏移量x
#y+=偏移量
#转向
如果y=0:
偏移量y=-偏移量y
#20毫秒后重复
root.after(20,滚动图片)
偏移量x=0
偏移量y=-3
root=tk.tk()
canvas=tk.canvas(根)
canvas.pack(fill='both',expand=True)#使用完整窗口
#photo=tk.PhotoImage(file=“Capture.gif”)
image=image.open(“image.jpg”)
photo=ImageTk.PhotoImage(图像)
img_id=canvas.create_image(0,0,image=photo)
#开始滚动
滚动_图像()
root.mainloop()
您还可以使用
canvas.coords(ID,x,y)
设置新位置



GitHub上的更多示例:和

图像上下移动

本例使用

  • 模块
    PIL/枕头
    加载
    jpg/png
    而不是
    gif
  • after(时间单位为毫秒,函数名称)
    重复移动图像的函数
  • img\u id
    仅使用一个图像(而不是使用
    create\u image
    创建多个图像)
  • canvas.move(ID,offset\x,offset\y)
    移动图像(或其他对象)
  • canvas.coords(ID)
    获取图像(或其他对象)的当前位置
  • canvas.pack(fill='both',expand=True)
    使用完整窗口。画布将使用完整窗口,即使在调整窗口大小时也是如此
代码:

将tkinter作为tk导入
从PIL导入图像,ImageTk
def scroll_image():
全局偏移量_y#通知函数要将值赋给外部变量而不是局部变量。
#移动图像
canvas.move(img\u id、offset\u x、offset\u y)
#获取当前位置
x、 y=canvas.coords(img_id)
打印(x,y)
#设置位置(如果不使用canvas.move)
#canvas.coords(img_id,x+offset_x,y+offset_y)
#x+=偏移量x
#y+=偏移量
#转向
如果y=0:
偏移量y=-偏移量y
#20毫秒后重复
root.after(20,滚动图片)
偏移量x=0
偏移量y=-3
root=tk.tk()
canvas=tk.canvas(根)
canvas.pack(fill='both',expand=True)#使用完整窗口
#photo=tk.PhotoImage(file=“Capture.gif”)
image=image.open(“image.jpg”)
photo=ImageTk.PhotoImage(图像)
img_id=canvas.create_image(0,0,image=photo)
#开始滚动
滚动_图像()
root.mainloop()
您还可以使用
canvas.coords(ID,x,y)
设置新位置



关于GitHub的更多示例:和

如何创建循环?您是否使用了
window.after()
来创建不停止
mainloop()
的函数,该函数会重新绘制窗口中的元素?或者您是否在循环中使用
window.update()
请求'mainloop()重新绘制窗口?@furas nope我刚才使用了for循环。你们看,我是一个初学者,正在努力自学Python。你们是如何创建循环的?您是否使用了
window.after()
来创建不停止
mainloop()
的函数,该函数会重新绘制窗口中的元素?或者您是否在循环中使用
window.update()
请求'mainloop()重新绘制窗口?@furas nope我刚才使用了for循环。你看,我是一个初学者,正在努力自学Python。我还没有运行它,但是有没有其他方法可以不用PIL模块就达到同样的效果?因为该模块不是内置的,实际上我只想使用内置模块。没有PIL,您只能使用GIF或PPM。医生:没有这个限制,我有什么问题吗?因为如果这是唯一的限制,我可以应付。一切取决于你需要什么。特金特还有其他一些问题。您可以在画布上绘制,但不能将其保存在位图文件中-只能保存在postscript中。它不能显示HTML。小部件不能有透明的背景,所以不能创建透明窗口或形状不规则的窗口。当它的窗口没有焦点时,它不能获取按键事件——当窗口被最小化时,你不能创建可以运行功能的键盘记录器或全局键。但在开始时,你必须了解PhotoIm中的错误
import tkinter as tk
from PIL import Image, ImageTk


def scroll_image():
    global offset_y  # inform function that you want to assign value to external variable instead of local one.

    # move image
    canvas.move(img_id, offset_x, offset_y)

    # get current position
    x, y = canvas.coords(img_id)
    print(x, y)

    # set position (if you don't use canvas.move)
    #canvas.coords(img_id, x+offset_x, y+offset_y)
    # x += offset_x
    # y += offset_y

    # change direction
    if y <= -100 or y >= 0:
        offset_y = -offset_y

    # repeat after 20ms
    root.after(20, scroll_image)


offset_x = 0
offset_y = -3

root = tk.Tk()

canvas = tk.Canvas(root)
canvas.pack(fill='both', expand=True) # use full window

#photo = tk.PhotoImage(file="Capture.gif")
image = Image.open("image.jpg")
photo = ImageTk.PhotoImage(image)
img_id = canvas.create_image(0, 0, image=photo)

# start scrolling
scroll_image()

root.mainloop()