weave
weave

Reputation: 3

Dynamically changing the background color for a widget added to a gridlayout

Morning....

Having a bit of trouble with a custom widget added to a column of a kivy gridlayout. I still learning kivy/kivyMD and was able to previously do this quite easily with PYQT6.

When my main page opens I'm reading a SQLite Database and loading the record contents in a gridlayout. I know you achieve this by setting the background properties for each widget added but in my case my custom widget has Buttonbehavior and an Image widget which I'm using to display and clickable image within the gridlayout. I read a posting saying you can set the background by setting the background_normal to "" and background_color to a color list but this did not work for me. I tried dynamically setting the canvas.before and canvas.after while loading the record contents but this as well messed up my screen.

What's currently happening is the image does properly display in my gridlayout but the background color is not working. The background color for the odd row setting was originally a color list of [.78, .85, .92, 1] which also did not work.

As usual any suggestions greatly appreciated.

thanks

main.py: (bottom function after page is displayed)

def show_service_schedule(self):

    # Read Service Schedule

    self.schedule_recs = DataRtns.get_records("schedule")

    if len(self.schedule_recs) > 0:

        self.ids.scrollgrid.rows = len(self.schedule_recs)

        # Adjust Scrollview height if rows are less than 5

        if self.ids.scrollgrid.rows < 5:

            # Adjust Title padding as well

            if self.ids.scrollgrid.rows == 1:
                self.ids.title.padding = [0, 0, 0, dp(90)]
            else:
                dp_val = self.ids.scrollgrid.row_default_height * self.ids.scrollgrid.rows + 60
                self.ids.title.padding = [0, 0, 0, dp(dp_val)]

            self.ids.scroll.height = self.ids.scrollgrid.row_default_height * self.ids.scrollgrid.rows

        recCtr = 0

        for rec in self.schedule_recs:

            if recCtr % 2 > 0:

                addr_col = ClickLabel()
                addr_col.text = rec[2]
                addr_col.padding = [5, 0]
                addr_col.md_bg_color = "#c5d8ea"

                city_col = ClickLabel()
                city_col.text = rec[3]
                city_col.padding = [40, 0]
                city_col.md_bg_color = "#c5d8ea"

                type_col = ClickLabel()
                type_col.text = rec[4]
                type_col.padding = [20, 0]
                type_col.md_bg_color = "#c5d8ea"

                stat_col = StatButton()
                stat_col.background_normal = ""
                stat_col.background_color = "#c5d8ea"
                stat_col.source = self.get_image(rec[4], rec[6])

            else:

                addr_col = ClickLabel(text=rec[2], padding=[5, 0])
                city_col = ClickLabel(text=rec[3], padding=[40, 0])
                type_col = ClickLabel(text=rec[4], padding=[20, 0])

                stat_col = StatButton()
                stat_col.background_normal = ""
                stat_col.background_color = [.9, .9, .9, 1]
                stat_col.source = self.get_image(rec[4], rec[6])


            self.ids.scrollgrid.add_widget(addr_col)
            self.ids.scrollgrid.add_widget(city_col)
            self.ids.scrollgrid.add_widget(type_col)
            self.ids.scrollgrid.add_widget(stat_col)

            recCtr += 1

    else:

        self.ids.title.padding = [0, 0, 0, dp(90)]

        self.ids.scrollgrid.cols = 1
        self.ids.scrollgrid.rows = 1
        self.ids.scroll.height = dp(36)

        self.schedule_recs = []

        no_records_col = MDLabel(text="- No Records Exist -", halign="center")
        self.ids.scrollgrid.add_widget(no_records_col)

custom widget:

class StatButton(ButtonBehavior, Image):
    def on_release(self):
        print("Image clicked ...")

Upvotes: 0

Views: 56

Answers (1)

weave
weave

Reputation: 3

I resolved this by using a MDRectangleFlatIconButton which gave me everything I needed. The Buttonbehavior I originally used although giving me clickable functionality for the widget I still had no ability to change the background color and even after redoing my image (also added) did not properly fill the gridlayouts column.

Upvotes: 0

Related Questions