how to get tab_id to set it as active tab

≡放荡痞女 提交于 2021-02-05 08:56:09

问题


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()


回答1:


You can do this by the built_in functions of the Notebook like:

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)
    tabControl.select(new_tab)

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()

and note that according to the docs there is already a built_in function to keep track of your tabs. you could just replace in the line of tabControl.select(new_tab) new_tab with the binary number, so the very first default would be 0 and the next would be 1 and so on.

If you want for any reason to keep track in another way you could store new_tab each time in a list or dictonary. Let me know if you are missing something here.



来源:https://stackoverflow.com/questions/62978589/how-to-get-tab-id-to-set-it-as-active-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!