5

So I'm making a program which requires a user to enter in a value. I want the value to be displayed via the message widget (or label widget) and updates whenever a new input is enter.

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 the variable of v to replace the text of Message Widget??

Note:

If I replace text with textvariable, it updates the text after every character key is pressed, where as I need it to update when the user presses the button.


My complete code:

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()
DavalliTV
  • 65
  • 1
  • 9
  • Take a look at the `command` parameter of a tkinter button. – galah92 Sep 06 '16 at 17:08
  • Thanks! have to teach a few hours, will look at it later tonight :) – Jacob Vlijm Sep 06 '16 at 17:29
  • Ok. But now instead of the `PY_VAR0`, it returns `.56748976`. But I can wait till you get back. Or if anyone else can help, it would be appreciated. – DavalliTV Sep 06 '16 at 17:31
  • 1
    It is actually a new question. The snippet you posted originally is fine now and runs correctly, the text is fetched correctly. – Jacob Vlijm Sep 06 '16 at 17:34
  • ...but by applying what is in the second part of the answer and set your text like: `m = Message(root, text = "Monkey")` fixes it: https://dl.dropboxusercontent.com/u/1155139/fixed.py – Jacob Vlijm Sep 06 '16 at 17:46
  • what a strange code - it doesn't work (without IDLE) because it needs `root.mainloop()` in last line. Program should use only one root window (`Tk`) and only one `mainloop()` - subwindows create with `Toplevel()` and subwindows don't need own mainloop. – furas Sep 06 '16 at 23:06
  • @furas Thanks for the info on my code. I haven't really used `Toplevel()`'s before! And what do you mean when you say subwindows?? Do they still create a new window or do the just refresh the window that is already open? – DavalliTV Sep 07 '16 at 15:29
  • `subwindows` means dialog windows and popup windows - you still have main window opened but you want to show something in second window. `Tk` creates main window and `Toplevel()` creates second/third window. But if you need one window with different content then you can put widgets in `Frame` and many Frames (with different content) you can put in window one abow another (so you see only one Frame - top most). Later you can move other frame above anothers and you have different content in window. In one answer on SO is `Page` system which do something like this. – furas Sep 07 '16 at 17:47
  • see http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter/7557028#7557028 – furas Sep 07 '16 at 17:52

1 Answers1

7

Simply 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 do not necessarily need the StringVar(). The code below will do exactly 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()
Jacob Vlijm
  • 3,099
  • 1
  • 21
  • 37