代码之家  ›  专栏  ›  技术社区  ›  Nirdesh Kumawat

如何在kivy+python中从数据库中获取数据并在表中显示

  •  8
  • Nirdesh Kumawat  · 技术社区  · 7 年前

    列表py公司

    import kivy
    
    kivy.require('1.9.0')  # replace with your current kivy version !
    import sqlite3 as lite
    from kivy.uix.screenmanager import Screen
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.boxlayout import BoxLayout
    from kivy.core.window import Window
    
    Window.clearcolor = (0, 0.517, 0.705, 1)
    
    Window.size = (500, 330)
    from easygui import msgbox
    
    
    con = lite.connect('demo.db')
    con.text_factory = str
    cur = con.cursor()
    
    class TestScreen(Screen):
        pass
    
        def get_user(self):
    
            cur.execute("SELECT * FROM `user` order by id asc")
            self.rows = cur.fetchall()
    
            print(self.rows)
    
    class List(App):
        def build(self):
            self.root = Builder.load_file('list.kv')
            return TestScreen().get_user()
    
    
    
    if __name__ == '__main__':
        List().run()
    

    上面的代码获取结果,但我不知道如何像表一样列出

    如何从数据库中检索数据并以类似表格的形式列出

    id名称
    1 abc
    2 def

    当单击行时,它将显示在新窗口中,包含具有编辑值的文本类型。

    提前感谢

    2 回复  |  直到 5 年前
        1
  •  16
  •   ikolim    7 年前

    解决方案是使用 RecycleView 使用SelectableButton、SelectableRecycleGridleLayout、Popup和TextInput。有关详细信息,请参阅下面的示例。

    实例

    主要的py公司

    import sqlite3
    
    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
    
    
    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_users()
    
        def get_users(self):
            connection = sqlite3.connect("demo.db")
            cursor = connection.cursor()
    
            cursor.execute("SELECT * FROM Users ORDER BY UserID ASC")
            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 & SQLite3 Demo"
    
        def build(self):
            return RV()
    
    
    if __name__ == "__main__":
        TestApp().run()
    

    测验千伏

    #: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: "Save Changes"
                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: "User ID"
                Label:
                    text: "User Name"
    
            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
    

    输出

    enter image description here enter image description here enter image description here enter image description here

        2
  •  0
  •   Musab Khan    3 年前

    RecycleView 使用SelectableButton、SelectableRecycleGridleLayout、Popup和TextInput。有关详细信息,请参阅下面的示例。

    为了避免 在main中添加这两行。如果使用单独的

    主要的py公司

    import sqlite3
    
    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
    # Add these two lines to your code if you use separate .kv file
    from kivy.lang.builder import Builder
    Builder.load_file('kivy StactOverflow.kv')
    
    
    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_users()
    
        def get_users(self):
            connection = sqlite3.connect("demo.db")
            cursor = connection.cursor()
    
            cursor.execute("SELECT * FROM Users ORDER BY UserID ASC")
            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 & SQLite3 Demo"
    
        def build(self):
            return RV()
    
    
    if __name__ == "__main__":
        TestApp().run()
    

    测验千伏

    #:it will work kivy 1.10.0 and above
    
    <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: "Save Changes"
                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: "User ID"
                Label:
                    text: "User Name"
    
            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
    

    输出 Fetched from database to frontend