簡體   English   中英

Kivy,按下按鈕時更新值

[英]Kivy, Update value on button press

對於一個學校項目,我試圖用 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)

你應該能夠通過兩種方式做到這一點。 按下的 function 中的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()

如果您只需要不斷更新一個值,您應該考慮 kivy 屬性(StringProperty、NumericProperty...)。 但是當您將變量放在字符串中時,它不會以這種方式更新。 為了向您展示 kivy 屬性的結構,我在此示例中使用了一個屬性,即使僅此一項還不夠。 我還添加了一個綁定 function ,每當鏈接屬性更改其值時就會觸發。 綁定 function 需要在值更改時觸發的回調 function(此處為 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM