简体   繁体   English

绑定key到Tkinter中的function

[英]Binding a key to a function in Tkinter

I have a problem with binding a key (Ctrl-A) to a function (select all) in menu_bar function. Whatever I do, this key combination is moving me to beginning of the line.我在将键 (Ctrl-A) 绑定到 menu_bar function 中的 function(全选)时遇到问题。无论我做什么,这个组合键都会让我移动到行首。

My main program:我的主要程序:

from tkinter import *
import tkinter as tk
import func
from tkinter import font

class Editor():
    def __init__(self, window):
        # Window + Title
        window.geometry('1200x800')
        window.title('SuperEditor')
        # Scrool bar
        scrollbar = Scrollbar(window)
        scrollbar.pack(side=RIGHT,fill=Y)
        # Text area
        user_font=font.Font(size=20)
        editor = Text(window,width=400,height=450,yscrollcommand=scrollbar.set, undo = True,
        font=user_font)
        editor.pack(fill=BOTH)
        scrollbar.config(command = editor.yview)

        # Selfs
        self.window = window
        self.editor = editor
        self.user_font = user_font

        editor.bind("<Control-A>", func.select_all(editor))



    def menu_bar(self):
        menubar = Menu(self.window)
        # Edit menu
        editmenu = Menu(menubar, tearoff=0)
        editmenu.add_command(label="Select All", command=lambda: func.select_all(self.editor), accelerator='Ctrl-A')
        menubar.add_cascade(label="Edit", menu=editmenu)

        
        # Add the menu bar
        self.window.config(menu=menubar)




def main():
    # Create a SuperEditor window
    se_window = Tk()
    # Create a text editor
    editor = Editor(se_window)
    # Create menubar
    editor.menu_bar()
    # Rune the SuperEditor
    se_window.mainloop()


if __name__ == "__main__":
    main()

Functions file:函数文件:

import tkinter as tk
import tkinter.filedialog as fd
from tkinter import font


def select_all(text, event=None):
    '''Select all text'''
    if event:
        print('Up')
        text.tag_add("sel", "1.0","end")
        text.tag_config("sel",background="gray",foreground="white")
        return 'break'
    else:
        text.tag_add("sel", "1.0","end")
        text.tag_config("sel",background="gray",foreground="white")
       

When I put this in __init__ , it selects everything at start, but doesn't work later:当我把它放在__init__中时,它会在开始时选择所有内容,但以后不起作用:

self.editor.bind("<Control-A>", functions.select_all(editor))

I tried to do it with lambda, then i get:我试着用 lambda 来做,然后我得到:

TypeError: Editor.menu_bar.<locals>.<lambda>() takes 0 positional arguments but 1 was given

Change改变

editor.bind("<Control-A>", func.select_all(editor))

to

editor.bind("<Control-A>", lambda event: func.select_all(editor, event=event))

This answer was posted as an edit to the question Binding a key to a function in Tkinter by the OP koleks92 under CC BY-SA 4.0.此答案作为对 Tkinter 中的问题Binding a key to a function编辑发布,由 OP koleks92在 CC BY-SA 4.0 下发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM