0

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)  
  • [Why is having identical str(object)s as textvariable option to widgets make their texts synchronized?](https://stackoverflow.com/q/48352341/7032856) – Nae Feb 14 '18 at 00:36

1 Answers1

4
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)
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Thanks a lot. Got what was needed. – Abhishek Bhandari Feb 12 '18 at 14:17
  • @BryanOakley, I think we've had this conversation before :-D but I couldn't remember what our conclusion was the last time, hence my equivocal tone above. – Kevin Feb 12 '18 at 15:19
  • 1
    All tkinter options can be specified with an abbreviation, as long as it's unambiguous. For example, `back` instead of `background`, `wi` instead of `width`, etc. You can't use `b` for `borderwidth` since it's ambiguous (tkinter can't tell if you meant `borderwidth` or `background`). – Bryan Oakley Feb 12 '18 at 15:33