python - 如何在 tkinter 画布中将图像移动到其他图像之上?
问题描述
我正在尝试使用 tkinter 制作类似于 Photoshop 的应用程序。我使用 tkinter 的列表框来实现图像分层功能,就像在 Photoshop 中一样,列表框中的每个项目都对应一个导入的图像。当列表框中的一项被拖动到另一项的顶部或下方时,相应的图像也应放置在另一张图像的顶部或下方。当列表框中项目的位置发生变化时,如何在不必重新创建图像的情况下实现这一点。我尝试使用项目配置方法,但它没有工作
这是代码(它不是整个代码,只有相关功能)(也忽略缩进和格式)
from tkinter import*
from tkinter import filedialog
import shutil as shu
from PIL import Image,ImageTk
import os
#importing the image into listbox and canvas
def open(self): #importing the image and inserting into listbox
file=filedialog.askopenfilename()
self.temp.append(file)
for i in self.temp:
shu.copy(i, 'cache/') #copying the files in a folder called cache
layers=os.listdir('cache/')
for i in layers:
if i not in self.layerbox.get(0,END): #self.layerbox is the listbox
self.layerbox.insert(0,i)
self.temp=[]
self.layer_naming+=1
self.drawoncanvas()
os.chdir(self.parent_path)
def drawoncanvas(self):# this function reads the listbox and displays the images accordingly
os.chdir('cache/')
for index,i in enumerate(reversed(self.layerbox.get(0,END))):
if i not in self.laycanconnect:
exec('self.pic'+str(index)+'=ImageTk.PhotoImage(Image.open(i))')
exec('self.pix'+str(index)+'='+ 'self.canvas.create_image(self.canvas.winfo_width()/2,self.canvas.winfo_height()/2,image=self.pic'+str(index)+')')
exec('self.connector(i,self.pic'+str(index)+')') #There are two empty dictionaries to store the image creation variable i.e self.pix and the image ii.e self.pic
exec('self.connector2(i,self.pix'+str(index)+')')
# self.connector and self.connector2 functions references both image and canvas creation variable using the image file name
def connector(self,name,key):
self.laycanconnect[name]=key #self.laycanconnect and self.laycanconnect2 are the two dictionaries
def connector2(self,name,key):
self.laycanconnect2[name]=key
def setcurrent(self,event): #These two functions are bound by mouse events Button 1 and B1-Motion
#inorder to implement dragging and dropping of listbox items
self.curindex=self.layerbox.nearest(event.y)
def shift(self,event):
global corx,cory
i=self.layerbox.nearest(event.y)
y = self.layerbox.get(self.curindex)
corx, cory = self.canvas.coords(self.laycanconnect2[y])
if i<self.curindex: #moving up
x=self.layerbox.get(i)
self.layerbox.delete(i)
self.layerbox.insert(i+1,x)
self.curindex = i
elif i>self.curindex:#moving down
x=self.layerbox.get(i)
corx, cory = self.canvas.coords(self.laycanconnect2[x])
self.layerbox.delete(i)
self.layerbox.insert(i-1,x)
self.curindex=i
for i in reversed(self.layerbox.get(0,END)):
self.canvas.delete(self.laycanconnect2[i])
self.laycanconnect2[i]=self.canvas.create_image(corx,cory,image=self.laycanconnect[i])
#Here is the problem I don't want to recreate the image in order to place
#an image over the other
解决方案
推荐阅读
- php - 当我使用内部连接关键字时得到错误的数据
- javascript - Polymer 3 纸质对话框:未捕获的类型错误:无法读取未定义的属性“对话框”
- python-3.x - 在嵌套列表中查找除第一个之外的每列的最大值
- ionic-framework - IONIC 4:离子搜索栏的 cancelButtonText 不起作用
- css - 跳过菜单 div 并显示内容
- wordpress - Wordpress 多站点插件
- c# - 如何在 MVC 客户端应用程序中使用刷新令牌更新令牌?
- php - PHP按数量范围价格计算定价
- java - 我们能否获得 TestNG 报告,显示测试类是否通过或失败
- android - 如何在android中的谷歌地图上绘制路线并计算多个标记之间的距离