在python tkinter画布中移动对象 [英] Move object in python tkinter canvas

查看:53
本文介绍了在python tkinter画布中移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一艘潜艇:

from tkinter import *
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title("Bubble Blaster")
c = Canvas(window, width=WIDTH, height=HEIGHT, bg="darkblue")
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill="red")
ship_id2 = c.create_oval(0, 0, 30, 30, outline="red")  

而且我正在阅读一本关于如何使子移动的书,它是这样说的:

and Im reading a book on how to make the sub move, this is what it says:

def move_ship(event):
    if event.keysym == "up":
        c.move(ship_id, 0, -ship_spd)
        c.move(ship_id2, 0, -ship_spd)
    elif event.keysym == "Down":
        c.move(ship_id, 0, ship_spd)
        c.move(ship_id2, 0, ship_spd)
    elif event.keysym == "Left":
        c.move(ship_id, -ship_spd, 0)
        c.move(ship_id2,  -ship_spd, 0)
    elif event.keysym == "Right":
        c.move(ship_id, ship_spd, 0)
        c.move(ship_id2,  ship_spd, 0)
    c.bind_all('<key', move_ship)

当我运行它时,它给了我一个错误:
PS.我在一个片段中这样做,因为 Ctrl+v 不会处理所有消息

When I run it, it gives me an error:
PS. Im doing it in a snippet because Ctrl+v wont do all of the message

<h4 style="color: red">Traceback (most recent call last):
  File "C:\Users\Bloxy Craft\Desktop\Bubble Blaster.py", line 28, in &lt;module&gt;
    c.bind_all('&lt;key&gt;', move_ship)
  File "C:\Users\Bloxy Craft\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1257, in bind_all
    return self._bind(('bind', 'all'), sequence, func, add, 0)
  File "C:\Users\Bloxy Craft\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1200, in _bind
    self.tk.call(what + (sequence, cmd))
_tkinter.TclError: bad event type or keysym "key"</h4>

有人可以帮我吗?
谢谢!
- Bloxy 工艺

Can someone help me?
Thanks!
- Bloxy Craft

推荐答案

喜欢 Bryan Oakley 说你需要将 ' 替换为 ''.您还需要在代码中将 if event.keysym == "up": 替换为 if event.keysym == "Up": (U in 'Up' 必须成为资本).孔代号为:

Like Bryan Oakley said you need to replace '<key' with '<Key>'. Also you need to replace if event.keysym == "up": with if event.keysym == "Up": in your code (U in 'Up' must be capital). Hole code is:

from tkinter import *
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title("Bubble Blaster")
c = Canvas(window, width=WIDTH, height=HEIGHT, bg="darkblue")
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill="red")
ship_id2 = c.create_oval(0, 0, 30, 30, outline="red")

def move_ship(event):
    if event.keysym == "Up":
        c.move(ship_id, 0, -ship_spd)
        c.move(ship_id2, 0, -ship_spd)
    elif event.keysym == "Down":
        c.move(ship_id, 0, ship_spd)
        c.move(ship_id2, 0, ship_spd)
    elif event.keysym == "Left":
        c.move(ship_id, -ship_spd, 0)
        c.move(ship_id2,  -ship_spd, 0)
    elif event.keysym == "Right":
        c.move(ship_id, ship_spd, 0)
        c.move(ship_id2,  ship_spd, 0)

c.bind_all('<Key>', move_ship)

这篇关于在python tkinter画布中移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