gpt4 book ai didi

python - 按下键盘按键时 Tkinter 无法关闭图像

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

这里的目标是使用 tkinter 在图像上绘制一个矩形,然后按空格键并关闭图像,但按空格时无法关闭窗口。

我正在做一个心理学实验,最近我必须将此图像保存在一个文件夹中,如果要求不是太多,如果你也帮助我解决这个问题,我将非常感激......抱歉

import tkinter as tk 
from tkinter import *
from PIL import Image, ImageTk
import sys


class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
self.canvas = tk.Canvas(self, width=512, height=720, cursor="cross")
self.canvas.pack(side="top", fill="both", expand=True)
self.canvas.bind("<ButtonPress-1>", self.on_button_press)
self.canvas.bind("<B1-Motion>", self.on_move_press)
self.canvas.bind("<ButtonRelease-1>", self.on_button_release)
self.rect = None
self.start_x = None
self.start_y = None
self._draw_image()

# Space to destroy
self.canvas.bind("<space>", self.on_space_press)

def _draw_image(self):
self.im = Image.open('./EFCTupright_04203d903.jpg')
self.tk_im = ImageTk.PhotoImage(self.im)
self.canvas.create_image(0,0,anchor="nw",image=self.tk_im)

# Destroy image:
def on_space_press(self, event):
app.destroy()

def on_button_press(self, event):
# save mouse drag start position
self.start_x = event.x
self.start_y = event.y

# create rectangle if not yet exist
#if not self.rect:
self.rect = self.canvas.create_rectangle(self.x, self.y, 2, 2, fill="", outline="red")

def on_move_press(self, event):
curX, curY = (event.x, event.y)

# expand rectangle as you drag the mouse
self.canvas.coords(self.rect, self.start_x, self.start_y, curX, curY)


def on_button_release(self, event):
pass



if __name__ == "__main__":
app = ExampleApp()
app.mainloop()

提前致谢

最佳答案

我编辑了你的代码。

而不是

self.canvas.bind("<space>", self.on_space_press)

使用

self.bind("<space>", self.on_space_press)

我还在您的代码中添加了保存功能

def save_file(self):
# This will draw a rectangle on the image
self.draw.rectangle( self.rec_coords, outline=self.outline_color)

# Name of the file and format you want to save as
self.im.save("out_put.jpg","JPEG")

完整代码

import tkinter as tk 
from tkinter import *
from PIL import Image, ImageTk, ImageDraw
import sys

class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
self.canvas = tk.Canvas(self, width=512, height=720, cursor="cross", highlightthickness=0)
self.canvas.pack(side="top", fill="both", expand=True)
self.update()
self.canvas.bind("<ButtonPress-1>", self.on_button_press)
self.canvas.bind("<B1-Motion>", self.on_move_press)
self.canvas.bind("<ButtonRelease-1>", self.on_button_release)
self.rect = None
self.start_x = None
self.start_y = None
self.outline_color = "red"
self._draw_image()

# Space to destroy
self.bind("<space>", self.on_space_press)

def _draw_image(self):
self.im = Image.open('./EFCTupright_04203d903.jpg').resize((self.winfo_reqwidth(), self.winfo_reqheight()))
self.tk_im = ImageTk.PhotoImage(self.im)
self.draw = ImageDraw.Draw(self.im)
self.canvas.create_image(0,0,anchor="nw",image=self.tk_im)

# Destroy image:
def on_space_press(self, event):
self.save_file()
self.destroy()

def on_button_press(self, event):
# save mouse drag start position
self.start_x = event.x
self.start_y = event.y

# create rectangle if not yet exist
#if not self.rect:
self.rect = self.canvas.create_rectangle(self.x, self.y, 0, 0, outline=self.outline_color, tag="rect")

def on_move_press(self, event):
curX, curY = (event.x, event.y)
# expand rectangle as you drag the mouse
self.canvas.coords(self.rect, self.start_x, self.start_y, curX, curY)

def on_button_release(self, event):
pass

def save_file(self):
# This will draw a rectangle on the image
for rect in self.canvas.find_withtag("rect"):
self.draw.rectangle( self.canvas.coords(rect), outline=self.outline_color)

# Name of the file and format you want to save as
self.im.save("out_put.jpg","JPEG")


if __name__ == "__main__":
app = ExampleApp()
app.mainloop()

关于python - 按下键盘按键时 Tkinter 无法关闭图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55526496/

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