Python kivy add_widget adds double the amount of widget that overlap on each other

Python kivy add_widget adds double the amount of widget that overlap on each other

I tested dynamically adding widgets and they behave very oddly, i created custom widget that i add to the grid on pressing button add, but it all messed up. It definetely adds and clears, but the content is very weird, dont undestand what is wrong.

from kivy.app import App
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.lang.builder import Builder

Builder.load_file('conc.kv')

def search_sentences(text, query):
    """
    A function that searches for sentences containing a specific query within a given text.
    
    Parameters:
    text (str): The input text to search for sentences.
    query (str): The query string to search for within sentences.
    
    Returns:
    str: A string containing the sentences that contain the query.
    """
    sentences = re.split(r'(?<=[.!?])\s*', text)  # split on punctuation followed by whitespace or end of line
    result = []
    for sentence in sentences:
        if query.lower() in sentence.lower():
            result.append(sentence.strip() + '.')
    return '\n'.join(result)

class FilesWidget(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    
    def remove_files(self):
        self.parent.remove_widget(self)


class MyConcLayout(Widget):

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

    def add_files(self):
        fileswidget=FilesWidget()
        self.ids.files_layout.add_widget(fileswidget)



    def search(self):
        search_input = self.ids.search_input.text
        pass
    
        
        


    

        

class ConcApp(App):   
    def build(self):
        Window.clearcolor = (1, 1, 1, 1)
        Window.size = (500, 700)
        self.title = "Concordance"
        return MyConcLayout()



if __name__ == '__main__':
    ConcApp().run()
#:kivy 2.3.0
<FilesWidget>:
    file_name:file_name

    remove_file:remove_file

    cols: 3
    size_hint: 1, None
    height: 30
    CheckBox:
        size_hint: None, None
        size: 20, 20
    Label:
        id: file_name
        text: 'File name'
        color: [0, 0, 0, 1]
    Button:
        id: remove_file
        text: 'Remove'
        color: [1, 1, 1, 1]
        size_hint: None, 1
        size: 50, 30
        background_normal: ''
        background_color: [1, 0, 0, 1]
        on_press: root.remove_files()

<MyConcLayout>:

    add_file:add_file

    search:search

    output:output

    files_label:files_label

    search_input:search_input

    files_layout:files_layout

    GridLayout:
        cols: 2
        size: root.width, root.height
        StackLayout:
            canvas.before:
                Color:
                    rgba: 0.8, 0.8, 0.8, 1 
                Rectangle:
                    size: self.size
                    pos: self.pos
            orientation: 'lr-bt'
            size_hint_max_x: 50
            Button:
                id: add_file
                size_hint: None, None
                color: [0, 0, 0, 1]
                width: 50
                height: 50
                text: 'Add'
                on_press: root.add_files()
        GridLayout:
            cols: 1
            
            GridLayout:
                cols: 3
                size_hint: 1, None
                spacing: 2
                height: 30
                canvas.before:
                    Color:
                        rgba: 0.8, 0.8, 0.8, 1 
                    Rectangle:
                        size: self.size
                        pos: self.pos
                Button:
                    id: clear_search_input
                    text: 'Clear'
                    size_hint: None, None
                    size: 40, 30
                    color: [1, 1, 1, 1]
                    background_normal: ''
                    background_color: [1, 0, 0, 1]
                    on_press: root.ids.search_input.text = ''
                    
                TextInput:
                    id: search_input
                    hint_text: 'Search...'
                    multiline: False
                    size_hint: 1, None
                    height: 30


                Button:
                    id: search
                    text: 'Search'
                    size_hint: None, None
                    size: 100, 30
                    background_normal: ''
                    background_color: [0, 0, 0, 0]
                    
                    canvas.before:
                        Color:
                            rgba: 0, 0.5, 1, 1
                        RoundedRectangle:
                            size: self.size
                            pos: self.pos
                            radius: [0,13,13,0]
                    #on_press: root.search()
            Label:
                id: output
                text: 'output here'
                color: [0, 0, 0, 1]
                size_hint: 1, 1
                halign: 'center'
            GridLayout:
                id: files_layout
                cols: 1  
                size_hint: 1, 1
                Button:
                    id: files_label
                    text: 'Files here'
                    color: [0, 0, 0, 1]
                    halign: 'center'
                

When i add widgets, it all looks like overlapping mess, it adds 2 instances instead of one and this function is only called once

enter image description here

Upvotes: 0

Views: 23

Answers (0)

Related Questions