简体   繁体   中英

Tkinter: How to insert text into text widget that's part of a Frame?

I need help inserting text in a Text widget that's part of a Frame .

class Test_frame(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.config(width = 700, height = 450)
        self.logs_frame = LabelFrame(self, height = 402, width = 348, text = 'Logs')
        self.logs_frame.grid(column = 1, row = 0, pady=10, sticky = N)
        self.logs_frame.grid_propagate(0)

        self.text_box = Text(self.logs_frame, width=40, pady=10, height=22)
        self.text_box.pack(side="left")

        self.scroll_y = Scrollbar(self.logs_frame, orient="vertical", command=self.text_box.yview)
        self.scroll_y.pack(side="left", expand=True, fill="y")
        self.text_box.configure(yscrollcommand=self.scroll_y.set)

        self.text_box.insert (?????.END, "Sample Text")

I have no idea how to access that Text widget now.

Test_frame is part of a Notebook :

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.my_notebook = ttk.Notebook(self)
        self.my_notebook.pack(pady = 5)
        self.frames = {}
        for F in (Test_frame, Final_flash_frame):
            frame = F()
            self.frames[F] = frame
            frame.pack(fill = 'both', expand = 1)
        self.my_notebook.add(self.frames[Test_frame], text = "Testing")

I created a minimal source, but I guess you solved your problem.

I added a little button to add text, and added some text from outside the class.

Here it is, anyway:

import tkinter as Tk
import tkinter.ttk as ttk
from tkinter import *

def BumpText(textbox):
    textbox.insert(END,", MORE TEXT")


class Test_frame(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.config(width = 700, height = 450)
        self.logs_frame = LabelFrame(self, height = 402, width = 348, text = 'Logs')
        self.logs_frame.grid(column = 1, row = 0, pady=10, sticky = N)
        self.logs_frame.grid_propagate(0)

        self.text_box = Text(self.logs_frame, width=40, pady=10, height=22)
        self.text_box.pack(side="left")

        self.scroll_y = Scrollbar(self.logs_frame, orient="vertical", command=self.text_box.yview)
        self.scroll_y.pack(side="left", expand=True, fill="y")
        self.text_box.configure(yscrollcommand=self.scroll_y.set)

        self.text_box.insert(END, "Sample Text")

        self.button = Button(self.logs_frame, text="Add\nText", command=lambda:self.text_box.insert(END, ", More Text"))
        self.button.pack(side="bottom")

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.my_notebook = ttk.Notebook(self)
        self.my_notebook.pack(pady = 5)
        self.frames = {}
        for F in (Test_frame,):  # , Final_flash_frame):
            frame = F()
            self.frames[F] = frame
            frame.pack(fill = 'both', expand = 1)
        self.my_notebook.add(self.frames[Test_frame], text = "Testing")

        for F,f in self.frames.items():
            try:
                f.text_box.insert(END," (Added by App)")
            except Exception as e:
                pass  # ignore frames without text_box 


if __name__ == "__main__":
    App().mainloop()

Ok, so apparently just using:

self.text_box.insert (END, "Sample Text")

works

Cheers, A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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