亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按鍵盤鍵時Tkinter無法關閉圖像

按鍵盤鍵時Tkinter無法關閉圖像

慕容708150 2021-12-29 19:22:05
這里的目標是在圖像上使用 tkinter 繪制一個矩形,然后按空格鍵并關閉圖像,但按空格時我無法關閉窗口。我這樣做是為了一個心理學實驗,最近我必須把這張圖片保存在一個文件夾中,如果不是問太多,如果你也幫我解決這個問題,我將非常感激......對不起import tkinter as tk from tkinter import *from PIL import Image, ImageTkimport sysclass 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):        passif __name__ == "__main__":    app = ExampleApp()    app.mainloop()
查看完整描述

1 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

我編輯了你的代碼。


代替


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()



查看完整回答
反對 回復 2021-12-29
  • 1 回答
  • 0 關注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號