列出数据库中的订单号,项目和子项目

时间:2019-04-23 01:35:39

标签: python mysql listview kivy checkboxlist

因此,我正在尝试创建一个列表,该列表从一个显示订单号的mysql数据库中拉出,并显示一个新窗口,以显示订单号项目/子项目以及带有复选框的复选框,以选中该项目。我有一个弹出的窗口;但是,什么也没有填充,并且我的查询打印出了信息/列表。选中所有框并单击“完成”按钮后,它将从列表中删除订单以显示已处理。我在论坛上找到了;但是,这对我没有任何帮助: How to fetch data from database and show in table in kivy+python

谢谢!

main.py

import pymysql

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup

orderNum = "1"

class TextInputPopup(Popup):
    obj = ObjectProperty(None)
    obj_text = StringProperty("")

    def __init__(self, obj, **kwargs):
        super(TextInputPopup, self).__init__(**kwargs)
        self.obj = obj
        self.obj_text = obj.text


class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                  RecycleGridLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' Add selection support to the Button '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableButton, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected

    def on_press(self):
        popup = TextInputPopup(self)
        popup.open()

    def update_changes(self, txt):
        self.text = txt


class RV(BoxLayout):
    data_items = ListProperty([])

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.get_order()

    def get_order(self):
        connection = pymysql.connect(host='localhost', user='root',passwd='password', db='Shop')
        cursor = connection.cursor()

        cursor.execute("SELECT itemName, ITEM  FROM order_list WHERE orderID=%s", (orderNum,))
        rows = cursor.fetchall()


        # create data_items
        for row in rows:
            for col in row:
                self.data_items.append(col)


class TestApp(App):
    title = "Kivy RecycleView & pymysql Demo"

    def build(self):
        return RV()


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

kivy.kv

#:kivy 1.10.0

<TextInputPopup>:
    title: "Popup"
    size_hint: None, None
    size: 400, 400
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        TextInput:
            id: txtinput
            text: root.obj_text
        Button:
            size_hint: 1, 0.2
            text: "Finish"
            on_release:
                root.obj.update_changes(txtinput.text)
                root.dismiss()
        Button:
            size_hint: 1, 0.2
            text: "Cancel Changes"
            on_release: root.dismiss()


<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    BoxLayout:
        orientation: "vertical"

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 2

            Label:
                text: "Order Number"

        BoxLayout:
            RecycleView:
                viewclass: 'SelectableButton'
                data: [{'text': str(x)} for x in root.data_items]
                SelectableRecycleGridLayout:
                    cols: 2
                    default_size: None, dp(26)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'
                    multiselect: True
                    touch_multiselect: True

这是我的列表,其中提取信息并使用打印功能: (“ Sub”,“ Italian”) (“ Sub”,“ Meatball”) (“饮料”,“苹果汁”) ('配菜沙拉') (“汤”,“鸡肉和米饭”)

What I have and What I'm Trying to Achieve

0 个答案:

没有答案