Python 从函数更新tkinter中的文本小部件
问题:Python 从函数更新tkinter中的文本小部件,python,function,tkinter,widget,Python,Function,Tkinter,Widget,问题: 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 i
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()
我试图从包含一些文本的函数中更新相同的文本小部件框。相反,每次都会出现一个全新的文本窗口
这是我的代码:
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 他提到将创建文本窗口的代码部分移到函数外部,并将创建文本的代码部分保留在函数内部 之前:
#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)
之后:(更新)
每次调用函数时,您都会创建一个新的
Text
小部件。将函数的前5行移到函数外部,它也应该可以工作。为什么要调用T.pack()
,然后调用T.pack(side=RIGHT,fill=Y)
?只需使用完全有效的T.pack(side=RIGHT,fill=Y)
@TheLizzard即可。我删除了T.Pack()并从函数中取出前五行,将其放在函数上方,它就可以工作了!文本小部件将出现在我现在点击按钮之前,但如果它没有每次都创建新窗口,这对我来说很酷。哈哈,再次感谢。你可以删除这个问题或回答它,或者@TheLizzard可以回答它。@CoolCloud在我看来,删除这个问题不是一个好的做法。其他人可能也有同样的问题,偶然发现了这个问题,并找到了解决方案,即使是在注释中。此外,您可以将S.config(command=T.yview)
和T.config(yscrollcommand=S.set,state=DISABLED)
移动到您的函数之外,使其更快。还有T.pack\u forget()
没有做任何事情,因此您可以删除它(它对实际代码没有影响)啊,是的,我忘记删除t.pack\u forget()
。谢谢你接电话。我还修改了答案以反映新的信息。再次感谢!