Python: how to change the text of a message widget to user input

So, I am making a program that requires the user to enter a value. I want the value to be displayed through the message widget (or label widget) and updated whenever a new input is entered.

def Enter(): s = v.get() print (v.get()) e.delete(0, END) e.insert(0, "") #Code, Code, Code ... # Area To Enter Text v = StringVar() e = Entry(root, textvariable=v) e.pack() m = Message(root, text = "Your Input") m.pack() # Enter Button b = Button(root, text="OK", command=Enter) b.pack() 

Is there a way for v to replace Message Widget text ??

Note:

If I replaced text with textvariable , it will update the text after each key is pressed, where I need it to be updated when the user clicks the button.


My full code is:

 from tkinter import * import os # All Functions Below def callback(): print ("HI") def Exit(): os._exit(0) def Enter(): s = e.get() print (e.get()) m.configure(text=s) e.delete(0, END) e.insert(0, "") def Population(): root = Tk root.mainloop() def SurvivalRate(): root = Tk root.mainloop() def BirthRate(): root = Tk root.mainloop() def NewGen(): root = Tk root.mainloop() root = Tk() generation = 0 menubar = Menu(root) menubar.add_command(label="Hello!", command=callback) menubar.add_command(label="Quit!", command=Exit) # Area To Enter Text e = Entry(root) e.pack() m = Message(root, text = e) m.pack() # Enter Button b = Button(root, text="OK", command=Enter) b.pack() Pop = Button(root, text="Population", command=Population) Pop.pack() 
+5
source share
1 answer

Just add:

 m.configure(text=s) 

to your function:

 def Enter(): s = v.get() print (v.get()) m.configure(text=s) e.delete(0, END) e.insert(0, "") 

As a side note, you don't necessarily need StringVar() . The code below will do the same:

 def Enter(): s = e.get() m.configure(text=s) e.delete(0, END) e.insert(0, "") #Code, Code, Code ... # Area To Enter Text e = Entry(root) e.pack() m = Message(root, text = "Your Input") m.pack() # Enter Button b = Button(root, text="OK", command=Enter) b.pack() root.mainloop() 
+4
source

All Articles