spinner widget crashes my kivy app on start

huangapple go评论56阅读模式
英文:

spinner widget crashes my kivy app on start

问题

I was working on a kivy(2.1.0) and added a spinner widget when I tried to run the app it crashed on startup. it works without the spinner widget. my code:

the .kv file:

<GridLayout>
    cols: 2
    rows: 8
    Label:
        text: "name:"
        row: 0
        col: 0
    TextInput:
        id: name
        multiline: False
        row:0
        col:1
        on_text: 
            if len(self.text.strip()) >= 31: self.text = self.text[0:30]
    Label:
        text: "description:"
        row: 1
        col: 0
    TextInput:
        id: description
        multiline: False
        row:1
        col:1
        on_text: 
            if len(self.text.strip()) >= 100: self.text = self.text[0:99]
    Spinner:
        text: "hi"
        values: ("hi1","hi2")

my python file:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner


class SkinAssent(App):
    def __init__(self, **kwargs):
        super(SkinAssent, self).__init__(**kwargs)

    def build(self):
        return GridLayout()


def main():
    SkinAssent().run()


if __name__ == '__main__':
    main()

a window pops for a second up then I get this error:

   File "C:\Usersmyrc\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\lang\builder.py", line 558, in _apply_rule
     assert(rule not in self.rulectx)
 AssertionError

Thanks, myrccar

英文:

I was working on a kivy(2.1.0) and added a spinner widget when I tried to run the app it crashed on startup. it works without the spinner widget. my code:

the .kv file:

&lt;GridLayout&gt;
    cols: 2
    rows: 8
    Label:
        text: &quot;name:&quot;
        row: 0
        col: 0
    TextInput:
        id: name
        multiline: False
        row:0
        col:1
        on_text: 
            if len(self.text.strip()) &gt;= 31: self.text = self.text[0:30]
    Label:
        text: &quot;description:&quot;
        row: 1
        col: 0
    TextInput:
        id: description
        multiline: False
        row:1
        col:1
        on_text: 
            if len(self.text.strip()) &gt;= 100: self.text = self.text[0:99]
    Spinner:
        text: &quot;hi&quot;
        values: (&quot;hi1&quot;,&quot;h12&quot;)
    

my python file:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner


class SkinAssent(App):
    def __init__(self, **kwargs):
        super(SkinAssent, self).__init__(**kwargs)

    def build(self):
        return GridLayout()


def main():
    SkinAssent().run()


if __name__ == &#39;__main__&#39;:
    main()

a window pops for a second up then i get this error:

   File &quot;C:\Usersmyrc\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\lang\builder.py&quot;, line 558, in _apply_rule
     assert(rule not in self.rulectx)
 AssertionError

thanks, myrccar

答案1

得分: 1

我相信GridLayout类存在某种冲突。尝试使用自定义类。您可以定义一个自定义类:

class MyGridLayout(GridLayout):
    pass

然后在您的kv文件中使用该自定义类:

<MyGridLayout>:
    cols: 2
    rows: 8
    Label:

以及在您的build()方法中:

def build(self):
    return MyGridLayout()
英文:

I believe there is some kind of conflict with the GridLayout class. Try using a custom class. You can define a custom class:

class MyGridLayout(GridLayout):
    pass

Then use that custom class in your kv:

&lt;MyGridLayout&gt;
    cols: 2
    rows: 8
    Label:

and in your build() method:

def build(self):
    return MyGridLayout()

huangapple
  • 本文由 发表于 2023年2月10日 07:09:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定