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

时间:2018-10-15 17:29:35

标签: python kivy kivy-language

我正在努力参考ScreenManager屏幕上的标签。我有一个RecycleView,该类在 init 中总计。我想将此总数放在RecycleView之外的屏幕上。 .kv文件中的ID为t_pay。总数在Nq_rv类中作为self.total_bill累积。如何从该课程中获取总计并更新“付款”屏幕上的总计字段?

请注意此屏幕截图底部的总字段:

enter image description here

我的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

embryo要求提供一些样本数据。文件“ 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

1 个答案:

答案 0 :(得分:0)

创建自定义类的想法是为您提供自定义属性,例如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