python-entry widget 位置更改不会发生并且标签创建问题

代码如下所示,但未重新定位条目小部件位置,也未创建标签。在输入此代码之前 [ 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()

提前致谢..


湖上湖
浏览 80回答 1
1回答

慕少森

您对 2 个小部件使用相同的变量并在其上使用网格函数Grid 和 pack 是两个内置布局管理器,包括 place我们只能在单个元素上使用其中之一在您的程序中,您正在打包e1、e2、e3并尝试为它们提供网格布局。此外,您还使用了e1.grid()两次不同的列值。try :    import tkinter as tk # Python 3except :    import Tkinter as tk # Python 2def 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    returnroot = 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.# Entriese1 = 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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python