python-条目小部件位置更改不会发生并且标签创建问题

python-entry widget Place change is not going to happen and Label creating problem

下面显示了代码,但没有重新定位条目小部件的位置,也没有创建标签。在输入此代码之前 [ e1.grid(row=1,column=1) ] 程序运行良好,输入代码后不起作用,如何处理这个问题..

节目是

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()

提前致谢..

您正在为 2 个小部件使用相同的变量并在其上使用网格函数

Grid 和 pack 是 build-in 布局管理器中的两个,包括 place

我们只能在单个元素上使用其中一个

在您的程序中,您正在打包 e1e2e3 并尝试为它们提供网格布局。

您还用不同的列值使用了两次 e1.grid()

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()