24
Июл
2021

При добавлении нового виджета в kivy приложение перестает работать

Недавно начал разбираться с kivy и столкнулся с такой проблемой. Приложение работает при запуске на ПК, но после сборки через buildozer оно не запускается. Выводил логи через logcat, какой-то определенной ошибки нет. В процессе разбирательства заметил, что подобная ошибка возникает если добавить любой дополнительный Layout в Container (будь то box, grid или scroll view). Пытался дублировать уже работающие элементы, но ситуация не меняется. Может быть в kivy есть какое-то ограничение по количеству виджетов?

main.py

from russian_names import RussianNames
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.button import ButtonBehavior
from kivy.uix.togglebutton import ToggleButton, ToggleButtonBehavior
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.popup import Popup
from kivy.lang import Builder



temp = [...]
aug = [...]
characters=[...]
navs=[...]
pr_arr=["hello"]
gun_corps=[...]
gun_list=[...]
mellee_list=[...]
sus=[...]
pril=[...]
weap=[]


class Pop(Popup):
    title = StringProperty()

    def __init__(self, title, **kwargs):
        super(Pop, self).__init__(**kwargs)
        self.set_description(title)

    def set_description(self, title):
        x=""
        i=int(title)
        x+="Противник " + str(int(title)+1)
        self.title = x
        self.pw1.text=str(pr_arr[i][0][0])
        self.itl1.text=str(pr_arr[i][0][1])
        self.ag1.text=str(pr_arr[i][0][2])
        self.augpr.text=str(pr_arr[i][2])
        self.cold.text=str(pr_arr[i][1][0])
        self.gun.text=str(pr_arr[i][1][1])
        self.hand.text=str(pr_arr[i][1][2])

class InsPanel(TabbedPanel):

    def clk(self,instance_toggle_button):
        instance_toggle_button.state="normal"
        popup = Pop(str(instance_toggle_button.group))
        popup.open()


    def chp(self):
        x=random()
        y=random()
        z=random()
        pw=x/(x+y+z)*15
        itl=y/(x+y+z)*15
        ag=z/(x+y+z)*15
        pw=round(pw)+1
        itl=round(itl)+1
        ag=round(ag)+1
        return pw, itl, ag

    def navs(self,hr):
        nav=""
        if hr<3:
            x=randint(1,2)
            cold=x
            x=randint(1,2)
            gun=x
            x=randint(1,2)
            hand=x
        elif hr==3:
            x=randint(1,3)
            cold=x
            x=randint(1,3)
            gun=x
            x=randint(1,3)
            hand=x
        elif hr>3:
            x=randint(1,5)
            cold=x
            x=randint(1,5)
            gun=x
            x=randint(1,5)
            hand=x
        return cold, gun, hand


    def aug(self,hr):
        augp=""
        if hr<3:
            numb=randint(0,1)
        elif hr==3:
            numb=randint(1,2)
        elif hr>3:
            numb=randint(2,4)

        if numb == 0:
            augp+="Не найдены"
        else:

            for i in range(numb):
                part=randint(0, len(aug)-1)
                if part == 0:
                    name = "\n   Руки: "
                elif part == 1:
                    name="\n   Голова: "
                elif part == 2:
                    name="\n   Ноги: "
                elif part == 3:
                    name="\n   Тело: "
                aug_i=randint(0, len(aug[part])-1)
                augp+= str(name) + str(aug[part][aug_i])
        return augp




    def ez(self):
        numb=randint(1,2)
        for i in range(numb):
            pr=[]
            ch=self.chp()
            nav=self.navs(1)
            augp=self.aug(1)
            pr.append(ch)
            pr.append(nav)
            pr.append(augp)
            pr_arr.append(pr)
            self.ids.grd1.add_widget(
                ToggleButton(
                    text=f'Противник {i+1}',
                    group=i,
                    on_press=self.clk,
                    size_hint_y=None,
                    height=150,
                    state='normal'
                )
            )


    def light(self):
        numb=randint(1,3)
        for i in range(numb):
            pr=[]
            ch=self.chp()
            nav=self.navs(2)
            augp=self.aug(2)
            pr.append(ch)
            pr.append(nav)
            pr.append(augp)
            pr_arr.append(pr)
            self.ids.grd1.add_widget(
                ToggleButton(
                    text=f'Противник {i+1}',
                    group=i,
                    on_press=self.clk,
                    size_hint_y=None,
                    height=150,
                    state='normal'
                )
            )

    def norm(self):
        numb=randint(1,4)
        for i in range(numb):
            pr=[]
            ch=self.chp()
            nav=self.navs(3)
            augp=self.aug(3)
            pr.append(ch)
            pr.append(nav)
            pr.append(augp)
            pr_arr.append(pr)
            self.ids.grd1.add_widget(
                ToggleButton(
                    text=f'Противник {i+1}',
                    group=i,
                    on_press=self.clk,
                    size_hint_y=None,
                    height=150,
                    state='normal'
                )
            )

    def hard(self):
        numb=randint(1,4)
        for i in range(numb):
            pr=[]
            ch=self.chp()
            nav=self.navs(4)
            augp=self.aug(4)
            pr.append(ch)
            pr.append(nav)
            pr.append(augp)
            pr_arr.append(pr)
            self.ids.grd1.add_widget(
                ToggleButton(
                    text=f'Противник {i+1}',
                    group=i,
                    on_press=self.clk,
                    size_hint_y=None,
                    height=150,
                    state='normal'
                )
            )

    def imp(self):
        numb=randint(1,5)
        for i in range(numb):
            pr=[]
            ch=self.chp()
            nav=self.navs(5)
            augp=self.aug(5)
            pr.append(ch)
            pr.append(nav)
            pr.append(augp)
            pr_arr.append(pr)
            self.ids.grd1.add_widget(
                ToggleButton(
                    text=f'Противник {i+1}',
                    group=i,
                    on_press=self.clk,
                    size_hint_y=None,
                    height=150,
                    state='normal'
                )
            )

    def genp(self, l):
        self.ids.grd1.clear_widgets()
        pr_arr.clear()
        if l==1:
            self.ez()
        elif l==2:
            self.light()
        elif l==3:
            self.norm()
        elif l==4:
            self.hard()
        elif l==5:
            self.imp()





