'How to force focus in text editor written with tkinter?
I wrote a minimal text editor in python with the help of a youtube video.
I want to use it as a distraction-free writing app. The goal is to just boot up a pi and run this script directly. there are a few more features that I have planned, but for this here is my biggest challenge.
The problem right now is, that when I run the script I can't just start typing. I have to click into the text field or press the tab once to be able to write.
I want to open the script and be directly able to start typing. What do I need to add, so that the focus is directly in the text field from the start?
I tried hacking it by forcing a keypress at the beginning of the script, but I couldn't get it to work, and there must be a more elegant way to solve the problem.
Thanks, everyone for taking the time to read my question.
It's a short script, so I just added it in its entirety.
import tkinter as tk
from tkinter import filedialog
class Menubar:
def __init__(self, parent):
font_specs = ("PibotoLt", 10)
menubar = tk.Menu(parent.master, font=font_specs)
menubar.configure(bg='black', fg='white')
parent.master.config(menu=menubar)
file_dropdown = tk.Menu(menubar, font=font_specs, tearoff=0)
file_dropdown.configure(bg='black', fg='white')
file_dropdown.add_command(label="New File",
accelerator="Ctrl+N",
command=parent.new_file)
file_dropdown.add_command(label="Open File",
accelerator="Ctrl+O",
command=parent.open_file)
file_dropdown.add_command(label="Save",
accelerator="Ctrl+S",
command=parent.save)
file_dropdown.add_command(label="Save As",
accelerator="Ctrl+Shift+S",
command=parent.save_as)
file_dropdown.add_separator()
file_dropdown.add_command(label="Exit",
command=parent.master.destroy)
menubar.add_cascade(label="Menu", menu=file_dropdown)
class Statusbar:
def __init__(self, parent):
font_specs = ("URW Gothic", 20, "bold")
self.status = tk.StringVar()
label = tk.Label(parent.textarea, textvariable=self.status, fg="yellow", bg="black", font=font_specs)
label.pack(side=tk.BOTTOM, fill=tk.BOTH)
def update_status(self, *args):
if isinstance(args[0], bool):
self.status.set("Your File Has Been Saved!")
else:
self.status.set("")
class PyWriter:
def __init__(self, master):
master.title("Untitled - Focalscribe")
master.geometry("1200x700")
master.attributes("-fullscreen", True)
font_specs = ("PibotoLt", 14)
self.master = master
self.filename = None
self.textarea = tk.Text(master, font=font_specs)
self.scroll = tk.Scrollbar(master, command=self.textarea.yview)
self.scroll.configure(bg='black')
self.textarea.configure(yscrollcommand=self.scroll.set, bg='black', fg='white', insertbackground='white')
self.textarea.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.scroll.pack(side=tk.RIGHT, fill=tk.Y)
self.menubar = Menubar(self)
self.statusbar = Statusbar(self)
self.bind_shortcuts()
def set_window_title(self, name=None):
if name:
self.master.title(name + " - Focalscribe")
else:
self.master.title("Untitled - Focalscribe")
def new_file(self, *args):
self.textarea.delete(1.0, tk.END)
self.filename = None
self.set_window_title()
def open_file(self, *args):
self.filename = filedialog.askopenfilename(
defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Files", "*.txt*"),
("Markdown Documents", "*.md*"),
("Python Scrips", "*.py*")])
if self.filename:
self.textarea.delete(1.0, tk.END)
with open (self.filename, "r") as f:
self.textarea.insert(1.0, f.read())
self.set_window_title(self.filename)
def save(self, *args):
if self.filename:
try:
textarea_content = self.textarea.get(1.0, tk.END)
with open(self.filename, "w") as f:
f.write(textarea_content)
self.statusbar.update_status(True)
except Exception as e:
print(e)
else:
self.save_as()
def save_as(self, *args):
try:
new_file = filedialog.asksaveasfilename(
initialfile="Untitled.txt",
defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Files", "*.txt*"),
("Markdown Documents", "*.md*"),
("Python Scrips", "*.py*")])
textarea_content = self.textarea.get(1.0, tk.END)
with open(new_file, "w") as f:
f.write(textarea_content)
self.filename = new_file
self.set_window_title(self.filename)
self.statusbar.update_status(True)
except Exception as e:
print(e)
def bind_shortcuts(self):
self.textarea.bind('<Control-n>', self.new_file)
self.textarea.bind('<Control-o>', self.open_file)
self.textarea.bind('<Control-s>', self.save)
self.textarea.bind('<Control-S>', self.save_as)
self.textarea.bind('<Key>', self.statusbar.update_status)
if __name__ == "__main__":
master = tk.Tk()
pw = PyWriter(master)
master.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
