Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12201

Kivy RecycleView custom labels

$
0
0

I'm creating an app with five screens.

  • On four screens the user can save different kind of data (number, string, list) to an sqlite3 database.
  • The fifth screen is the "History Screen" where the user can fetch the desired number of last elements from the database.

DB structure:

ListNameDateResult1Result1
DataInput15/06/202485None
DataInput115/06/20241, 2, 310, 11
DataInput215/06/2024PythonNone

On the History Screen I use RecycleView. It works but I want to improve it a bit. Unfortunately I'm stuck.

RecycleView now:

enter image description here

What I want to achive: to create a custom label for DataInput, Date, Result1, Result2 (and maybe a divider between the list name and the last number)

What I've tried so far: create a custom viewclass. The problem here is that I don't know how to populate the custom labels and achive the same result as you see in the picture above but with custom labels. I found similar questions here on stackoverflow but I still can't figure it out how to solve it.

Viewclass example:

<CustomView@BoxLayout>:    value_listname: ""    value_date: ""    value_result1: ""    value_result2: ""    BoxLayout:        orientation: "vertical"        Label:            text: root.value_listname            color: 1, 1, 0, 1            font_size: "13sp"        Label:            text: root.value_date            color: 1, 0, 1, 1        Label:            text: root.value_result1        Label:            text: root.value_result2        #Divider after the last number

Example code:

from kivy.app import Appfrom kivy.lang import Builderimport sqlite3import randomimport datetimefrom kivy.uix.screenmanager import ScreenManager, Screen, NoTransitionfrom kivy.properties import ObjectProperty, ListPropertyBuilder.load_string("""<CustomView@BoxLayout>:    value_listname: ""    value_date: ""    value_result1: ""    value_result2: ""    BoxLayout:        orientation: "vertical"        Label:            text: root.value_listname            color: 1, 1, 0, 1            font_size: "13sp"        Label:            text: root.value_date            color: 1, 0, 1, 1        Label:            text: root.value_result1        Label:            text: root.value_result2        #Divider after the last element<DataInputScreen>:    number_1: number_1    number_2: number_2    result: result    BoxLayout:        orientation: "vertical"        TextInput:            id: number_1            hint_text: "from... (int)"            halign: "center"            font_size: "50dp"            size_hint: 1, 0.5        TextInput:            id: number_2            hint_text: "...to (int)"            halign: "center"            font_size: "50dp"            size_hint: 1, 0.5        Label:            id: result            font_size: "50dp"        Button:            text: "Show the number and save it to DB"            on_release:                root.random_choice()        Button:            text: "Go to 'History' screen"            on_release:                app.root.current = "historyscreen"<HistoryScreen>:    record_text: record_text                BoxLayout:        orientation: "vertical"        TextInput:            id: record_text            hint_text: "Show the last 'n' record(s)"            halign: "center"            font_size: "50dp"            size_hint: 1, 0.2               Button:            text: "Show the records from DB"            size_hint: 1, 0.2            on_release:                root.show_records()        Button:            text: "Back to 'DataInput' screen"            size_hint: 1, 0.2            on_release:                app.root.current = "datainput"            BoxLayout:            size_hint: 1, 0.600            pos_hint: {"center_x": 0.500, "center_y": 0.350}            RecycleView:                id: rv                viewclass: "Label"                #viewclass: "CustomView"                RecycleBoxLayout:                    default_size: None, dp(30)                    default_size_hint: 1, None                    size_hint_y: None                    height: self.minimum_height                    orientation: 'vertical'"""                    )class DataInputScreen(Screen):    number_1 = ObjectProperty()    number_2 = ObjectProperty()    result = ObjectProperty()    def random_choice(self):        first_number = int(self.number_1.text)        second_number = int(self.number_2.text)        if first_number > second_number:            first_number, second_number = second_number, first_number        x = random.randint(first_number, second_number)        self.result.text = str(x)        conn = sqlite3.connect("results.db")        c = conn.cursor()        c.execute("INSERT INTO numbers VALUES (:ListName, :Date, :Result1, :Result2)",                  {"ListName": "DataInput","Date": datetime.datetime.now(),"Result1": self.result.text,"Result2": None})        conn.commit()        conn.close()class HistoryScreen(Screen):    record_text = ObjectProperty()    data_items = ListProperty([])    def show_records(self):        self.data_items = ""        conn = sqlite3.connect("results.db")        c = conn.cursor()        c.execute("SELECT Date, ListName, Result1, Result2 FROM numbers LIMIT ?", (self.record_text.text,))        records = c.fetchall()        conn.commit()        conn.close()        for record in filter(None, records):            for col in filter(None, record):                self.data_items.append(col)        self.ids.rv.data = [{'text': str(x)} for x in self.data_items]class myApp(App):    def build(self):        sm = ScreenManager(transition=NoTransition())        sm.add_widget(DataInputScreen(name="datainput"))        sm.add_widget(HistoryScreen(name="historyscreen"))"""Create Database"""        conn = sqlite3.connect("results.db")        c = conn.cursor()        c.execute("""CREATE TABLE if not exists numbers(        Date text,        ListName text,               Result1 text,                   Result2 text        )""")        conn.commit()        conn.close()        return smmyApp().run()

Could you help me?


Viewing all articles
Browse latest Browse all 12201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>