How can i change content inside of a window after pressing a button in tkinter?

Viewed 27

I actually found a solution for this but they used an entirely different way of making windows/buttons and i can't figure out how to use it. Please tell me if there's a way to make this happen with my code. The code for my window and its content is as below

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()
1 Answers
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()

Here I've created a new label called my_label and when the play button calls for the play() function, i've used label.config(text="new text") to change text.
You can run the sample code above to get your results.

Related