如何获取 tab_id 以将其设置为活动选项卡 [英] how to get tab_id to set it as active tab

查看:24
本文介绍了如何获取 tab_id 以将其设置为活动选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tkinter 构建文本编辑器.我只是想在新打开的选项卡中设置焦点.通过使用静态 tab_id 我可以立即设置它,但是如果我一次有超过 15 个选项卡,则很难找到 tab_id .我想要带有 tab_name 或 tab_title 或任何其他方式的 tab_id
这是我的代码

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

推荐答案

你可以通过 Notebook 的内置函数来做到这一点,例如:

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

并注意根据 docs已经有一个 built_in 函数来跟踪您的标签.您可以将 tabControl.select(new_tab) new_tab 行替换为二进制数,因此第一个默认值为 0,下一个为 1,依此类推.

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.

如果您出于任何原因想要以另一种方式进行跟踪,您可以每次将 new_tab 存储在列表或字典中.如果您在这里遗漏了什么,请告诉我.

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.

这篇关于如何获取 tab_id 以将其设置为活动选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