我对 python 很陌生, 我在基于图像的项目中工作,我想在画布上水平移动多个图像,逐行(从左到右)。
这里是一个示例代码:-
from tkinter import*
from PIL import Image,ImageTk
import time
import cv2
win=Tk()
win.geometry("700x350")
canvas_1 = Canvas(win,width=600, height=60, bg="white")
canvas_1.pack(pady=20)
img = Image.open('image2.jpg')
img_0 =img.resize((80,30))
img_1 = ImageTk.PhotoImage(img_0)
img_2 = canvas_1.create_image(0,0, anchor='nw', image=img_1)
h,w = img_0.size
x,y=h,0
def move_fn(x,y):
canvas_1.move(img_2,x,y)
move_fn(x,y)
win.mainloop()
请帮帮我。
我想在水平画布(x 轴)上移动图像。 我想读取图像并在左侧创建该图像,然后再次读取另一张图像,之前的图像应移至右侧,最新图像应在左侧创建,然后继续此过程,直到画布有 10 张图像。最后最右边的图像应该自动删除。
对左右都使用
win.bind
。还没有测试它。
片段:
from tkinter import*
from PIL import Image,ImageTk
import time
import cv2
win=Tk()
win.geometry("700x350")
canvas_1 = Canvas(win,width=600, height=60, bg="white")
canvas_1.pack(pady=20)
img = Image.open('image2.jpg')
img_0 =img.resize((80,30))
img_1 = ImageTk.PhotoImage(img_0)
img_2 = canvas_1.create_image(0,0, anchor='nw', image=img_1)
def move_right_1(e):
x = 20
y = 0
canvas_1.move(img_2,x,y)
def move_left_1(e):
x = -20
y = 0
canvas_1.move(img_2,x,y)
def move_right_2(e):
x = 20
y = 0
canvas_1.move(img_2,x,y)
def move_left_2(e):
x = -20
y = 0
canvas_1.move(img_2,x,y)
win.bind("<Left>", move_left)
win.bind("<Right>", move_right)
win.bind("<Left>", move_left_2)
win.bind("<Right>", move_right_2)
win.mainloop()