Tkinter 绑定不调用函数

Tkinter bind does not call funtion

我正在尝试更改 tkinter 条目小部件中的文本,使其成为用户输入的组合键(例如:ShiftL+ShiftR),python 程序 运行 没问题,但不更改条目,为什么以及如何修复它? 我的图形用户界面:

 # Program by Fares Al Ghazy started 20/5/2017
# Python script to assign key combinations to bash commands, should run in the background at startup
# Since this program is meant to release bash code, it is obviously non-system agnostic and only works linux systems that use BASH
# This is one file which only creates the GUI, another file is needed to use the info taken by this program

FileName  = 'BinderData.txt'
import tkinter as tk
from ComboDetect import ComboDetector

# Create a class to get pressed keys and print them
KeyManager = ComboDetector()


# Class that creates GUI and takes info to save in file

class MainFrame(tk.Tk):
    # variable to store pressed keys
    KeyCombination = ""

    def KeysPressed(self, Entry, KeyCombination):
        KeyCombination = KeyManager.getpressedkeys()
        Entry.delete(0, tk.END)
        Entry.insert(0, KeyCombination)

    # constructor

    def __init__(self, FileName, **kwargs):
        tk.Tk.__init__(self, **kwargs)
        # create GUI to take in key combinations and bash codes, then save them in file
        root = self  # create new window
        root.wm_title("Key binder")  # set title
        #  create labels and text boxes
        KeyComboLabel = tk.Label(root, text="Key combination = ")
        KeyComboEntry = tk.Entry(root)

        # Bind function to entry

        KeyComboEntry.bind('<FocusIn>',self.KeysPressed(KeyComboEntry, self.KeyCombination))

        KeyComboEntry.grid(row=0, column=1)
        ActionEntry.grid(row=1, column=1)
        # create save button
        SaveButton = tk.Button(root, text="save",
                               command=lambda: self.SaveFunction(KeyComboEntry, ActionEntry, FileName))
        SaveButton.grid(row=2, column=2, sticky=tk.E)


app = MainFrame(FileName)
app.mainloop()

和 ComboDetect:

   #this program was created by LadonAl (Alaa Youssef) in 25.May.17
    #it detects a combination of pressed key and stores them in a list and prints the list when at least
    # one of the keys is released

import time
import pyxhook

class ComboDetector(object):
    def getpressedkeys(self):
        return self.combo

编辑:我已经更改了按键功能来测试它

 def KeysPressed(self, Entry, KeyCombination):
        Entry.config(state="normal")
        Entry.insert(tk.END, "Test")
        print("test")
        KeyCombination = KeyManager.getpressedkeys()
        Entry.delete(0, tk.END)
        Entry.insert(tk.END, KeyCombination)

这是我注意到的: 当模块是 运行 时,"test" 被打印到控制台,没有其他事情发生。 当我尝试在条目小部件外部单击并再次在其中单击(退出焦点并重新输入)时,没有任何反应

问题是,当您尝试绑定到 KeyComboEntry 时,您正在调用过程 KeysPressed,而不是将 bind 传递给 KeyComboEntry 的方法,您可以使用 KeyComboEntry.bind("<Key>", self.KeysPressed, KeyComboEntry, self.KeyCombination) 来解决这个问题。然后,Bind 将使用 KeyComboEntryself.KeyCombination 的参数调用 KeysPressed。另一种选择是使用 lambda 函数,就像您在 SaveButton 中使用的那样,考虑 bind 将事件传递给它。