我的程序应该解决方程式。我刚刚添加了区分+和 - 的部分。这些方程式非常简单。 (如2x = 4 + 2或4x = 1-4)这是我的代码:
from tkinter import *
import time
form = Tk()
form.title('Hello')
auff = Label(form, text='Put in Equation!')
rein = Entry(form)
lein = Entry(form)
istg = Label (form, text='=')
stop = Button(form, text='Stop', command=form.destroy)
start = Button (form, text='Compute')
erge = Label (form, text='')
r = rein.get()
l = lein.get()
def operation (event):
if ('+' or '-' in l):
if '+' in l:
l1,l2 = l.split ('+')
if ('x' not in l1 or l2):
ll = l1 + l2
return ll
if '-' in l:
l1,l2 = l.split ('-')
if ('x' not in l1 or l2):
ll = l1 - l2
return ll
if ('+' or '-' in r):
if '+' in r:
r1,r2 = r.split ('+')
if ('x' not in r1 or r2):
rr = r1 + r2
return rr
if '-' in r:
r1,r2 = r.split ('-')
if ('x' not in r1 or r2):
rr = r1 - r2
return rr
def einfach (event):
r = rein.get()
l = lein.get()
x = 'x'
if x in l:
operation (event)
lr = int(lr)
end = rr/lr
end = int (end)
erge['text']= 'x = {}'.format(end)
elif x in r:
operation (event)
rl = int(rl)
end = ll/rl
end = int(end)
erge['text']='x = {}' .format(end)
start.bind('<Button-1>', einfach)
auff.grid(row = 0, column = 1)
rein.grid(row = 1, column = 2)
istg.grid(row = 1, column = 1)
lein.grid(row = 1, column = 0)
erge.grid(row = 4, column = 1)
start.grid(row = 2, column = 1)
starts.grid(row = 3, column = 1)
erkl1.grid(row = 5, column = 1)
erkl2.grid(row = 6, column = 1)
erkl3.grid(row = 7, column = 1)
erkl4.grid(row = 8, column = 1)
stop.grid(row = 9, column = 1)
start.mainloop()
我可能会添加错误:没有错误消息但是当您按下计算时它不会执行任何操作。
提前致谢,
香蕉
答案 0 :(得分:0)
您的代码有几个问题。
您编写while
语句的方式不符合您的预期。
声明:
while '+' or '-' in l:
并不意味着当+
或-
位于l
时,请执行某些操作。相反,这意味着当+
为True或-
位于l
时,则执行某些操作。因为+
总是为True,所以while语句将始终处于活动状态。
你应该写的是:
while '+' in l or '-' in l:
这与以下几行:+
中的l
或-
中l
的{{1}}然后执行某些操作。
您可能需要查看与解决x相关的一些堆栈溢出帖子。
这里有一篇你可能会发现有帮助的帖子How can I solve equations in Python?