在tkinter中移动和调整图像画布对象的大小

时间:2017-02-24 13:48:16

标签: python tkinter

我想移动并调整tkinter画布图像对象的大小。 它可以很好地移动图像。 但使用绑定或按钮调整图像大小是错误的。帮我。 也许,我不熟悉bind和button的命令功能(参数)。 谁通过示例代码轻松解释我.. 我的英语很糟糕..

from tkinter import *
from PIL import Image, ImageTk
import os
import PIL.Image

def  move_rectangular(event):
    if event.keysym == 'Up':
        canvas.move(object_1, 0, -3)
      #  canvas.move(object_2, 0, -3)
    if event.keysym == 'Down':
        canvas.move(object_1, 0, 3)
      #  canvas.move(object_2, 0, 3)   
    if event.keysym == 'Left':
        canvas.move(object_1, -3, 0)
     #   canvas.move(object_2, -3, 0)  
    if event.keysym == 'Right':
        canvas.move(object_1, 3, 0)
    #    canvas.move(object_2, 3, 0)
def  size_change(event, obj, img):    
    if event.keysym == 'b':        
        width = img.width
        height = img.height
        w = width+10
        h = height+10
        canvas.config(obj,width=w, height=h)

    if event.keysym == 's':        
        width = img.width
        height = img.height
        w = width-10
        h = height-10
        canvas.config(obj,width=w, height=h)

def size_down(obj,img):
        width = img.width
        height = img.height
        w = width-10
        h = height-10
        canvas.config(obj,width=w, height=h)
def size_up(obj,img):
        width = img.width
        height = img.height
        w = width+10
        h = height+10
        canvas.config(obj,width=w, height=h)

tk = Tk()
tk.geometry("1400x1200")
tk.title("move,resize")
mainMenu = Menu(tk)
tk.config(menu=mainMenu)


canvas = Canvas(tk, width=1300, height=853)
canvas.grid(row=0, column=2, sticky=E)

img = Image.open(os.getcwd() + '/rectangular.jpg')
photo3 = ImageTk.PhotoImage(img)
object_1 = canvas.create_image(300,220, anchor=NW, image=photo3)

canvas.bind_all('<KeyPress-Up>', move_rectangular)
canvas.bind_all('<KeyPress-Down>', move_rectangular)
canvas.bind_all('<KeyPress-Left>', move_rectangular)
canvas.bind_all('<KeyPress-Right>', move_rectangular)
# canvas bind size change
canvas.bind_all('<KeyPress-s>', lambda event, a=object_1, b=img: size_change(a, b))
canvas.bind_all('<KeyPress-b>', lambda event, a=object_1, b=img: size_change(a, b))

#button canvas size change
btn = Button(tk, text="size-", command=lambda:size_down(object_1,img))
btn.grid(row=1, column=2, sticky=N)
btn = Button(tk, text="size+", command=lambda:size_up(object_1,img))
btn.grid(row=2, column=2, sticky=N)

tk.mainloop()
  

_cnfmerge:后备原因:&#39; int&#39; object不可迭代Tkinter回调中的异常Traceback(最近一次调用last):File   &#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,107行,_cnfmerge       cnf.update(c)TypeError:&#39; int&#39;对象不可迭代

     

在处理上述异常期间,发生了另一个异常:

     

Traceback(最近一次调用最后一次):文件   &#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,第1482行,通话       return self.func(* args)File&#34; D:/test/move,resize.py" ;,第70行,in       btn =按钮(tk,text =&#34; size - &#34 ;, command = lambda:size_down(object_1,img))文件   &#34; D:/test/move,resize.py",第39行,在size_down中       canvas.config(obj,width = w,height = h)File&#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,line 1270,in configure       return self._configure(&#39; configure&#39;,cnf,kw)文件&#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,第1254行,在_configure中       cnf = _cnfmerge((cnf,kw))文件&#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,第110行,在_cnfmerge中       for k,v in c.items():AttributeError:&#39; int&#39;对象没有属性&#39; items&#39;   _cnfmerge:后备原因:&#39; int&#39; object不可迭代Tkinter回调中的异常Traceback(最近一次调用last):File   &#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,107行,_cnfmerge       cnf.update(c)TypeError:&#39; int&#39;对象不可迭代

     

在处理上述异常期间,发生了另一个异常:

     

Traceback(最近一次调用最后一次):文件   &#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,第1482行,通话       return self.func(* args)File&#34; D:/test/move,resize.py" ;,第72行,in       btn = Button(tk,text =&#34; size +&#34 ;, command = lambda:size_up(object_1,img))File&#34; D:/test/move,resize.py",   第45行,size_up       canvas.config(obj,width = w,height = h)File&#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,line 1270,in configure       return self._configure(&#39; configure&#39;,cnf,kw)文件&#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,第1254行,在_configure中       cnf = _cnfmerge((cnf,kw))文件&#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,第110行,在_cnfmerge中       for k,v in c.items():AttributeError:&#39; int&#39;对象没有属性&#39; items&#39; Tkinter回调Traceback中的异常(最新版本   最后调用):文件&#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,第1482行,   在通话       return self.func(* args)File&#34; D:/test/move,resize.py" ;,第66行,in       canvas.bind_all(&#39;&#39;,lambda事件,a = object_1,b = img:size_change(a,b))TypeError:size_change()缺少1需要   位置论证:&#39; img&#39; Tkinter回调Traceback中的异常   (最近一次调用最后一次):File&#34; C:\ Python34 \ lib \ tkinter__init __。py&#34;,   第1482行,致电       return self.func(* args)File&#34; D:/test/move,resize.py" ;,第67行,in       canvas.bind_all(&#39;&#39;,lambda事件,a = object_1,b = img:size_change(a,b))TypeError:size_change()缺少1需要   位置论证:&#39; img&#39;

0 个答案:

没有答案