【发布时间】:2014-11-15 01:37:54
【问题描述】:
from Tkinter import *
from cmath import sqrt
window = Tk()
solution = "Nothing to see for now ~"
a = Entry(window)
a.pack()
b = Entry(window)
b.pack()
c = Entry(window)
c.pack()
result_text = Text(window)
result_text.pack()
def calculate():
na = int(a.get())
nb = int(b.get())
nc = int(c.get())
delta = (nb **2) - (4 * na * nc )
if delta > 0:
x1 = ((- nb) + sqrt(delta)) / (2 * na)
x2 = ((- nb) - sqrt(delta)) / (2 * na)
solution = "x1 = " + str(x1) + "\n x2 = " + str(x2)
return solution
elif delta==0:
x = (-nb) / (2 * na)
solution = "x1 = " + str(x)
return solution
else:
solution = "There isn't any solution for this equation."
return solution
button = Button(window , text = "Calculate" , command = calculate)
button.pack()
result_text.insert(END,solution)
mainloop()
顺便说一句,我仍然是一个初学者......所以基本上我想做的是用 Tkinter 编写一个简单的 GUI 来计算二次方程的解,用户只需给出 a、b 和 c等式和程序在单击“计算”按钮后在文本小部件中显示结果,我做得很好,但它仍然不想用结果刷新文本小部件,而不是“暂时看不到任何东西” ~”消息!请问我该怎么办?
【问题讨论】:
-
calculate的返回值永远不会在任何地方使用 - 您需要在该函数中更新Text。
标签: python user-interface tkinter