从函数更新 tkinter 中的文本小部件

Update text widget in tkinter from function

问题:

我正在尝试通过包含一些文本的函数更新同一个文本小部件框。相反,每次都会出现一个全新的文本 window。

这是我的代码:

from tkinter import *
import os

#Tkinter graphics

homepage = Tk()
homepage.title("My first GUI")
# set size of window
homepage.geometry('1200x400')
# Add image file 
bg = PhotoImage(file = "maxresdefault.png") 
    
# Show image using label 
label1 = Label( homepage, image = bg) 
label1.place(x = 0, y = 0) 

label2 = Label( homepage, text = "Test App") 
label2.pack() 

# Create Frame 
frame1 = Frame(homepage) 
frame1.pack()

#button initatiors
def buttonupdate():
    S = Scrollbar(homepage)
    T = Text(homepage, height=100, width=30)
    T.pack()
    T.pack(side=RIGHT, fill= Y)
    S.pack(side = RIGHT, fill = Y)
    S.config(command=T.yview)
    T.insert(END, "test")
    T.config(yscrollcommand=S.set, state=DISABLED)


    

# Static buttons
tickets30button = Button(text = "This is button 1", command=buttonupdate) 
tickets30button.place(x=0, y=26) 

mcibutton = Button(text = "This is button 2") 
mcibutton.place(x=0, y=52)

hdebutton = Button(text = "This is button 3")
hdebutton.place(x=0, y=78)

homepage.mainloop()

如果我点击第一个按钮三次,结果如下:

如果您有任何我可以尝试的建议,请告诉我。

感谢您的宝贵时间,

感谢@TheLizzard,我能够更新我的文本 window 而不是创建一个新文本,每次单击按钮。

他提到将创建文本的代码部分 window 移到函数外部,并将创建文本的代码部分保留在函数内部。

之前:

#button initiators
def buttonupdate():
    S = Scrollbar(homepage)
    T = Text(homepage, height=100, width=30)
    T.pack()
    T.pack(side=RIGHT, fill= Y)
    S.pack(side = RIGHT, fill = Y)
    S.config(command=T.yview)
    T.insert(END, "test")
    T.config(yscrollcommand=S.set, state=DISABLED)

之后:(更新)

S = Scrollbar(homepage)
T = Text(homepage, height=100, width=30)
T.pack(side=RIGHT, fill= Y)
S.pack(side = RIGHT, fill = Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set, state=DISABLED)

#button initatiors
def myTicketstatusbutton():
    T.delete(1.0,END)
    T.insert(END, "test")