hanweicheng1 2022-11-12 18:01 采纳率: 100%
浏览 15
已结题

tkinter canvas move 和 tkinter bind 达不到预期效果

tkinter canvas move 和 tkinter bind 达不到预期效果

初学者,变量命名不规范,请见谅。

源码:
# -*- coding: UTF-8 -*-
from tkinter import Tk,Canvas
from PIL import Image,ImageTk
class Main:
    def Main_gui (self):
        self.root = Tk()
        self.root.title('game')
        self.root.state('zoomed')
        self.root.update()
        self.width = (self.root.winfo_width())
        self.height = (self.root.winfo_height())
        self.ci2x = (500)
        self.ci2y = (500)
        _image1 = Image.open('bgimage.png')
        __image1 = _image1.resize((self.width,self.height))
        self.image1 = ImageTk.PhotoImage(__image1)
        self.image2 = ImageTk.PhotoImage(_image1)
        self.c1 = Canvas(self.root,height=self.height,width=self.width,bg='green')
        self.ci1 = self.c1.create_image(0,0,image=self.image1,anchor='nw')
        self.ci2 = self.c1.create_image(self.ci2x,self.ci2y,image=self.image2,anchor='nw')
        self.c1.pack()
        self.root.bind('<Key>',self.Move)
        self.root.focus_set()
        self.root.mainloop()
    def Move (self,event):
        if (event.keysym == 'w'):
            self.ci2y -= (5)
        elif (event.keysym == 'a'):
            self.ci2x -= (5)
        elif (event.keysym == 's'):
            self.ci2y += (5)
        elif (event.keysym == 'd'):
            self.ci2x += (5)
        else:
            return
        self.c1.move(self.ci2,self.ci2x,self.ci2y)
_Main = Main()
_Main.Main_gui()
预期:

按w图片往上
按a图片往左
按s图片往下
按d图片往右

实际效果:

按wasd图片就会移到下图位置:

img


原本位置:

img

  • 写回答

1条回答 默认 最新

  • Smart-Space 2022-11-18 16:56
    关注

    第36行使用moveto方法,你的代码中给出的是绝对坐标,不是相对移动坐标。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月25日
  • 已采纳回答 11月25日
  • 创建了问题 11月12日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。