Kivy,按按钮更新值

2024-05-23 16:47:59 发布

您现在位置:Python中文网/ 问答频道 /正文

对于一个学校项目,我试图用kivy制作一个应用程序,跟踪你喝了多少水。它的意思是,当你点击按钮时,它会添加一个1,这个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)

Tags: offromimportselfaddappwidgetfile
2条回答

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

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)

你应该可以用两种方法来做。pressed函数中的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()

相关问题 更多 >