spideyonthego
spideyonthego

Reputation: 123

How to insert text onto an entry box when a button is clicked

Can anyone help me?

from Tkinter import *
import easygui

def whenoneclick():
    #How do I insert "1" into the Entry Label (inputbox)?


def mainfunc():
    inputvalue = inputvar.get()
    outputvalue = eval(inputvalue)

easygui.msgbox("The answer is: " + str(outputvalue), title="Answer")



maingui = Tk()


inputvar = StringVar()

inputbox = Entry(maingui, textvariable=inputvar).place(x=10,y=10)
inputbut = Button(text="=",command=mainfunc).place(x=10,y=40)

button = Button(text="1",command=whenoneclick).pack()

Please reply.

Upvotes: 0

Views: 6663

Answers (2)

user2133443
user2133443

Reputation: 171

If you use a StringVar as per your code then you set the value

def whenoneclick():
    inputvar.set("1")  ## "1" not 1 because it is a StringVar

Note that you redefine here but it is local to the mainfunc function

def mainfunc():
    inputvalue = inputvar.get()

Also there is no mainloop in your program and "outputvalue" is local to the function so you will get an error on the easygui.msgbox, or indent the line so it is part of the function.

Upvotes: 0

user2555451
user2555451

Reputation:

Well, to start, you need to make inputbox refer to the entrybox itself, not the return value of the place method (which is None). This can be done by making the code like so:

inputbox = Entry(maingui, textvariable=inputvar)
inputbox.place(x=10,y=10)

Once this is done, you can use the entrybox's insert method. I wrote a simple script to demonstrate:

from Tkinter import *

root = Tk()

def click():
    inputbox.insert(0, "1")

inputbox = Entry()
inputbox.place(x=10,y=10)

Button(text="Click",command=click).place(x=10,y=40)

root.mainloop()

If you want to delete text, you can use the delete method:

inputbox.delete(0, END)

Upvotes: 2

Related Questions