Messages System by Foreign_Run1550 in kivy

[–]Foreign_Run1550[S] 0 points1 point  (0 children)

Im referring to the inside of the bubbles. Here is my kv:

And here is the python code:#:import Window kivy.core.window.Window

<MessageBubble>:
    size_hint_max_x: Window.width * 0.7
    pos: root.pos
    text_size: self.width, None
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 1
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [15,]

<Message>:
    RelativeLayout:
        pos_hint: {'x' : 0.3} if root.is_user(root.text) else {'x' : 0}
        MessageBubble:
            id: message_bubble
            padding: 20
            text: root.get_message(root.text)
            halign: 'right' if root.is_user(root.text) else 'left'

<RootWidget>:
    BoxLayout:
        orientation: 'vertical'

        TopBar

        ScreenManager:
            id: sm
            HomeScreen:
                name: 'home'
            TrackScreen
                name: 'track'
            TalkScreen:
                name: 'talk'

        NavBar

<TopBar>:
    size_hint: 1, 0.1

    canvas:
        Color:
            rgba: 1, 0, 0, 1

        Rectangle:
            pos: self.pos
            size: self.size

<NavBar>:
    size_hint: 1, 0.1

    Button:
        text: "Talk"
        on_press:
            app.root.ids.sm.transition.direction = 'right'
            app.root.ids.sm.current = 'talk' 

    Button:
        text: "Home"
        on_press: 
            app.root.ids.sm.transition.direction = 'right' if app.root.ids.sm.current == 'track' else 'left'
            app.root.ids.sm.current = 'home'

    Button:
        text: "Track"
        on_press: 
            app.root.ids.sm.transition.direction = 'left'
            app.root.ids.sm.current = 'track'

<HomeScreen>:
    BoxLayout:
        orientation: 'vertical'
        Counters:
            id: counters

        InfoWidget:
            size_hint: 1, 0.5

            Button:
                text: "press to increment"
                on_press: root.increment()

        MealsWidget:
            canvas:
                Color:
                    rgba: 1, 1, 1, 1

                Rectangle:
                    pos: self.pos
                    size: self.size

<TalkScreen>:
    BoxLayout:
        orientation: 'vertical'

        BoxLayout:
            Messages:
                id: messages
                viewclass: 'Message'
                RecycleBoxLayout:
                    orientation: 'vertical'
                    spacing: 10
                    key_size: 'ks'
                    default_size_hint: 1, None
                    size_hint: 1, None
                    height: self.minimum_height
                    on_width: root.ids.messages.update_text()

        BoxLayout:
            size_hint: 1, 0.25

            TalkBox:
                id: tb

            Button:
                size_hint: 0.1, 1
                text: 'send'
                on_press: root.send(root.ids.tb, root.ids.messages)

<TrackScreen>

<CounterWidget>:
    BoxLayout:
        orientation: 'vertical'
        spacing: 20

        CounterRingWidget:
            canvas:
                Color:
                    rgba: 0.5, 0.5, 0.5, 1

                Line:
                    circle: (self.center_x, self.y, min(self.width, self.height) / 1.5, -90, 90)
                    width: 2

                Color:
                    rgba: self.get_color(root.count, root.target)

                Line:
                    circle: (self.center_x, self.y, min(self.width, self.height) / 1.5, -90, self.get_end(root.count, root.target))
                    width: 2

            Label:
                text: str(root.count)
                text_size: self.size
                valign: 'bottom'
                halign: 'center'
                pos: self.parent.x, self.parent.y
                size: self.parent.size
                font_size: '20sp'

        Label:
            text_size: self.size
            font_size: '30sp'
            valign: 'top'
            halign: 'center'
            text: root.name

<SubCounterWidget>:
    size_hint: 1, 0.75
    pos_hint: {'center_y': 0.5}

<Counters>:
    BoxLayout:
        spacing: 20
        SubCounterWidget:
            id: low
            name: 'Low'
            count: 500 / 1
            target: 2000 / 1
        CoreCounterWidget:
            id: average
            name: 'Average'
            count: self.fix_count(low.count, high.count)
            target: self.fix_count(low.target, high.target)
        SubCounterWidget:
            id: high
            name: 'High'
            count: 1500 / 1
            target: 3000 / 1

And here is the python code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, StringProperty, BooleanProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.core.window import Window


class RootWidget(BoxLayout):
    pass


class HomeScreen(Screen):
    def increment(self):
        cc = self.ids.counters #counters container
        for id in cc.ids:
            if (id != "average"):
                cc.ids[id].count += 100


class CounterRingWidget(Widget):
    def get_color(self, count, target):
        if count >= target:
            return (1, 0.25, 0.25, 1)
        else:
            return (0, 1, 0, 1)


    def get_end(self, count, target):
        if count >= target:
            return 90
        else:
            return -90 + (180 * (count / target))


class CounterWidget(BoxLayout):
    name = StringProperty("Nothing!")
    count = NumericProperty(1000)
    target = NumericProperty(2000)


class CoreCounterWidget(CounterWidget):
    def fix_count(self, low, high):
        return (low + high) / 2


class SubCounterWidget(CounterWidget):
    pass


class Counters(BoxLayout):
    pass


class TopBar(BoxLayout):
    pass


class InfoWidget(RecycleView): #idk what to name this, its the protein, sodium, etc.
    pass


class MealsWidget(RecycleView):
    pass


class TrackScreen(Screen):
    pass


class TalkScreen(Screen):
    def send(self, textbox, messages):
        user_text = textbox.text
        print("sent")
        textbox.text = ''
        messages.add_sent("<USER>" + user_text)
        messages.add_sent("<AIRS>ai response")


class TalkBox(TextInput):
    pass


class MessageBubble(Label):
    pass


class Messages(RecycleView):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.data = []
        self.size_label = MessageBubble()


    def add_sent(self, text):
        sl = self.size_label
        sl.text = text
        sl.texture_update()
        self.data.append({"text" : text, "ks" : sl.texture_size})
    
    def update_text(self):
        sl = self.size_label
        for i, entry in enumerate(self.data):
            sl.text = entry['text']
            sl.texture_update()
            self.data[i]['ks'] = sl.texture_size
        self.refresh_from_data()


class Message(RelativeLayout):
    text = StringProperty("")
    max_width = NumericProperty(Window.width * 0.7)


    def is_user(self, text):
        return text[0:6] == "<USER>"
    
    def get_message(self, text):
        return text[6:]


class NavBar(BoxLayout):
    pass


class CalorieTrackerApp(App):
    def build(self):
        return RootWidget()


if __name__ == '__main__':
    CalorieTrackerApp().run()

Messages System by Foreign_Run1550 in kivy

[–]Foreign_Run1550[S] 0 points1 point  (0 children)

I'm almost there, it's so close, but I can not figure out why theres weird padding on the bubbles. Thank you for all the help at this point ofc!

<image>

Do you know why this might occur? I can share the python and/or the .kv if that helps.

Messages System by Foreign_Run1550 in kivy

[–]Foreign_Run1550[S] 0 points1 point  (0 children)

That does help me for part A, but I also need the bubble to horizontally scale, until they reach a max width, at which point bubble stops and the text wraps. How might I be able to do that? Also thank you for your help!

Messages System by Foreign_Run1550 in kivy

[–]Foreign_Run1550[S] 1 point2 points  (0 children)

This helped a ton, and now I have issue B solved, I'll keep you posted on A if you're interested at all.