首页 > 解决方案 > Kivy,按下按钮时更新值

问题描述

对于一个学校项目,我试图用 kivy 制作一个应用程序来跟踪你喝了多少水。它的意思是,当您单击按钮时,它会添加一个读取并添加的 1,它在技术上有效,我面临的问题是我似乎无法找到一种方法来更新小部件以在单击后显示新值按钮。这是我到目前为止得到的任何代码,所有帮助表示赞赏

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)

标签: pythonkivy

解决方案


如果您只需要不断更新一个值,您应该考虑 kivy 属性(StringProperty、NumericProperty ...)。但是当您将变量放在字符串中时,它不会以这种方式更新。为了向您展示 kivy 属性的结构,我在此示例中使用了一个,即使仅此一项还不够。我还添加了一个绑定函数,该函数在链接属性更改其值时触发。绑定函数需要一个回调函数(此处为 on_value_change),该函数在值更改时触发。起初这听起来有点复杂,但随着代码变得越来越复杂,这是处理属性的最佳方式。我希望这对你有用。

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)

推荐阅读