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