Tkinter键盘绑定不起作用?

时间:2014-08-08 12:39:23

标签: python tkinter

我使用Tkinter在Python中汇总了一个非常基本的文本编辑器,但无论出于何种原因,我似乎无法使我的键盘事件绑定工作。我已经检查并仔细检查了使用.bind()和.bind_all()的文档,但没有成功。我希望一些新鲜且经验更丰富的眼睛能够发现我肯定会错过的明显错误。在此先感谢您的帮助。

import Tkinter as tk
import ScrolledText
import tkFont
import tkFileDialog
import tkSimpleDialog
from ScrolledText import *
from Tkinter import *
import tkMessageBox
from tkSimpleDialog import askstring


class SimpleTextEditor:

def __init__(self, parent):
    self.parent = parent
    self.parent.title("TextPerfect")
    self.textWidget = ScrolledText(parent, width=80, height=50, font=(tkFont.Font(family= "Consolas", size= "12")))
    self.textWidget.pack()

    self.menuBar = tk.Menu(parent, tearoff=0)
    # About Menu
    self.about_menu = tk.Menu(self.menuBar, tearoff= 0)
    self.menuBar.add_cascade(label= "TextPerfect", menu= self.about_menu)
    self.about_menu.add_command(label= "About", command= self.about_command)
    self.about_menu.add_command(label= "Quit", command= self.exit_program, accelerator="Cmd+W")
    self.about_menu.bind_all("<Command-W>", self.exit_program)

    # File Menu
    self.file_menu = tk.Menu(self.menuBar, tearoff = 0)
    self.menuBar.add_cascade(label = "File", menu=self.file_menu)
    self.file_menu.add_command(label="New", command=self.new_command, accelerator="Cmd+N")
    self.about_menu.bind_all("<Command-N>", self.dummy_funct)
    self.file_menu.add_separator()
    self.file_menu.add_command(label="Open", command=self.open_command, accelerator="Cmd+O")
    self.file_menu.add_command(label="Save", command=self.save_command, accelerator="Cmd+S")
    self.file_menu.add_command(label="Save As...", command=self.saveAs_command, accelerator="Cmd+Shift+S")

    # Edit Menu
    self.edit_menu = tk.Menu(self.menuBar, tearoff=0)
    self.menuBar.add_cascade(label= "Edit", menu= self.edit_menu)
    self.edit_menu.add_command(label = "Cut", command = self.dummy_funct, accelerator="Cmd+X")
    self.edit_menu.add_command(label = "Copy", command = self.dummy_funct, accelerator="Cmd+C")
    self.menuBar.add_cascade(label = "Paste", menu=self.dummy_funct, accelerator="Cmd+V")

    parent.config(menu=self.menuBar)

##################################################

def open_command(self):
    file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Select a file')
    if file != None:
        contents = file.read()
        self.textWidget.insert("1.0",contents)
        file.close()

def save_command(self):
    file = tkFileDialog.asksaveasfile(mode= 'w')
    if file != None:
        data = self.textWidget.get("1.0", END+'-1c')   #remove EOF new line char. from get
        file.write(data)
        file.close()

def saveAs_command(self):
    file = tkFileDialog.asksaveasfile(mode= 'w')
    if file != None:
        data = self.textWidget.get("1.0", END+'-1c')   #remove EOF new line char. from get
        file.write(data)
        file.close()

def exit_program(self):
    if tkMessageBox.askokcancel("Quit", "Are you sure you want to quit?"):
        self.parent.destroy()

def new_command(self):
    print "hi"

def about_command(self):
    label = tkMessageBox.showinfo("About", "A super duper simple text editor.")                


#dummy function as a place holder while constructing further functionality        
def dummy_funct(self):
    print"The life of a function is often very lonely..."

if __name__ == "__main__":
    root = tk.Tk()
    textApp = SimpleTextEditor(root)
    root.mainloop()

1 个答案:

答案 0 :(得分:3)

<Command-N>的可能原因可能与<Command-n>的解释不同。 由于我没有Mac,因此我将Command更改为Control并生成了一个事件。

如果您想坚持<Command-N>,请尝试使用Caps ON Command + n key

EDIT1:你的程序的另一个错误是,回调方法没有正确接收事件对象。您需要修改这些方法的签名才能接收事件对象。 当tk回调时,它还会发送一个包含事件详细信息的事件对象。