class Container(TabbedPanel):

    def rand_guns(self, *args):
        #self.ids.list.clear_widgets()
        weap.clear()
        for j in gun_list:
            for i in range(25):
                l=""
                x=randint(0,len(gun_corps)-1)
                y=randint(0,len(pril)-1)
                z=randint(0,len(sus)-1)
                k=len(sus[z])
                m=len(pril[y])

                if x==1:
                    ren=randint(0,1)
                    if ren==1:
                        rn = RussianNames(count=1, patronymic=False,name=False).get_person()
                        ye=randint(1944,2040)
                        l= j + " " + str(gun_corps[x]) +' "'+str(rn)+" "+ str(ye)+'"'
                    else:
                        if sus[z][k-1] == "а" or sus[z][k-1] == "ь" or sus[z][k-1] == "я":
                            lek=pril[y][0:m-2]
                            lek=str(lek)+"aя"
                            l= j + " " + str(gun_corps[x]) + ' "' + lek + " " + str(sus[z]) + '"'
                        elif sus[z][k-1] == "е" or sus[z][k-1] == "о":
                            lek=pril[y][0:m-2]
                            lek=str(lek)+"ое"
                            l= j + " " + str(gun_corps[x]) + ' "' + lek + " " + str(sus[z]) + '"'
                        elif sus[z][k-1] == "и":
                            lek=pril[y][0:m-2]
                            lek=str(lek)+"ие"
                            l= j + " " + str(gun_corps[x]) + ' "' + lek + " " + str(sus[z]) + '"'
                        else:
                            l= j + " " + str(gun_corps[x]) + ' "' + str(pril[y]) + " " + str(sus[z]) + '"'
                else:
                    if sus[z][k-1] == "а" or sus[z][k-1] == "ь" or sus[z][k-1] == "я":
                        lek=pril[y][0:m-2]
                        lek=str(lek)+"aя"
                        l= j + " " + str(gun_corps[x]) + ' "' + lek + " " + str(sus[z]) + '"'
                    elif sus[z][k-1] == "е" or sus[z][k-1] == "о":
                        lek=pril[y][0:m-2]
                        lek=str(lek)+"ое"
                        l= j + " " + str(gun_corps[x]) + ' "' + lek + " " + str(sus[z]) + '"'
                    elif sus[z][k-1] == "и":
                        lek=pril[y][0:m-2]
                        lek=str(lek)+"ие"
                        l= j + " " + str(gun_corps[x]) + ' "' + lek + " " + str(sus[z]) + '"'
                    else:
                        l= j + " " + str(gun_corps[x]) + ' "' + str(pril[y]) + " " + str(sus[z]) + '"'
                weap.append(l)
                #self.ids.list.add_widget(Label(text=str(l)))

    def rand_character(self):
        numb=randint(2,4)
        for i in range(numb):
            ch=randint(0, len(characters)-1)
            if i==0:
                self.char.text=str(characters[ch])
            else:
                self.char.text+="\n" + str(characters[ch])
    def rand_aug(self):
        numb=randint(0,3)
        if numb == 0:
            self.aug.text ="Имплантов не обнаружено"
        else:
            for i in range(numb):
                part=randint(0, len(aug)-1)
                if part == 0:
                    name = "Руки: "
                elif part == 1:
                    name="Голова: "
                elif part == 2:
                    name="Ноги: "
                elif part == 3:
                    name="Тело: "
                aug_i=randint(0, len(aug[part])-1)
                if i==0:
                    self.aug.text= str(name) + str(aug[part][aug_i])
                else:
                    self.aug.text+= "\n" + str(name) + str(aug[part][aug_i])
    def rand_ch(self):
        x=random()
        y=random()
        z=random()
        pw=x/(x+y+z)*15
        itl=y/(x+y+z)*15
        ag=z/(x+y+z)*15
        pw=round(pw)+1
        itl=round(itl)+1
        ag=round(ag)+1
        self.pw.text=str(pw)
        self.itl.text=str(itl)
        self.ag.text=str(ag)
    def rand_temp(self):
        temp_i=randint(0, len(temp)-1)
        self.temp.text = str(temp[temp_i])
    def rand_name(self):
        rn = RussianNames(count=1, patronymic=False).get_person()
        self.name.text = str(rn)
        self.ct.text="Имя"
    def gen(self, *args):
        self.rand_name()
        self.rand_temp()
        self.rand_ch()
        self.rand_character()
        self.rand_aug()

