当按钮被点击时是否停止?

2024-04-19 16:46:44 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我用Tkinter做了一个游戏,但是我想做的是当我点击键盘上的一个按钮,比如“w”,它运行一个函数,比如x增加5。你知道吗

这是我的密码。你知道吗

__author__ = 'Zac'
from Tkinter import *
from random import randint

class Application:
    def circle(self, r, x, y):
        return (x-r, y-r, x+r, y+r)

    def square(self, s, x, y):
        return (x, y, s, s)

    def __init__(self, canvas, r, x, y):
        self.canvas = canvas
        self.r = r
        self.x = x
        self.y = y
        self.ball = canvas.create_oval(self.circle(r, x, y))


root = Tk()
canvas = Canvas(root, width = 1000, height = 1000)
canvas.pack()

ball1 = Application(canvas, 20, 50, 50)


root.mainloop()

Tags: 函数fromimportself游戏密码returnapplication
1条回答
网友
1楼 · 发布于 2024-04-19 16:46:44

使用widget.bind方法将keypress与事件处理程序绑定。你知道吗

例如:

....

ball1 = Application(canvas, 20, 50, 50)

def increase_circle(event):
    canvas.delete(ball1.ball)
    ball1.r += 5
    ball1.ball = canvas.create_oval(ball1.circle(ball1.r, ball1.x, ball1.y))

root.bind('<w>', increase_circle)  # < - Bind w-key-press with increase_circle

root.mainloop()

Events and Bindings。你知道吗

相关问题 更多 >