Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 自动热键绑定键“;1“;至;9“;重新分配属性值的步骤_Python_Lambda_Tkinter - Fatal编程技术网

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,我更新了示例代码以说明相同的问题。