Reputation: 5064
I threw together a very basic text editor in Python using Tkinter, but for whatever reason I can't seem to get my keyboard event bindings to work. I've checked and double-checked the documentation on using .bind() and .bind_all(), but without success. I was hoping that some fresh and definitely more experienced eyes would be able to spot the obvious error that I am sure to be missing. Thanks in advance for any help.
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()
Upvotes: 0
Views: 906
Reputation: 712
A possible cause could be <Command-N>
is interpreted differently from <Command-n>
.
As I don't have a Mac, I changed the Command
to Control
and it generated an event.
If you want to stick with <Command-N>
, then try Command + n key
with Caps ON.
EDIT1 : Another bug with your program is, the callback methods don't correctly receive the event objects. You need to modify the signature of those methods to receive the event object. When tk calls back, it also sends an event object containing details of the event.
Upvotes: 3