0

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.

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:

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

I tried to do it with lambda, then i get:

TypeError: Editor.menu_bar.<locals>.<lambda>() takes 0 positional arguments but 1 was given
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
koleks92
  • 1
  • 1
  • Can't reproduce the problem with the code you have provided. – figbeam Jan 05 '23 at 12:06
  • Please [edit] to provide the full traceback. See also the [help] and in particular [How to ask](/help/how-to-ask) as well as the guidance for providing a [mre]. – tripleee Jan 05 '23 at 12:07
  • Thanks for the edit. We'd still like to see the full traceback revealed. – tripleee Jan 05 '23 at 13:03
  • 1
    Change `editor.bind("", func.select_all(editor))` to `editor.bind("", lambda event: func.select_all(editor, event=event))` – TheLizzard Jan 05 '23 at 13:22
  • After chaning to 'editor.bind("", lambda event: func.select_all(editor, event=event))', it's not selecting at the start, but Ctrl+A still moves the cursor to the beggining – koleks92 Jan 05 '23 at 13:27
  • @koleks92 Does it call the `select_all` function? – TheLizzard Jan 05 '23 at 13:39
  • No, if i checked correctly( i added print statement to the function), it does not call select_all function. – koleks92 Jan 05 '23 at 13:56
  • It works, took me some time to realise that it's capital A in the code, thanks for help – koleks92 Jan 05 '23 at 16:37
  • Does this answer your question? [Why is my Button's command executed immediately when I create the Button, and not when I click it?](https://stackoverflow.com/questions/5767228/why-is-my-buttons-command-executed-immediately-when-i-create-the-button-and-no) – Delrius Euphoria Jan 26 '23 at 09:00

1 Answers1

0

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81