Kivy,按下按钮时更新值

问题描述 投票:0回答:2

对于一个学校项目,我正在尝试使用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)
python kivy
2个回答
0
投票

您应该可以通过两种方式做到这一点。按下的功能中的instance变量是对所单击按钮的引用。因此,您可以使用它来设置文本值,也可以通过self属性进行引用并进行更改。

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()

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()

0
投票

如果只需要不断更新一个值,则应考虑kivy属性(StringProperty,NumericProperty ...)。但是,当您将变量放在字符串中时,它不会以这种方式更新。为了向您展示kivy属性的结构,在此示例中我使用了一个,即使仅此一项是不够的。我还添加了一个绑定函数,只要链接属性更改其值,该函数就会触发。 bind函数需要一个回调函数(在此为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)
© www.soinside.com 2019 - 2024. All rights reserved.