tkinter键绑定错误

时间:2016-04-28 21:53:24

标签: python-3.x tkinter

我正在尝试为tkinter下拉菜单创建一些键盘快捷键,例如,'Ctrl + O'打开文件浏览器以打开新文件。我可以使用单个键成功绑定self.master.bind('<Control_L>', func)self.master.bind('<O>', func)但是当我尝试将它们组合在一起时,我得到:

文件“C:\ Users \ kelly \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ tkinter__init __。py”,第1099行,绑定     return self._bind(('bind',self._w),sequence,func,add)   文件“C:\ Users \ kelly \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ tkinter__init __。py”,第1054行,在_bind中     self.tk.call(什么+(序列,cmd)) _tkinter.TclError:绑定详细信息后的额外字符

此代码产生我的错误:

from tkinter import *


class Application():

    def __init__(self, master=None):
        '''Create a 'master' frame of 1 row x 1 column'''
        self.master = master
        button = Button(self.master, text = 'Button', command = self.test)
        button.pack()
        #This Binding works
        self.master.bind('<Return>', self.test)
        #this Binding produces Error
        self.master.bind('<Control_L-o>', self.test)


    def test(self, event = None):
        print("Succesful Event")

root = Tk()
app = Application(master=root)

1 个答案:

答案 0 :(得分:2)

这只是一个语法错误。将将CTRL + O绑定到self.master.bind('<Control_L><o>', self.test)

的行更改

您使用该单行更改了代码:

class Application():

    def __init__(self, master=None):
        '''Create a 'master' frame of 1 row x 1 column'''
        self.master = master
        button = Button(self.master, text = 'Button', command = self.test)
        button.pack()
        #This Binding works
        self.master.bind('<Return>', self.test)
        #this Binding produces Error
        self.master.bind('<Control_L><o>', self.test)


    def test(self, event = None):
        print("Succesful Event")

root = Tk()
app = Application(master=root)