简体   繁体   English

如何在 tkinter ttk 笔记本的特定位置插入选项卡?

[英]How do you insert a tab in a specific place in a tkinter ttk notebook?

I would like to know how you would set a tkinter notebook tab in a specific place.我想知道如何在特定位置设置 tkinter 笔记本标签。 For example, I have this tab that allows you to clone other tabs, but I want it to stay at the end.例如,我有一个允许您克隆其他选项卡的选项卡,但我希望它保留在最后。 The code I tried does not seem to work though, as it generates an tkinter.TclError: "Slave index (whatever number) out of bounds".我尝试的代码似乎不起作用,因为它会生成 tkinter.TclError: "Slave index (whatever number) out of bounds"。

` `

from tkinter import *\
from tkinter import ttk, messagebox as msg\
from widgets.translate import Translator\
from widgets.explore import FileExplorer\

root = Tk()\
root.title('Widgets')

tabs = ttk.Notebook(root, width=900, height=350)
tabs.pack(fill='both', expand=1, pady=(5, 0))

tr = ttk.Frame(tabs)
tr_frame = ttk.Frame(tr)
tr_frame.pack(fill=Y)
tabs.add(tr, text='Translator')
    
Translator(tr_frame)

explorers=0
translators=0

def fileexplore_tab(index=2):
    global explorers

    fl_explore = ttk.Frame(tabs)
    tabs.insert(index, fl_explore, text='File Explorer')
    FileExplorer(fl_explore)
    explorers += 1

fileexplore_tab(2)
fileexplore_tab(len(tabs.tabs())-1)

print(len(tabs.tabs()))

root.mainloop()

` `

How do you insert a tab in a specific place in a tkinter ttk notebook?如何在 tkinter ttk 笔记本的特定位置插入选项卡?

The insert method allows you to specify an integer id. insert方法允许您指定一个 integer id。 For example, the following code creates four tabs, then inserts a fifth in the middle.例如,以下代码创建四个选项卡,然后在中间插入第五个选项卡。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True)

for color in ("red", "orange", "green", "blue"):
    frame = tk.Frame(notebook, background=color)
    notebook.add(frame, text=color)

extra_frame = tk.Frame(notebook, background="white")
notebook.insert(2, extra_frame, text="white")

root.mainloop()

The first argument to insert is a numerical index. insert的第一个参数是数字索引。 Tabs are numbered starting with zero, so the position 0 will insert the tab as the first tab.选项卡从零开始编号,因此 position 0将插入选项卡作为第一个选项卡。 You can use the string "end" to insert the tab at the end.您可以使用字符串"end"在末尾插入制表符。

last_frame = tk.Frame(notebook, background="black")
notebook.insert("end", last_frame, text="black")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM