1

the code is presented below but not relocated entry widget place, label is also not created. before entered this code [ e1.grid(row=1,column=1) ] the program working perfectly, after entering the code not working, how to handle this problem..

the program is

try :
import tkinter as tk # Python 3
except :
import Tkinter as tk # Python 2

def update_sum() :
# Sets the sum of values of e1 and e2 as val of e3
try :
    sum_tk.set((float(e1_tk.get().replace(' ', '')) + float(e2_tk.get().replace(' ', ''))))
except :
    pass

root.after(10, update_sum) # reschedule the event
return

root = tk.Tk()
root.geometry('850x450')

e1_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e1's val.
e2_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e2's val.
sum_tk = tk.StringVar(root) # Initializes a text variable of tk to use to set e3's val.

# Entries
e1 = tk.Entry(root, textvariable = e1_tk)
e1.grid(row=1,column=1)
e2 = tk.Entry(root, textvariable = e2_tk)
e2.grid(row=1,column=2)
e3 = tk.Entry(root, textvariable = sum_tk)
e3.grid(row=1,column=3)

e1=Label(root,text="SL")
e1.grid(row=1,column=0)

e1.pack()
e2.pack()
e3.pack()

# Will update the sum every second 10 ms = 0.01 second it takes ms as arg.
root.after(10, update_sum)
root.mainloop()

Thanks in Advance..

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

0

You are using the same variable for 2 widgets and using the grid function on it

Grid and pack are 2 of the build-in layout managers including the place

We can only use one of these on a single element

In your program you are packing e1,e2,e3 and trying to give them a grid layout.

Also you are using e1.grid() twice with different column values .

try :
    import tkinter as tk # Python 3
except :
    import Tkinter as tk # Python 2

def update_sum() :
# Sets the sum of values of e1 and e2 as val of e3
    try :
        sum_tk.set((float(e1_tk.get().replace(' ', '')) + float(e2_tk.get().replace(' ', ''))))
    except :
        pass

    root.after(10, update_sum) # reschedule the event
    return

root = tk.Tk()
root.geometry('850x450')

e1_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e1's val.
e2_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e2's val.
sum_tk = tk.StringVar(root) # Initializes a text variable of tk to use to set e3's val.

# Entries
e1 = tk.Entry(root, textvariable = e1_tk)
e1.grid(row=1,column=1)
e2 = tk.Entry(root, textvariable = e2_tk)
e2.grid(row=1,column=2)
e3 = tk.Entry(root, textvariable = sum_tk)
e3.grid(row=1,column=3)

e4=tk.Label(root,text="SL")
e4.grid(row=1,column=0)



# Will update the sum every second 10 ms = 0.01 second it takes ms as arg.
root.after(10, update_sum)
root.mainloop()
Sandrin Joy
  • 1,120
  • 10
  • 28