簡體   English   中英

Tkinter鍵盤綁定不起作用?

[英]Tkinter keyboard bindings not working?

我使用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()

可能的原因可能是<Command-N><Command-n>解釋不同。 由於沒有Mac,我將Command更改為Control ,並生成了一個事件。

如果要堅持使用<Command-N> ,請嘗試在打開Caps的情況下按Command + n key

EDIT1:程序的另一個錯誤是,回調方法不能正確接收事件對象。 您需要修改這些方法的簽名以接收事件對象。 當tk回叫時,它還會發送一個包含事件詳細信息的事件對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM