In this article let us continue with the previous example by adding a text box widget with a width of 16 at column 0, row 1 position within the Tkinter window, which simply means the second row. This text widget will receive a text from a user and after the user has cliked on the button the string var object will get the text’s entry from the user and replace the old text with the latest one within the label widget.
import tkinter as tk
from tkinter import ttk
win = tk.Tk()
win.title("Hello World!")
def showHello():
#hello.configure(text=txt) # change the text inside the button
hello_label.configure(text=helo.get()) # change the text inside the label
hello = ttk.Button(win, text="Hello!", command=showHello) # create a button
hello.grid(column=0, row=0)
hello_label = ttk.Label(win, text="Hello!") # create a label
hello_label.grid(column=1, row=0)
helo = tk.StringVar() # create the helo string variable object
entry_text = ttk.Entry(win, width=16, textvariable=helo)
entry_text.grid(column=0, row=1)
win.mainloop()
As you can see the program is almost identical to the previous one except the label widget will not get the text from the button but instead will get it directly from the user input through the text box!