Python/Tkinter 事件参数问题 [英] Python/Tkinter Event Argument Issue
问题描述
我的代码似乎运行良好,棕色方块出现在空白窗口中,直到我尝试按下其中一个键,当我这样做时,除了出现错误消息之外没有任何反应.有什么想法吗?
from tkinter import *x, y, a, b = 50, 50, 100, 100d = 无vel_dic = {"左": ("左", -4, 0),"右": ("右", 4, 0),"向下": ("向下", 0, 4),"上": ("上", 0, -4)}类精灵:def __init__(self):自我移动self.x, self.y, self.a, self.b = 50, 50, 100, 100自我.d = 0self.canvas = Canvas(tk, height = 600, width = 600)self.canvas.grid(row=0, column=0,sticky = W)self.coord = [self.x, self.y, self.a, self.b]self.shape = self.canvas.create_rectangle(*self.coord, outline = "#cc9900", fill = "#cc9900")定义移动():如果 self.direction != 0:self.canvas.move(self.rect, self.xv, self.yv)tk.after(33, 移动)def on_keypress(事件):self.direction, self.xv, self.yv = vel_dic[event.keysym]def on_keyrelease(事件):self.direction = 0tk = tk()tk.geometry("600x600")sprite1 = Sprite()tk.bind_all('', sprite1.on_keypress)tk.bind_all('', sprite1.on_keyrelease)
按向右箭头键时的错误消息:
Tkinter 回调中的异常回溯(最近一次调用最后一次):文件C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\idlelib\run.py",第 137 行,在 mainseq, request = rpc.request_queue.get(block=True, timeout=0.05)文件C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\queue.py",第 172 行,在 get提高空队列.空在处理上述异常的过程中,又发生了一个异常:回溯(最近一次调用最后一次):文件C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py",第 1699 行,在 __call__ 中返回 self.func(*args)类型错误:on_keypress() 采用 1 个位置参数,但给出了 2 个Tkinter 回调中的异常回溯(最近一次调用最后一次):文件C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\idlelib\run.py",第 137 行,在 mainseq, request = rpc.request_queue.get(block=True, timeout=0.05)文件C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\queue.py",第 172 行,在 get提高空队列.空在处理上述异常的过程中,又发生了一个异常:回溯(最近一次调用最后一次):文件C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py",第 1699 行,在 __call__ 中返回 self.func(*args)类型错误:on_keyrelease() 采用 1 个位置参数,但给出了 2 个
在对象内部调用函数时,self
(对象的实例)作为第一个参数发送.您可以使用一些方法来撤消它,staticmethod
作为最常见的方法,但这不是您在这种情况下要寻找的.p>
你得到的错误表明解释器发送了这个self
参数和常规的event
参数,但你的方法只有一个参数,无法处理.>
确保您的所有函数都将 self
(或您选择的任何名称,如 inst
)作为第一个参数以及其他参数:
def on_keyrelease(self, event):
move
和 on_keypress
也是如此.
my code seems to run fine with the brown square appearing in the blank window until I try and press one of the keys, when I do that nothing happens apart from an error message appearing. Any ideas?
from tkinter import *
x, y, a, b = 50, 50, 100, 100
d = None
vel_dic = {
"Left": ("left", -4, 0),
"Right": ("right", 4, 0),
"Down": ("down", 0, 4),
"Up": ("up", 0, -4)}
class Sprite:
def __init__(self):
self.move
self.x, self.y, self.a, self.b = 50, 50, 100, 100
self.d = 0
self.canvas = Canvas(tk, height = 600, width = 600)
self.canvas.grid(row=0, column=0, sticky = W)
self.coord = [self.x, self.y, self.a, self.b]
self.shape = self.canvas.create_rectangle(*self.coord, outline = "#cc9900", fill = "#cc9900")
def move():
if self.direction != 0:
self.canvas.move(self.rect, self.xv, self.yv)
tk.after(33, move)
def on_keypress(event):
self.direction, self.xv, self.yv = vel_dic[event.keysym]
def on_keyrelease(event):
self.direction = 0
tk = Tk()
tk.geometry("600x600")
sprite1 = Sprite()
tk.bind_all('<KeyPress>', sprite1.on_keypress)
tk.bind_all('<KeyRelease>', sprite1.on_keyrelease)
Error message when I press the right arrow key:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\idlelib\run.py", line 137, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\queue.py", line 172, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
TypeError: on_keypress() takes 1 positional argument but 2 were given
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\idlelib\run.py", line 137, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\queue.py", line 172, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\robsa\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
TypeError: on_keyrelease() takes 1 positional argument but 2 were given
When calling to function inside an object, self
(the instance of the object) is sent as a first argument. You can undo that with some methods, staticmethod
as the most common one, but that is not what you look for in that case.
The error you get indicates the interpreter sent this self
parameter and the regular event
parameter, but your method gets only one parameter, and can not handle them.
Make sure all your functions gets self
(or any name you choose like inst
) as a first parameter in addition to the other parameters:
def on_keyrelease(self, event):
Same goes for move
and on_keypress
.
这篇关于Python/Tkinter 事件参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!