class MyApp(App):
    def build(self):
        return Container()


if __name__ == "__main__":
    MyApp().run()

my.kv


<Container>:
    guns:guns
    name:name
    temp:temp
    ct:ct
    pw:pw
    itl:itl
    ag:ag
    aug: aug
    char:char
    do_default_tab: False
    tab_width:358

    TabbedPanelItem:
        text: 'НПС'
        GridLayout:
            rows: 32
            GridLayout:
                cols:2
                Label:
                    id:ct
                    text:""
                    bold: True
                    italic: True
                Label:
                    id: name
                    text:""
            GridLayout:
                cols:3
                GridLayout:
                    rows:2
                    Label:
                        text:"Мощь"
                        bold: True
                        italic: True
                    Label:
                        font_size: '100'
                        id: pw
                        text:""
                GridLayout:
                    rows:2
                    Label:
                        text:"Скепсис"
                        bold: True
                        italic: True
                    Label:
                        font_size: '100'
                        id: itl
                        text:""
                GridLayout:
                    rows:2
                    Label:
                        text:"Проворство"
                        bold: True
                        italic: True
                    Label:
                        font_size: '100'
                        id: ag
                        text:""
            GridLayout:
                rows:2
                Label:
                    text:"Темперамент"
                    bold: True
                    italic: True
                Label:
                    id: temp
                    text:""
            GridLayout:
                rows:2
                Label:
                    text:"Аугментации"
                    bold: True
                    italic: True
                Label:
                    id: aug
                    text:""
            GridLayout:
                rows:2
                Label:
                    padding_bottom: 20
                    text:"Характер"
                    bold: True
                    italic: True
                Label:
                    id: char
                    text:""
            BoxLayout:
                size_hint: .5, .5
                padding: 20
                Button:
                    text: "Сгенерировать"
                    on_release:
                        root.gen()

    TabbedPanelItem:
        text: 'Противники'
        BoxLayout:
            InsPanel:
    TabbedPanelItem:
        text: 'Лут'
        BoxLayout:
            orientation:"vertical"
            padding: 20
            Button:
                size_hint:1,.3
                text: "Сгенерировать"
                on_release:
                    root.rand_guns()
            Label:
                 id:guns
                 text:"blabla"






