Python-我想要两个不使用按钮的条目窗口小部件

Python-我想要两个不使用按钮的条目窗口小部件,python,tkinter,tkinter-entry,Python,Tkinter,Tkinter Entry,我不知道该怎么做。我需要两个条目的总和,然后将总和放入另一个没有任何按钮的条目小部件中 例一 from tkinter import * def sum(): a=float(t1.get()) b=float(t2.get()) c=a+b t3.insert(0,c) win=Tk() win.geometry('850x450') l1=Label(win,text="First Number") l1.grid(row=0,column=0) t1=Entry(wi

我不知道该怎么做。我需要两个条目的总和,然后将总和放入另一个没有任何按钮的条目小部件中

例一

from tkinter import *
def sum():
a=float(t1.get())
b=float(t2.get())
c=a+b
t3.insert(0,c)
win=Tk()
win.geometry('850x450')

l1=Label(win,text="First Number")
l1.grid(row=0,column=0)
t1=Entry(win)
t1.grid(row=0,column=1)

l2=Label(win,text="Second Number")
l2.grid(row=1,column=0)
t2=Entry(win)
t2.grid(row=1,column=1)

l3=Label(win,text="Result")
l3.grid(row=2,column=0)
t3=Entry(win)
t3.grid(row=2,column=1)

b1=Button(win,text="Click For SUM",command=sum)
b1.grid(row=3,column=1)

win.mainloop()
我希望任何人都能处理好这件事


提前感谢。

如果没有任何按钮,您可能需要使用
bind
。所以,试着在代码末尾说这句话

t2.bind('<Return>',sum)
现在,您可以删除按钮,当您在第二个输入小部件中按Enter键时,它将调用
sum()
,然后将输出插入第三个输入小部件

额外提示:

  • 我建议将函数名从
    sum
    更改为其他名称,因为sum是一个内置python函数
  • 您还可以添加一个额外的
    bind
    例如,
    t1.bind('Return',lambda事件:t2.focus_force())
    这样,当用户在第一个条目中按enter键时,他们将移动到下一个条目小部件(与tab键相同)
希望它有帮助,如果有任何疑问,一定要让我知道


干杯

您可以运行一个函数,每隔一秒钟定期将第三个条目的值重置为第一个和第二个条目的值之和,如下所示:

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((int(e1_tk.get().replace(' ', '')) + int(e2_tk.get().replace(' ', ''))))
    except :
        pass
    
    root.after(1000, update_sum) # reschedule the event
    return

root = tk.Tk()

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)
e2 = tk.Entry(root, textvariable = e2_tk)
e3 = tk.Entry(root, textvariable = sum_tk)

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

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

您可以根据自己的意愿调整更新之间的延迟。

如果您能将我的问题标记为答案,则将不胜感激,前提是它有帮助。
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((int(e1_tk.get().replace(' ', '')) + int(e2_tk.get().replace(' ', ''))))
    except :
        pass
    
    root.after(1000, update_sum) # reschedule the event
    return

root = tk.Tk()

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)
e2 = tk.Entry(root, textvariable = e2_tk)
e3 = tk.Entry(root, textvariable = sum_tk)

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

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