-1

I'm trying to generate the output of each Button click to an Entry, each time I press generate button the output shows up in the console, but I want it to show in the GUI so I tried to add the command command=gen to the Entry and it gives me an error, it seems like Entry's don't accept the argument command, but I'm not too sure if it's just a syntax error, is there another way of displaying the output of the Button click on the Gui? The reason why I wanted it to be an Entry is because it provides a perfect rectangular shape with white inside the box which is perfect for generating things.

from Tkinter import * 

def gen():
    print ("testetstesttesttest")

window = Tk()
window.geometry("500x300")
window.title("TestGUI")
botton = Button(window, text="Generate ids",command=gen).place(x=210,y=160)
textbox = Entry(window, command=gen).place(x=210,y=100)
window.mainloop()

The error I receive:

(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-command"
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
Katz
  • 826
  • 3
  • 19
  • 40
  • you may want to look at [this question](http://stackoverflow.com/questions/21592630/why-do-my-tkinter-widgets-get-stored-as-none) since `(textbox is None)-> True` – Tadhg McDonald-Jensen May 08 '16 at 21:48

1 Answers1

1

You want to set the text of the entry directly. As shown in this answer, a StringVar is one way to go about this.

entryText = StringVar(window)

Then, in the your command:

def gen():
    entryText.set("testtesttesttest")

Putting it all together:

from Tkinter import * 

def gen():
    entryText.set("testtesttesttest")

window = Tk()
window.geometry("500x300")
window.title("TestGUI")
entryText = StringVar(window)
botton = Button(window, text="Generate ids",command=gen).place(x=210,y=160)
textbox = Entry(window, textvariable = entryText).place(x=210,y=100)
window.mainloop()
Community
  • 1
  • 1
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
  • 1
    this doesn't produce an error but it doesn't work, every time I click the button nothing happens, I saw that question before I posted my question. – Katz May 08 '16 at 21:42
  • @Francesc I tried this right now – the first time I click the button, the entry is populated with the text. Every click after, though, it just fills it with the same text, so nothing changes. – Rushy Panchal May 08 '16 at 21:45
  • 1
    @Francesc, do you by any chance have more then one Tk instance? you will need to make `entryText` part of the same application with `StringVar(window)` – Tadhg McDonald-Jensen May 08 '16 at 21:50
  • see http://stackoverflow.com/questions/36699249/scale-function-not-outputting-number-to-variable/36699481#36699481 – Tadhg McDonald-Jensen May 08 '16 at 21:53
  • 1
    I wouldn't say the `StringVar` is the _best_ way, just _a_ way. You don't need a `StringVar` since you can insert text directly into the widget. – Bryan Oakley May 08 '16 at 22:44