【问题标题】:How do we call itterative function in tkinter - Python-?我们如何在 tkinter - Python- 中调用迭代函数?
【发布时间】:2019-09-05 13:55:26
【问题描述】:

我是 tkinter 的新手,我想从我按下键盘上的一个键的那一刻起开始一种循环。 只是,当我调用我的第一个函数时,我有一个错误:

TypeError:anime_avancer () 缺少 1 个必需的位置参数:'event'。

我知道绑定方法有关系,但我不明白结构... 提前谢谢你!

from tkinter import *

fen=Tk()
can=Canvas(fen,bg="light gray", height=500, width=500)
can.pack()

def afficher_codeur():
    #code here
    anime_avancer()

def anime_avancer(event):
    #code here#
    afficher_codeur()

fen.bind("<Right>", anime_avancer)

fen.mainloop()

如果可能的话,我想解释一下“事件”的作用,谢谢! ;)

【问题讨论】:

  • 您的anime_avancer 回调调用afficher_codeur 再次调用自己anime_avancer,但没有参数而不是一个。你期待什么?
  • 那么我应该放入什么(le cul)?

标签: python-3.x tkinter


【解决方案1】:

您的anime_avancer 回调调用了afficher_codeur,它再次调用了自己anime_avancer,但没有参数而不是一个。因此出现运行时错误。

如果您想在某个时候调用您的 anime_avancer 方法但不带任何参数,只需调用 anime_avancer(None)

event 在 Tkinter 画布回调中是一个位置参数,用于存储有关触发事件时键盘和鼠标状态的各种信息。例如,event.xevent.y 存储鼠标的位置。

请参阅 Canvas.bind() here 的完整文档。

here 提供了有关如何在 Tk 中使用回调方法的完整示例。

【讨论】:

    【解决方案2】:

    感谢 Antoine C,答案是在 afficher_codeur 循环中将“无”作为anime_avancer 函数的参数!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多