<InsPanel>
    do_default_tab: False
    tab_width:537
    TabbedPanelItem:
        id: tab
        text: 'Противники'
        GridLayout:
            rows:2
            GridLayout:
                cols:3
                size_hint: 1,0.3
                padding: 10
                Button:
                    background_color: (0.0, 255.0, 0.0, 1.0)
                    bold: True
                    text: "Оч. Легко"
                    on_release:
                        root.genp(1)
                Button:
                    background_color: (0.0, 255.0, 0.0, 0.8)
                    bold: True
                    text: "Легко"
                    on_release:
                        root.genp(2)
                Button:
                    background_color: (0.0, 255.0, 255.0, 0.8)
                    bold: True
                    text: "Нормально"
                    on_release:
                        root.genp(3)
                Button:
                    background_color: (255.0, 0.0, 0.0, 1.0)
                    bold: True
                    text: "Сложно"
                    on_release:
                        root.genp(4)
                Button:
                    background_color: (128.0, 0.0, 0.0, 0.8)
                    bold: True
                    text: "Невозможно"
                    on_release:
                        root.genp(5)
            ScrollView:
                size: self.size
                do_scroll_x: False
                do_scroll_y: True
                GridLayout:
                    id:grd1
                    cols:1
                    padding:20
                    size_hint_y: None
                    row_default_height: '160px'
                    height: self.minimum_height

    TabbedPanelItem:
        text: 'Боссы'
        BoxLayout:
            Label:
                text: 'В разработке'



<[email protected]>
    pw1:pw1
    itl1:itl1
    ag1:ag1
    cold:cold
    gun:gun
    hand:hand
    augpr:augpr
    GridLayout:
        rows:7

        ScrollView:
            size: self.size
            do_scroll_x: False
            do_scroll_y: True
            GridLayout:
                padding:20
                cols:1
                size_hint_y: None
                row_default_height: '200px'
                height: self.minimum_height
                Label:
                    font_size: '75'
                    text:"Характеристики"
                    bold: True
                    italic: True
                GridLayout:
                    cols:3
                    GridLayout:
                        rows:2
                        Label:
                            text:"Мощь"
                            bold: True
                            italic: True
                        Label:
                            font_size: '100'
                            id: pw1
                            text:""
                    GridLayout:
                        rows:2
                        Label:
                            text:"Скепсис"
                            bold: True
                            italic: True
                        Label:
                            font_size: '100'
                            id: itl1
                            text:""
                    GridLayout:
                        rows:2
                        Label:
                            text:"Проворство"
                            bold: True
                            italic: True
                        Label:
                            font_size: '100'
                            id: ag1
                            text:""
                Label:
                    font_size: '75'
                    text:"Боевые навыки"
                    bold: True
                    italic: True
                GridLayout:
                    cols:3
                    GridLayout:
                        rows:2
                        Label:
                            text:"Холодное"
                            bold: True
                            italic: True
                        Label:
                            font_size: '100'
                            id: cold
                            text:""
                    GridLayout:
                        rows:2
                        Label:
                            text:"Огнестрельное"
                            bold: True
                            italic: True
                        Label:
                            font_size: '100'
                            id: gun
                            text:""
                    GridLayout:
                        rows:2
                        Label:
                            text:"Ближний бой"
                            bold: True
                            italic: True
                        Label:
                            font_size: '100'
                            id: hand
                            text:""
                GridLayout:
                    rows:2
                    Label:
                        text:"Аугментации"
                        bold: True
                        italic: True
                    Label:
                        id: augpr
                        text:""
        GridLayout:
            padding:10
            rows:1
            size_hint: 1,0.1
            Button:
                text: 'Закрыть'
                on_press: root.dismiss()

Прошу помощи, т.к. сам ничего не смог нагуглить. Содержимое массивов, думаю не очень важно, поэтому вырежу.

Источник: https://ru.stackoverflow.com/questions/1308309/%D0%9F%D1%80%D0%B8-%D0%B4%D0%BE%D0%B1%D0%B0%D0%B2%D0%BB%D0%B5%D0%BD%D0%B8%D0%B8-%D0%BD%D0%BE%D0%B2%D0%BE%D0%B3%D0%BE-%D0%B2%D0%B8%D0%B4%D0%B6%D0%B5%D1%82%D0%B0-%D0%B2-kivy-%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5-%D0%BF%D0%B5%D1%80%D0%B5%D1%81%D1%82%D0%B0%D0%B5%D1%82-%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%D1%82%D1%8C

Тебе может это понравится...

Добавить комментарий