Abhishek Bhandari
Abhishek Bhandari

Reputation: 53

Python Entry box takes text input in multiple Entry box

Im making a GUI application where I have 4 Entry section and if I enter text in one Entry box all Entry box are taking data. How do I code such that only on clicking over Entry box it should take input respectively...

Code:

from Tkinter import *

def time():
 t1h=int(Ihr.get())
 t1m=int(Imin.get())
 t2h=int(Ohr.get())
 t2m=int(Omin.get())

app = Tk()
app.title("VM")
app.geometry("150x210")
app.resizable(0,0)    

note = Label(app, text="Clock IN Time :")
note.place(x=10, y=10)
Ihr = Entry(app,text="...")
Ihr.place(x=10, y=30,width="30")
Ihr.focus()
note = Label(app, text="::")
note.place(x=43, y=30)
Imin = Entry(app,text="...")
Imin.place(x=60, y=30,width="30")
note = Label(app, text="(hr)")
note.place(x=12, y=50)
note = Label(app, text="(min)")
note.place(x=58, y=50)  

Upvotes: 0

Views: 1435

Answers (1)

Kevin
Kevin

Reputation: 76194

Ihr = Entry(app,text="...")

 

Imin = Entry(app,text="...")

I suspect the problem is on these two lines, in particular the text keyword arguments. At first glance I don't see the behavior of text documented anywhere, but I'm guessing they behave something like the textvariable argument. Since they both point to the same "..." object, changing one Entry will change the other.

Don't use the text argument to set the text of Entries. Use the .insert method instead.

Ihr = Entry(app)
Ihr.insert(0, "...")

 

Imin = Entry(app)
Imin.insert(0, "...")

Update:

Bryan Oakley confirms that the text and textvariable keyword arguments have the same effect. Generally, you should not pass a string object to these arguments; a StringVar is most conventional. You may be interested in using StringVars here, since you can use their .set method to set the contents of the Entry objects, without having to use the arguably more complicated .insert method.

Ihr_var = StringVar()
Ihr_var.set("...")
Ihr = Entry(app,text=Ihr_var)

 

Imin_var = StringVar()
Imin_var.set("...")
Imin = Entry(app,text=Imin_var)

Upvotes: 4

Related Questions