Python 自动热键绑定键“;1“;至;9“;重新分配属性值的步骤
我想将键Python 自动热键绑定键“;1“;至;9“;重新分配属性值的步骤,python,lambda,tkinter,Python,Lambda,Tkinter,我想将键1绑定到9(尽可能少),以使每个键更改我类中的当前类别 我的代码: from tkinter import * class MyCanvas: def __init__(self, categories): self.categories = categories self._current_category = categories[0] self._window = None
1
绑定到9
(尽可能少),以使每个键更改我类中的当前类别
我的代码:
from tkinter import *
class MyCanvas:
def __init__(self,
categories):
self.categories = categories
self._current_category = categories[0]
self._window = None
self._main()
def _main(self):
master = Tk()
self._window = Canvas(master, width=200, height=100)
self._window.pack()
self._assign_each_category_to_hotkey()
mainloop()
def _assign_each_category_to_hotkey(self):
for i in range(len(self.categories)):
self._window.bind(str(i+1), lambda e, c=self.categories[i]: self._change_category(c))
def _change_category(self, category):
print(category)
self._current_category = category
if __name__ == "__main__":
MyCanvas(categories=['tree', 'monster'])
我希望热键“1”、“2”等可以接受
当我用上面的示例代码分别按1键或2键时,我希望控制台中打印tree
或monster
,但似乎什么也没有发生
当我运行代码时,似乎没有调用\u change\u category
方法。我怎样才能改变这个
你的问题是什么?此代码是否未按预期工作?它是否引发异常?@Rawing我更新了它以询问问题。请创建一个@BryanOakley,我更新了示例代码以说明相同的问题。