按下tkinter中的按钮后,如何更改窗口内的内容



我实际上找到了一个解决方案,但他们使用了一种完全不同的方式来制作窗口/按钮,我不知道如何使用它。请告诉我是否有一种方法可以用我的代码实现这一点。我的窗口及其内容的代码如下

from tkinter import *
title = 'zromber'
window = Tk()
window.geometry("800x400")
def play():
print('welcome')
window.destroy()
def save():
print('yes')
playbutton = Button(window, text='play')
playbutton.config(command=play)
playbutton.config(font=('none', 50, 'bold'))
testlabel = Label(window, text=title)
testlabel.config(font=('Ink Free', 50))
testlabel.pack()
playbutton.pack()
savebutton = Button(window, text='save')
savebutton.config(command=save)
savebutton.config(font=('none', 50, 'bold'))
savebutton.pack()
window.mainloop()
from tkinter import *
title = 'zromber'
window = Tk()
window.geometry("800x400")
my_text = "Hi, I'm a the new label"

def play():
#use label.config(text="new text") to change text
my_label.config(text=my_text+" and I'm from play")

def save():
my_label.config(text=my_text+" and I'm from save")

playbutton = Button(window, text='play')
playbutton.config(command=play)
playbutton.config(font=('none', 50, 'bold'))
testlabel = Label(window, text=title)
testlabel.config(font=('Ink Free', 50))
testlabel.pack()
playbutton.pack()
savebutton = Button(window, text='save')
savebutton.config(command=save)
savebutton.config(font=('none', 50, 'bold'))
savebutton. pack()
#Define the label
my_label = Label(window, text="THIS IS A LABEL")
my_label.config(font=('none', 10, 'bold'))
my_label.pack()
window.mainloop()

在这里,我创建了一个名为my_label的新标签,当播放按钮调用play((函数时,我使用label.config(text="new text"来更改文本
您可以运行上面的示例代码来获得结果。

最新更新