SOOOOO 我还在从上一个问题开始研究那个学校项目,这次是原型,而不是测试。它应该解决方程式。我已经在vanilla Python和TkInter中编写了代码,这是有效的。它也有德国部分。我的奋斗是如何从TextInput到proccesing部分获取字符串并将其输出到LabelLös。所以这是我的代码:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
from kivy.uix.button import Button
a=42
class übung(GridLayout):
def __init__(self, **kwargs):
super(übung, self).__init__(**kwargs)
self.cols = 3
self.fill1 = Label(text = "")
self.label1 = Label(text = "Sagen sie ihre Gleichung")
self.fill2 = Label(text = "")
self.links = TextInput(multiline=False)
self.label2 = Label(text = '=')
self.rechts = TextInput(multiline=False)
self.fill3 = Label(text = '')
self.btn1 = Button(text="Lösen")
self.btn1.bind(on_press=self.einfach())
self.fill4 = Label(text = '')
self.fill5 = Label(text = '')
self.btn2 = Button(text="Mit Rechenweg lösen")
self.btn2.bind(on_press=self.einfachS)
self.fill6 = Label(text = '')
self.fill7 = Label(text = '')
self.lös = Label(text = '')
self.fill8 = Label(text = '')
self.fill9 = Label(text = '')
self.rech1 = Label(text = '')
self.fill10 = Label(text = '')
self.fill11 = Label(text = '')
self.rech2 = Label(text = '')
self.fill12 = Label(text = '')
self.fill13 = Label(text = '')
self.rech3 = Label(text = '')
self.fill14 = Label(text = '')
self.fill15 = Label(text = '')
self.rech4 = Label(text = '')
self.add_widget(self.fill1)
self.add_widget(self.label1)
self.add_widget(self.fill2)
self.add_widget(self.links)
self.add_widget(self.label2)
self.add_widget(self.rechts)
self.add_widget(self.fill3)
self.add_widget(self.btn1)
self.add_widget(self.fill6)
self.add_widget(self.fill7)
self.add_widget(self.btn2)
self.add_widget(self.fill8)
self.add_widget(self.fill9)
self.add_widget(self.lös)
self.add_widget(self.fill4)
self.add_widget(self.fill5)
self.add_widget(self.rech1)
self.add_widget(self.fill10)
self.add_widget(self.fill11)
self.add_widget(self.rech2)
self.add_widget(self.fill12)
self.add_widget(self.fill13)
self.add_widget(self.rech3)
self.add_widget(self.fill14)
self.add_widget(self.fill15)
self.add_widget(self.rech4)
def einfach(event, self):
r = self.rechts.text
l = self.links.text
x = 'x'
if x in l:
r1,r2 = r.split ('+')
rr = float(r1) + float(r2)
lr, ll = l.split('x')
lr = float(lr)
end = rr/lr
end = float (end)
self.lös.text = end
elif x in r:
l1,l2 = l.split ('+')
ll = float(l1) + float(l2)
rl, rr = r.split('x')
rl = float(rl)
end = ll/rl
end = float(end)
self.lös.text = end
def einfachS(self, event):
print ('nein')
class App(App):
def build(self):
return übung()
if __name__ == "__main__":
App().run()
如你所见,它非常紧凑,但我认为它只适用于1%左右 是的。 提前谢谢,我
答案 0 :(得分:1)
此:
self.btn1.bind(on_press=self.einfach())
与此完全相同:
result = self.einfach()
self.btn1.bind(on_press=result)
将功能绑定到按钮时,必须为按钮提供可调用。也就是说,函数的引用。相反,您正在调用函数并传入结果。
解决方法是将绑定更改为如下所示(注意缺少()
):
self.btn1.bind(on_press=self.einfach)
此外,您需要更改此内容:
def einfach(event, self):
......对此:
def einfach(self, event):