Python - 我想要两个入口小部件 Sum 而不使用按钮 [英] Python - I want two entry widgets Sum without using button
本文介绍了Python - 我想要两个入口小部件 Sum 而不使用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白如何做到这一点.我需要对两个条目求和,然后将总和放入另一个没有任何按钮的条目小部件中.
I do not understand how to do this. I need to sum of two entries and then put the sum into another entry widget without any Buttons.
示例一
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()
我希望任何人都能处理这个..
I hope anyone can handle this..
提前致谢..
推荐答案
您可以运行一个函数,每隔一秒定期将第三个条目的值重置为第一个和第二个条目的总和,如下所示:
You can run a function that regularly every one second resets the third entry's value to the sum of the first and the second like so-:
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()
您可以根据需要调整更新之间的延迟.
You can adjust the delay between updates as you wish.
这篇关于Python - 我想要两个入口小部件 Sum 而不使用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文