在每个选项卡上创建多个文本小部件

问题描述 投票:0回答:1

我需要什么:在每个选项卡上创建文本小部件。

我现在的代码

    for i in range(len(result)):
        tab[i] = ttk.Frame(tabControl)
        tabControl.add(tab[i], text=i)
    tabControl.pack(fill='both', expand=True)
    for i in range(len(result)):
        textwrite[i] = Text(tab[i])
        textwrite[i].pack(fill='both', expand=True)
        scrollbar_fortext[i] = Scrollbar(tab[i], orient='vertical', command=textwrite[i].yview)
        scrollbar_fortext[i].pack(fill='both', expand=True, sticky=NS)  

预期行为:每个选项卡都有一个文本小部件。

收到的行为

NameError: name 'textwrite' is not defined
python tkinter tkinter-text
1个回答
0
投票

如果要声明动态变量,请使用

dict
。 由于您的代码不完整,我将 len(result) 保留为 5。根据您的要求更改代码。 “-sticky”的选项只有 -after、-anchor、-before、-expand、-fill、-in、-ipadx、-ipady、-padx、-pady 或 -side。没有诸如NS之类的选项。所以,我已经删除了它。相应地改变。 代码应该是这样的。

from tkinter import *
from tkinter import ttk

root = Tk()
root.geometry('700x500')

tabControl = ttk.Notebook(root, width=700)
tabControl.pack()

dct = {}
#for testing purpose I've given length of result as 5.Change the code accordingly.
for i in range(5):
    dct[f'tab_{i}'] = ttk.Frame(tabControl)
    tabControl.add(dct[f'tab_{i}'], text = f'{i}')
    dct[f'scrollbar_fortext_{i}'] = Scrollbar(dct[f'tab_{i}'], orient='vertical')
    dct[f'scrollbar_fortext_{i}'].pack(fill='both', expand=True)
    
    
root.mainloop()
        
        
        
        
© www.soinside.com 2019 - 2024. All rights reserved.