我有两个python脚本mainapp.py和calculate.py
from tkinter import * import calculations root = Tk() mainapp = Frame(root) mainapp.grid() label1 = Label(mainapp, text="Value A") label1.grid(row=0, column=0) value_a = IntVar() entry1 = Entry(mainapp, textvariable=value_a) entry1.grid(row=0, column=1) label2 = Label(mainapp, text="Value B") label2.grid(row=1, column=0) value_b = IntVar() entry2 = Entry(mainapp, textvariable=value_b) entry2.grid(row=1, column=1) button = Button(mainapp, text="Calculate", command=calculations.addvalues(a=value_a.get(), b=value_b.get())) button.grid(row=2, columnspan=2) label3 = Label(mainapp, text="A+B") label3.grid(row=3, column=0) value_c = IntVar() entry3 = Entry(mainapp, textvariable=value_c) entry3.grid(row=3, column=1) root.mainloop()
def addvalues(a,b): 返回a + b
我想将Button命令计算传递给entry3字段。怎么做?
提前感谢您的帮助。
答案 0 :(得分:1)
entry3.delete(0,END)将确保条目清晰(尝试不使用此行,看看会发生什么!)
from tkinter import *
import calculations
def foo():
x = addvalues(a=value_a.get(), b=value_b.get())
entry3.delete(0, END)
entry3.insert(END, x)
root = Tk()
mainapp = Frame(root)
mainapp.grid()
label1 = Label(mainapp, text="Value A")
label1.grid(row=0, column=0)
value_a = IntVar()
entry1 = Entry(mainapp, textvariable=value_a)
entry1.grid(row=0, column=1)
label2 = Label(mainapp, text="Value B")
label2.grid(row=1, column=0)
value_b = IntVar()
entry2 = Entry(mainapp, textvariable=value_b)
entry2.grid(row=1, column=1)
button = Button(mainapp, text="Calculate",
command=foo)
button.grid(row=2, columnspan=2)
label3 = Label(mainapp, text="A+B")
label3.grid(row=3, column=0)
value_c = IntVar()
entry3 = Entry(mainapp, textvariable=value_c)
entry3.grid(row=3, column=1)
root.mainloop()