簡體   English   中英

Kivy:從RecycleView更新ScreenManager屏幕上的字段

[英]Kivy: Updating a field on a ScreenManager screen from a RecycleView

我在努力參考ScreenManager屏幕上的標簽。 我有一個RecycleView,該類在init中累積了總計。 我想將此總數放在RecycleView之外的屏幕上。 .kv文件中的ID為t_pay。 總數在Nq_rv類中作為self.total_bill累積。 我如何從該課程中獲取總計並更新“付款”屏幕上的總計字段?

注意此屏幕截圖底部的總字段:

在此處輸入圖片說明

我的main.py文件是:

class Nq_rv(RecycleView):
    def __init__(self, **kwargs):
        super(Nq_rv, self).__init__(**kwargs)
        bill_text = []
        self.total_bill = 0.0
        with open('bill.csv') as bill:
            bill_reader = csv.reader(bill)
            for row in bill_reader:
                for item in range(len(row)):
                    if item < 2:
                        item_text = row[item]
                    else:
                        item_text = '{0:.2f}'.format(float(row[2]))
                        self.total_bill += float(row[2])    #total accumulation
                    text_row = {'text': item_text}
                    bill_text.append(text_row)

        self.data = bill_text

        #How do I update the t-pay field in the .kv file?

class Sm(ScreenManager):
    total_pay = ObjectProperty()    #I don't know if this is needed

    def send_survey(self):
        mypopup = MyPopup()
        mypopup.show_popup('Survey', 'Survey sent!', 'OK!')

    def pay(self):
        mypopup = MyPopup()
        mypopup.show_popup('Pay', 'Please put your card in the card reader and follow the prompts.', 'OK!')

    def tip_slider_update(self):
        self.ids.pay_screen.tip.text = '{0:.2f}'.format(float(self.ids.pay_screen.total_pay.text) * self.ids.pay_screen.tip_sldr.value)

    def close_app(self):
        App.get_running_app().stop()

class Pay_screen(Screen):
    pass

class Survey_screen(Screen):
    pass

class Finish_screen(Screen):
    pass

class ImageButton(ButtonBehavior, Image):
    pass

class Nq_button(Button):
    pass

class MyPopup(Popup):
    def show_popup(self, title_text, label_text, button_text):
        mytext= label_text
        content = BoxLayout(orientation="vertical")
        content.add_widget(Label(text=mytext, font_size=20, text_size=(300, None)))
        mybutton = Button(text="Ok!", size_hint=(1,.20), font_size=20)
        content.add_widget(mybutton)
        mypopup = Popup(content = content,              
                        title = title_text,     
                        auto_dismiss = False,         
                        size_hint = (.5, .5))        
        mybutton.bind(on_press=mypopup.dismiss)  
        mypopup.open()  

class nextqualApp(App):
    icon = 'nextqual.png'
    title = 'Pay / survey / join'

if __name__ == '__main__':

    nextqualApp().run()

我的.kv文件是nextqual.kv:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

Sm:
    id: sm
    transition: FadeTransition()
    Pay_screen:
        id: pay_screen
        manager: sm
    Survey_screen:
        id: survey_screen
        manager: sm

<Nq_check_label@Label>:
    markup: True
    multiline: True

<Nq_check_items@Label>:
    color: 0,0,0,1


<Nq_rv>:
    viewclass: 'Nq_check_items'
    RecycleGridLayout:
        cols: 3
        default_size: None, dp(20)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'

<Pay_screen>:
    name: 'pay'
    total_pay: t_pay

    BoxLayout:
        orientation: "vertical"
        padding: 6
        font_size: '24'
        BoxLayout:
            size_hint_y: None
            height: "40dp"
            Button: 
                text: "Pay your bill"
                on_release: app.root.current = 'pay'
            Button:
                text: "Tell us how we did"
                on_release: app.root.current = 'survey'
            Button:
                text: "I'm finished"

        BoxLayout:
            BoxLayout:
                padding: 12
                orientation: 'vertical'
                BoxLayout:
                    height: "40dp"
                    size_hint_y: None

                    Label:
                        text: 'Table: 4'
                    Label:
                        text: 'Server: Julie'
                    Label:
                        text: 'Check # 58645'
                BoxLayout:
                    canvas:
                        Color:
                            rgb: 255,255,255,255 
                        Rectangle:
                            size: self.size
                            pos: self.pos

                    Nq_rv:

                BoxLayout:
                    height: "30dp"
                    size_hint_y: None
                    Label:
                        text: 'Total check:'
                        size_hint_x: 75

                    #This is the field I want to update!
                    Label:
                        id: t_pay
                        text: '0.00'
                        halign: 'right'
                        size_hint_x: 25




<Survey_screen>:
    name: "survey"

    BoxLayout:
        orientation: "vertical"
        padding: 6
        font_size: '24'
        BoxLayout:
            size_hint_y: None
            height: "40dp"
            Button: 
                text: "Pay your bill"
                on_release: app.root.current = 'pay'
            Button:
                text: "Tell us how we did"
                on_release: app.root.current = 'survey'
            Button:
                text: "I'm finished"

        BoxLayout:

        BoxLayout:
            height: "100dp"
            size_hint_y: None

            Label:
                size_hint_x: 40
            Button:
                size_hint_x: 20
                text:"Send survey"
                halign: "center"
                on_press: app.root.send_survey()
            Label:
                size_hint_x: 40

胚胎要求一些樣本數據。 文件“ bill.csv”如下所示:

1,Seafood Sampler,15.99
1,Tea Smoked Duck,19.95
2,Shredded Duck with Ginger,22
1,Deli Rueben,9.95
1,Sam Adams,3
1,Cotswold Premium,4
1,Btl Pinot Noir,25

創建自定義類的想法是為您提供自定義屬性,例如Nq_rv不僅是RecycleView,而且是您可以添加新屬性的類,例如,您可以給屬性total_bill提供具有總數的信息。 可以從外部訪問該屬性,因此可以與t_pay文本進行綁定:

*的.py

class Nq_rv(RecycleView):
    total_bill = NumericProperty(0.00) # <-- new property

    def __init__(self, **kwargs):
        super(Nq_rv, self).__init__(**kwargs)
        self.load_data()

    def load_data(self):
        bill_text = []
        total = 0.0
        with open('bill.csv') as bill:
            bill_reader = csv.reader(bill)
            for row in bill_reader:
                for item, val in enumerate(row):
                    if item < 2:
                        item_text = val
                    else:
                        item_text = '{0:.2f}'.format(float(val))
                        total += float(val)    #total accumulation
                    text_row = {'text': item_text}
                    bill_text.append(text_row)

        self.data = bill_text
        self.total_bill = total # <-- update property

* .kv

BoxLayout:
    canvas:
        Color:
            rgb: 255,255,255,255 
        Rectangle:
            size: self.size
            pos: self.pos

    Nq_rv:
        id: rv # <-- set id

BoxLayout:
    height: "30dp"
    size_hint_y: None
    Label:
        text: 'Total check:'
        size_hint_x: 75

    #This is the field I want to update!
    Label:
        id: t_pay
        text: '{0:.2f}'.format(rv.total_bill) # <--- binding
        halign: 'right'
        size_hint_x: 25

暫無
暫無

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

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