I am trying to build a text editor using tkinter. i just wanted to set the focus in newly opened tab.By using a static tab_id i can set this for instant but if I have more than 15 tabs at a time it is difficult to find tab_id . i want tab_id with tab_name or tab_title or any other way
Here is my code
import tkinter as tk
from tkinter import ttk, filedialog
import os
root = tk.Tk()
root.title("Tab Widget")
tabControl = ttk.Notebook(root)
def open_file(event=None):
file1 = filedialog.askopenfile(mode='r', initialdir=os.getcwd(), filetypes=(('All files', '*.*'), ('Text files', '*.txt'))).name
with open(file1) as f:
content_in_file = f.read()
new_tab = TabWin(tabControl, f'{file1.rsplit("/", 1)[-1]}').create_tab()
new_tab.delete(1.0, tk.END)
new_tab.insert(1.0, content_in_file)
class TabWin:
def __init__(self, parent, title, text=None, file_path=None):
self.parent = parent
self.tab_title = title
self.text = text
self.tab_id = title
self.tab = tk.Text(parent)
def create_tab(self):
self.parent.add(self.tab, text=self.tab_title)
return self.tab
tab3 = TabWin(tabControl, 'pavan').create_tab()
tabControl.pack(expand=1, fill="both")
root.bind('<Control o>', open_file)
root.mainloop()