python - 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)
解决方案
如果您只需要不断更新一个值,您应该考虑 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)
推荐阅读
- pdf - CKEditor 使用工具栏上的“图像”按钮处理 PDF
- dialog - AEM 6.4 - 如何使用 Sling 模型从设计对话框中读取多字段?
- c# - 更改网格视图中的选定项目后更新列表视图项目值
- angular - this.http.get 的超时不能超过 2 分钟?
- r - R: 执行 rbind() 后设置一致的索引值
- node.js - 如何要求 Node.js 模块?
- spring-boot - spring cloud config刷新缓存配置
- python - 有没有办法在 conda env 中指定 jupyter 实验室扩展?
- python - 保存使用 BatchNorm 的 TensorFlow 模型
- javascript - 从提供的数据 json/array 生成带有标签的 HTML