我正在尝试更改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:
^{pr2}$编辑:我已经改变了按键功能来测试它
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”被打印到console上,其他什么都不会发生。 当我尝试单击entry小部件的外部并再次单击它内部时(退出focus并重新进入它),什么都不会发生
问题是,当您试图绑定到}的方法,您可以使用}的参数调用
KeyComboEntry
时,您调用的是过程KeysPressed
,而不是将bind
传递给{KeyComboEntry.bind("<Key>", self.KeysPressed, KeyComboEntry, self.KeyCombination)
来解决这个问题。Bind将使用KeyComboEntry
和{KeysPressed
。另一种选择是使用lambda
函数,就像您在SaveButton
中使用的一样,说明bind
向它传递一个事件。在相关问题 更多 >
编程相关推荐