从Notebook小部件中的ScrolledText获取文本

时间:2019-02-13 13:56:40

标签: python tkinter

下面有一个代码,您可以在其中单击菜单上的按钮来创建选项卡。另外,还有一个打印按钮可以打印所有标签的所有内容。

from tkinter import *
from tkinter.ttk import *
from tkinter.scrolledtext import ScrolledText

root = Tk()


class F(Frame):
    def __init__(self):
    super().__init__()
    root.geometry("500x500")
    self.master.resizable(False,False)


    self.tab = Notebook(root)
    self.tab.grid(row=1, column=33, columnspan=10, rowspan=50, padx=5, pady=5, sticky='NS')

    self.mb = Menu( root )
    root.config( menu=self.mb )
    self.sub_mb = Menu( self.mb, tearoff=0 )
    self.mb.add_command( label='create tab', command = self.create_tab )
    self.mb.add_command( label='print', command=self.print_contents_of_all_tabs )

    def create_tab(self):
        self.new_tab = ScrolledText(height=20, width=50)
        self.tab.add( self.new_tab, text='tab' )

    **# this should print the contents inside all the tabs**
    def print_contents_of_all_tabs(self):
        all_tabs = self.tab.tabs()            # get frame name of all tabs
        for x in range(len(all_tabs)):
            print(self.tab.index(all_tabs[x])) # print the index using frame name
            print(self.new_tab.get(1.0, END))  # This prints only the content of recently created tab

def main():
    F().mainloop()

main()

现在,我想使用def print_contents_of_all_tabs(self)打印所有选项卡的所有内容。我已经获得了所有标签的索引,但是我不知道如何使用每个索引来获取每个标签的文本。另外,如果我使用self.new_tab.get(1.0,END),则仅接收最近创建的选项卡中的内容。

说我有3个标签,第一个标签包含“这是第一个标签”,第二个标签包含“这是第二个标签”,最后第三个标签包含“这是第三个标签”。当我单击打印时,上面代码的输出将是

0   # index of first tab
this is the third tab

1    # index of second tab
this is the third tab

2    # index of third tab
this is the third tab

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

按照post的说明小部件检索:

from tkinter import *
from tkinter.ttk import *
from tkinter.scrolledtext import ScrolledText

root = Tk()


class F(Frame):
    def __init__(self):
        super().__init__()
        root.geometry("500x500")
        self.master.resizable(False,False)


        self.tab = Notebook(root)
        self.tab.grid(row=1, column=33, columnspan=10, rowspan=50, padx=5, pady=5, sticky='NS')

        self.mb = Menu( root )
        root.config( menu=self.mb )
        self.sub_mb = Menu( self.mb, tearoff=0 )
        self.mb.add_command( label='create tab', command = self.create_tab )
        self.mb.add_command( label='print', command=self.print_contents_of_all_tabs )

    def create_tab(self):
        new_tab = ScrolledText(height=20, width=50)
        self.tab.add( new_tab, text='tab' )

    def print_contents_of_all_tabs(self):
        # this should print the contents inside all the tabs**
        all_tabs = self.tab.tabs()            # get frame name of all tabs
        for x in range(len(all_tabs)):
            print(self.tab.index(all_tabs[x])) # print the index using frame name
            frame = self.tab._nametowidget(all_tabs[x])
            scrolled_text = frame.winfo_children()[1]
            print(scrolled_text.get(1.0, END))  # This prints the content of the tab

def main():
    F().mainloop()

main()