为了账号安全,请及时绑定邮箱和手机立即绑定

python 另一个标签和条目小部件通过计算创建

python 另一个标签和条目小部件通过计算创建

幕布斯6054654 2023-07-18 15:30:04
代码如下所示创建一个标签和三个条目小部件,我想使用相同的计算创建另一个标签和三个条目小部件。我正在尝试添加标签和条目小部件,但不起作用并出现语法错误。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 :        passroot.after(10, update_sum) # reschedule the eventreturnroot = 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()我想创建另一个标签,如“SL”和带有计算事件的条目小部件
查看完整描述

1 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

您可以将参数传递给函数并使用不同的变量调用它:


import tkinter as tk



def update_sum(first_number_tk, second_number_tk, sum_tk) :

# Sets the sum of values of e1 and e2 as val of e3

    try:

        sum_tk.set((float(first_number_tk.get().replace(' ', '')) + float(second_number_tk.get().replace(' ', ''))))

    except :

        pass


    root.after(10, update_sum, first_number_tk, second_number_tk, sum_tk) # 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)


e3_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e1's val.

e4_tk = tk.StringVar(root) # Initializes a text variable of tk to use to get e2's val.

sum2_tk = tk.StringVar(root) # Initializes a text variable of tk to use to set e3's val.



# Entries

e5 = tk.Entry(root, textvariable = e3_tk)

e5.grid(row=2,column=1)

e6 = tk.Entry(root, textvariable = e4_tk)

e6.grid(row=2,column=2)

e7 = tk.Entry(root, textvariable = sum2_tk)

e7.grid(row=2,column=3)


e8=tk.Label(root,text="ML")

e8.grid(row=2,column=0)



# Will update the sum every second 10 ms = 0.01 second it takes ms as arg.

root.after(10, update_sum, e1_tk, e2_tk, sum_tk)

root.after(10, update_sum, e3_tk, e4_tk, sum2_tk)


root.mainloop()


查看完整回答
反对 回复 2023-07-18
  • 1 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信