For a school project im attempting to make a app with kivy that tracks how much water you drink. What its meant to do is when you click the button it adds a 1 which is read and added up, it technically works the problem im facing is i cant seem to find a way to update the widget to show the new value after i click the button. This is the code of what ive got so far any and all help is appreciated
import kivy
#Handles graphics and running of application
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.graphics.instructions import *
from kivy.clock import Clock
import time
#how many cups of water
file = open("C:\\Users\\kbhga\\Google Drive\\Data Science & Networking\\water app\\cupsofwater.txt", "r")
line = file.read()
line.split(",")
file.close()
MyCups = sum([int(num) for num in line.split(',')])
#sets up layout of app
class CGrid(GridLayout):
def __init__(self, **args):
super(CGrid, self) .__init__(**args)
self.cols = 1
self.inside = GridLayout()
self.inside.cols = 3
self.inside.add_widget(Label(text="You have drank "+str(MyCups)+" cups of water today.\n"
"Click "+"'Hydrate' "+ "to add another"))
#self.cups = TextInput(multiline=False)
#self.inside.add_widget(self.cups)
self.add_widget(self.inside)
self.addcup = Button(text="Hydrate", font_size=45)
self.addcup.bind(on_press=self.pressed)
self.add_widget(self.addcup)
def pressed(self, instance):
print("water")
file = open("C:\\Users\\kbhga\\Google Drive\\Data Science & Networking\\water app\\cupsofwater.txt", "a")
file.write(",1,0")
file.close()
#Actually runs the application
class MyApp(App):
def build(self):
#update = CGrid()
#Clock.schedule_interval(update.main, 1)
return CGrid()
if __name__ == "__main__":
MyApp() .run()
# Clock.schedule_interval(refresher.eggtart, 1)
You should be able to do it in two ways. The instance
variable in the pressed function is a reference to the button that was clicked. So you can use it to set the text value or you can reference back through self attribute and change.
def pressed(self, instance):
instance.text = "NEW TEXT"
print("water")
file = open("C:\\Users\\kbhga\\Google Drive\\Data Science & Networking\\water app\\cupsofwater.txt", "a")
file.write(",1,0")
file.close()
Or
def pressed(self, instance):
self.addcup.text = "NEW TEXT"
print("water")
file = open("C:\\Users\\kbhga\\Google Drive\\Data Science & Networking\\water app\\cupsofwater.txt", "a")
file.write(",1,0")
file.close()
If you only need to constantly update one value you should think about kivy properties (StringProperty, NumericProperty...). But as you place your variable within a string it wont update this way. To show you the structure of a kivy property I used one in this example even if this alone wont be enough. I also added a bind function which fires whenever the linked attribute is changing its value. The bind function requires a callback function (here on_value_change) that fires in case of a value change. This sound a little complicated at first, but as your code gets more complex it is the best way to handle your attributes. I hope this works for you.
import kivy
#Handles graphics and running of application
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.graphics.instructions import *
from kivy.properties import NumericProperty
from kivy.clock import Clock
import time
#how many cups of water
file = open("C:\\Users\\kbhga\\Google Drive\\Data Science & Networking\\water app\\cupsofwater.txt", "r")
line = file.read()
line.split(",")
file.close()
MyCups = sum([int(num) for num in line.split(',')])
#sets up layout of app
class CGrid(GridLayout):
mycups = NumericProperty(MyCups)
def __init__(self, **args):
super(CGrid, self).__init__(**args)
self.cols = 1
self.inside = GridLayout()
self.inside.cols = 3
self.mylabel = Label(text="You have drank "+str(self.mycups)+" cups of water today.\n"
"Click "+"'Hydrate' "+ "to add another")
self.inside.add_widget(self.mylabel)
self.bind(mycups=self.on_value_change)
#self.cups = TextInput(multiline=False)
#self.inside.add_widget(self.cups)
self.add_widget(self.inside)
self.addcup = Button(text="Hydrate", font_size=45)
self.addcup.bind(on_press=self.pressed)
self.add_widget(self.addcup)
def pressed(self, instance):
print("water")
self.mycups += 1
file = open("C:\\Users\\kbhga\\Google Drive\\Data Science & Networking\\water app\\cupsofwater.txt", "a")
file.write(",1,0")
file.close()
def on_value_change(self, instance, value):
self.mylabel.text = "You have drank "+str(value)+" cups of water today.\n" "Click "+"'Hydrate' "+ "to add another"
#Actually runs the application
class MyApp(App):
def build(self):
#update = CGrid()
#Clock.schedule_interval(update.main, 1)
return CGrid()
if __name__ == "__main__":
MyApp() .run()
# Clock.schedule_interval(refresher.eggtart, 1)
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.