我尝试使用键盘事件绑定从askquestion
调用tkSimpleDialog
方法,但它始终会导致窗口在MacOSX中挂起。
如果我从Find
中选择Menu
选项,它可以顺利运行,但每当我尝试使用键盘事件调用它时,窗口就会挂起。
问题出在edit_menu
部分和find_command
函数的最后一个绑定事件中。
有谁知道这是怎么发生的?
提前感谢您的帮助。
import Tkinter as tk
import ScrolledText
import tkFont
import tkFileDialog
import tkSimpleDialog
from ScrolledText import *
from Tkinter import *
import tkMessageBox
from tkSimpleDialog import askstring
#A very simple text editor
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= "Text Perfect", 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.parent.bind_all("<Command-w>", self.exit_program)
self.parent.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.parent.bind_all("<Command-n>", self.new_command)
self.parent.bind_all("<Command-N>", self.new_command)
self.file_menu.add_separator()
self.file_menu.add_command(label="Open", command=self.open_command, accelerator="Cmd+O")
self.parent.bind_all("<Command-o>", self.open_command)
self.parent.bind_all("<Command-O>", self.open_command)
self.file_menu.add_command(label="Save", command=self.save_command, accelerator="Cmd+S")
self.parent.bind_all("<Command-s>", self.save_command)
self.parent.bind_all("<Command-S>", self.save_command)
self.file_menu.add_command(label="Save As...", command=self.saveAs_command, accelerator="Cmd+Shift+S")
self.parent.bind_all("<Command-Shift-s>", self.saveAs_command)
self.parent.bind_all("<Command-Shift-S>", self.saveAs_command)
# 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.cut_command, accelerator="Cmd+X")
self.parent.bind_all("<Command-Shift-x>", self.cut_command)
self.parent.bind_all("<Command-Shift-X>", self.cut_command)
self.edit_menu.add_command(label = "Copy", command = self.copy_command, accelerator="Cmd+C")
self.parent.bind_all("<Command-Shift-c>", self.copy_command)
self.parent.bind_all("<Command-Shift-C>", self.copy_command)
self.edit_menu.add_command(label = "Paste", command = self.paste_command, accelerator="Cmd+V")
self.parent.bind_all("<Command-Shift-v>", self.paste_command)
self.parent.bind_all("<Command-Shift-V>", self.paste_command)
self.edit_menu.add_separator()
self.edit_menu.add_command(label= "Find", command= self.find_command, accelerator="Cmd+F")
self.parent.bind_all("<Command-f>", self.find_command) #<-------HERE------
self.parent.bind_all("<Command-F>", self.find_command)
parent.config(menu=self.menuBar)
##################################################
def open_command(self, event=None):
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, event=None):
file = tkFileDialog.asksaveasfile(mode= 'w')
if file != None:
data = self.textWidget.get("1.0", END+'-1c') #strip trailing \n at EOF
file.write(data)
file.close()
def saveAs_command(self, event=None):
file = tkFileDialog.asksaveasfile(mode= 'w')
if file != None:
data = self.textWidget.get("1.0", END+'-1c') #strip trailing \n at EOF
file.write(data)
file.close()
def exit_program(self, event=None):
if tkMessageBox.askokcancel("Quit", "Are you sure you want to quit?"):
self.parent.destroy()
def new_command(self, event=None):
print "new"
def cut_command(self, event=None):
text = self.textWidget.get(SEL_FIRST, SEL_LAST)
self.textWidget.delete(SEL_FIRST, SEL_LAST)
self.textWidget.clipboard_clear()
self.textWidget.clipboard_append(text)
def copy_command(self, event=None):
text = self.textWidget.get(SEL_FIRST, SEL_LAST)
self.textWidget.clipboard_clear()
self.textWidget.clipboard_append(text)
def paste_command(self, event=None):
try:
text = self.textWidget.selection_get(selection= 'CLIPBOARD')
self.textWidget.insert(INSERT, text)
except TclError:
pass
def find_command(self, event=None):
target = askstring('SimpleEditor', 'Search String?') #<----AND HERE-----
if target:
where = self.textWidget.search(target, INSERT, END)
if where:
print where
pastit = where + ('+%dc' % len(target))
self.textWidget.tag_add(SEL, where, pastit)
self.textWidget.mark_set(INSERT, pastit)
self.textWidget.see(INSERT)
self.textWidget.focus()
" def about_command(self):
label = tkMessageBox.showinfo("About", "A super duper simple text editor. It's now available through github, thanks to Jason!")
#dummy function as a place holder while constructing further functionality
def dummy_funct(self, event=None):
print"The life of a function is often very lonely..."
if __name__ == "__main__":
root = tk.Tk()
textApp = SimpleTextEditor(root)
root.mainloop()