Is it possible to get an automatic result calculation in kivy? I think that I have 3 TextInput fields and when at least 2 of them are filled in 3 I will get the result.
I think of something like that:
--------------
| 2 | --> TextInput 1
--------------
|result = 5 | --> TextInput 2
-------------
| 3 | ---> TextInput 3
--------------
Or
--------------
| result = 5 | --> TextInput 1
--------------
| 2 | --> TextInput 2
-------------
| 3 | ---> TextInput 3
--------------
The result should appear automatically after entering at least 2 numbers.
My my.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
class HomeScreen(Screen):
pass
class CalculatorHome(Screen):
pass
GUI = Builder.load_file("kv/my.kv")
class MainApp(App):
def build(self):
return GUI
def change_screen(self, screen_name, direction):
# Get the screen manager from the kv file
screen_manager = self.root.ids['screen_manager']
screen_manager.current = screen_name
screen_manager.transition.direction = direction
def clear_text(self, *args):
for ar in args:
ar.text = ''
MainApp().run()
Calculate.kv with TextIput
<CalculatorHome>:
FloatLayout:
GridLayout:
rows:3
pos_hint: {"top": .8, "left": 1 }
size_hint: 1, .4
TextInput:
id: number1
multiline: False
TextInput:
id: number2
multiline: False
TextInput:
id: number3
multiline: False
GridLayout:
rows:1
pos_hint: {"top": .3, "left": 1 }
size_hint: 1, .1
Button:
text: "CLEAR"
on_press: app.clear_text(number1, number2, number3 )
It would be more simple if you put something from kv to python file, like this:
class MainApp(App):
def build(self):
# if you use ScreenManager you should return it in build function
self.sm = ScreenManager()
# creating the objects of screen classes
self.homescreen = HomeScreen()
self.calculatorhome = CalculatorHome()
# don't forget to set names of screens in kv file!
self.sm.add_widget(self.homescreen)
self.sm.add_widget(self.calculatorhome)
self.t1 = ObjectProperty(None)
self.t2 = ObjectProperty(None)
self.t3 = ObjectProperty(None)
Clock.schedule_once(self.gettextinputs)
return self.sm
def gettextinputs(self, *args):
# get textinputs from kv
self.t1 = self.calculatorhome.ids.number1
self.t2 = self.calculatorhome.ids.number2
self.t3 = self.calculatorhome.ids.number3
# bind on change function calling
self.t1.bind(text = lambda e1, e2: self.checkinputs())
self.t2.bind(text = lambda e1, e2: self.checkinputs())
self.t3.bind(text = lambda e1, e2: self.checkinputs())
def checkinputs(self):
if self.t1.text and self.t2.text:
result = int(self.t1.text) + int(self.t2.text)
self.t3.text = str(result)
elif self.t1.text and self.t3.text:
result = int(self.t1.text) + int(self.t3.text)
self.t2.text = str(result)
elif self.t2.text and self.t3.text:
result = int(self.t2.text) + int(self.t3.text)
self.t1.text = str(result)
So now when you change text of your textinputs the empty one will show the result of others two.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.