簡體   English   中英

如何使用 kivy 給變量值

[英]How do I give variables value with kivy

我對此很陌生,所以我可以想象這看起來很糟糕。 我的問題從第 43 行開始。我使用了一些我發現的不同代碼來開始學習。 我無法在 kivy 上找到答案,就像我有關於 python 一樣。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

Builder.load_string("""
<Calc>:
    # This are attributes of the class Calc now
    a: _a
    b: _b
    c: _c
    d: _d
    e: _e
    result: _result
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                GridLayout:
                    cols:1
                    Label:
                        text: 'Grats'
                    TextInput:
                        id: _a
                    Label:
                        text: 'Alcohol Sales'
                    TextInput:
                        id: _b
                    Label:
                        text: 'Food Sales'
                    TextInput:
                        id: _c
                    Label:
                        id: _result
                    TextInput:
                        id: _d
                        text: '0.06'
                    TextInput:
                        id: _e
                        text: '0.05'
                    Button:
                        text: 'sum'
                        # You can do the opertion directly
                        on_press: _result.text = int(_a.text) - (str(int(root.b)) * int(float(root.d.text)) + str(int(root.c)) * int(float(root.e.text))
                    Button:
                        text: 'product'
                        # Or you can call a method from the root class (instance of calc)
                        on_press: root.product(*args)
                     
            Screen:
                name: 'screen2'
                Label: 
                    text: 'The second screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'Go to Screen 1'
                on_press: _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press: _screen_manager.current = 'screen2'""")

class Calc(FloatLayout):
    # define the multiplication of a function
    def product(self, instance):
        # self.result, self.a and self.b where defined explicitely in the kv
        self.result.text = int(_a.text) - (str(int(self.b.text) * int(self.d.text)))
        

class TestApp(App):
    def build(self):
        return Calc()

if __name__ == '__main__':
    TestApp().run()

我有一個類似的應用程序,它只運行 python,可以支付小費。 我想為其添加一個 GUI,但我無法讓數學部分工作。

為了正確編碼,您需要控制文本輸入。 因為如果用戶通過其中任何一個,程序就會失敗。但是檢查我的代碼,看看你如何修復你的.kv 代碼中的錯誤。你需要改進你的代碼。 祝你好運。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
Builder.load_string("""
<Calc>:
    # This are attributes of the class Calc now
    a: _a
    b: _b
    c: _c
    d: _d
    e: _e
    result: _result
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                GridLayout:
                    cols:1
                    Label:
                        text: 'Grats'
                    TextInput:
                        id: _a
                    Label:
                        text: 'Alcohol Sales'
                    TextInput:
                        id: _b
                    Label:
                        text: 'Food Sales'
                    TextInput:
                        id: _c
                    Label:
                        id: _result
                    TextInput:
                        id: _d
                        text: '0.06'
                    TextInput:
                        id: _e
                        text: '0.05'
                    Button:
                        text: 'sum'
                        on_press: _result.text = str(float(_a.text)-float(_b.text)*float(_d.text)+float(_c.text)*float(_e.text))
                    Button:
                        text: 'product'
            Screen:
                name: 'screen2'
                Label: 
                    text: 'The second screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'Go to Screen 1'
                on_press: _screen_manager.current = 'screen1'
            Button:
                text: 'Go to Screen 2'
                on_press: _screen_manager.current = 'screen2'""")
class Calc(FloatLayout):
    def product(self, instance):
        pass #For improve, you need to use ObjectProperty for check textinputs.
class TestApp(App):
    def build(self):
        return Calc()
if __name__ == '__main__':
    TestApp().run()

暫無
暫無

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

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