【发布时间】:2021-06-09 17:55:26
【问题描述】:
问题:
我正在尝试从包含一些文本的函数更新相同的文本小部件框。相反,每次都会出现一个全新的文本窗口。
这是我的代码:
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()
如果我点击第一个按钮三次,结果如下:
如果您有任何我可以尝试的建议,请告诉我。
感谢您的宝贵时间,
【问题讨论】:
-
每次调用函数时都会创建一个新的
Text小部件。将函数的前 5 行移到函数之外,它应该可以工作 -
另外你为什么打电话给
T.pack()然后T.pack(side=RIGHT, fill= Y)?只要有T.pack(side=RIGHT, fill= Y)。 -
@TheLizzard 完全有效。我删除了 T.Pack() 并从函数中取出前五行并将其放在函数上方并且它可以工作!文本小部件将在我现在单击按钮之前出现,但如果它不是每次都创建新窗口,那对我来说很酷。大声笑再次感谢。
-
你可以删除这个问题或者回答它或者@TheLizzard可以回答它。
-
@CoolCloud 在我看来,删除问题并不是一个好习惯。其他人可能有同样的问题,偶然发现了这个问题并找到了解决方案,即使它在 cmets 中。
标签: python function tkinter widget