繁体   English   中英

从 function 更新 tkinter 中的文本小部件

[英]Update text widget in tkinter from function

问题:

我正在尝试从包含一些文本的 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 的代码部分移到 function 之外,并将创建文本的代码部分保留在 function 内。

前:

#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")
    

暂无
暂无

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

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