【问题标题】:Changing class variable and print them on kivy screen labels更改类变量并将它们打印在 kivy 屏幕标签上
【发布时间】:2019-12-03 15:23:12
【问题描述】:

Python 代码:

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager,Screen,FadeTransition
from kivy.app import App
import copy

class introscreen(ScreenManager,Screen):
    pass

class screenone(Screen):
    pass

class screentwo(Screen):
    slips = []

    def Flowerscopy(self):
        Flower = ["Rose", "Tulips", "Sunflower", "Marigold"]
        slips = copy.copy(Flower)



class testApp(App):
    SO = screenone()
    ST = screentwo()
    def build(self):
        return introscreen()


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

.kv 代码

 <introscreen>:
 #: import SlideTransition kivy.uix.screenmanager.SlideTransition

 transition: SlideTransition()
 screenone:
 screentwo:

<screenone>:
 name: "One"
 FloatLayout:
     BoxLayout:
         orientation: "vertical"
         size_hint: 0.50,0.50
         pos_hint:{"center_x":0.5,"top":0.75}
         Button:
             text: "Flowers"
             size_hint: 0.5,2.5
             pos_hint:{"center_x":0.5}
             on_release:
                 app.root.transition = SlideTransition(direction = 'left')
                 app.root.current = "Two"
                 app.ST.Flowerscopy()
<screentwo>:
  name: "Two"
  BoxLayout:
     orientation : "horizontal"
     pos_hint : {"center_x": 0.2,"top": 0.2}
     size_hint: 0.80,0.30
     Label:
         id: s1
         text: str(root.slips[0])  

我正在练习我的 kivy 基础知识。我遇到了一个错误问题,即索引超出范围错误 当我尝试在 class screentwo 中的 slips list 的标签上放置文本并且我想使用Flowerscopy() 在第一个屏幕调用并且还想在标签上打印,因为单据是空的,他们给我错误请帮助我,我现在是初学者

【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

    这是您的代码的修改版本,可以满足您的要求:

    from kivy.lang import Builder
    from kivy.properties import ListProperty
    from kivy.uix.screenmanager import ScreenManager,Screen
    from kivy.app import App
    
    
    class introscreen(ScreenManager):
        pass
    
    
    class screenone(Screen):
        pass
    
    
    class screentwo(Screen):
        slips = ListProperty(['']*4)
    
        def Flowerscopy(self):
            Flower = ["Rose", "Tulips", "Sunflower", "Marigold"]
            self.slips = Flower
    
    
    Builder.load_string('''
    #: import SlideTransition kivy.uix.screenmanager.SlideTransition
    
    <screenone>:
        name: "One"
        FloatLayout:
            canvas.before:
                Color:
                    rgba: 1,0,0,1
                Rectangle:
                    size: self.size
                    pos: self.pos
            BoxLayout:
                canvas.before:
                    Color:
                        rgba: 0,1,0,1
                    Rectangle:
                        size: self.size
                        pos: self.pos
                orientation: "vertical"
                size_hint: 0.50,0.50
                pos_hint:{"center_x":0.5,"top":0.75}
                Button:
                    text: "Flowers"
                    size_hint: None, None
                    size: self.texture_size
                    size_hint: 0.5,2.5
                    pos_hint:{"center_x":0.5}
                    on_release:
                        app.root.transition = SlideTransition(direction = 'left')
                        app.root.current = "Two"
                        app.ST.Flowerscopy()
    <screentwo>:
        name: "Two"
        BoxLayout:
            canvas.before:
                Color:
                    rgba: 1,0,0,1
                Rectangle:
                    size: self.size
                    pos: self.pos
            orientation : "horizontal"
            pos_hint : {"center_x": 0.2,"top": 0.2}
            size_hint: 0.80,0.30
            Label:
                id: s1
                text: root.slips[0]
    ''')
    
    
    
    class testApp(App):
        SO = screenone()
        ST = screentwo()
        def build(self):
            theRoot = introscreen()
            theRoot.add_widget(self.SO)
            theRoot.add_widget(self.ST)
            return theRoot
    
    
    if __name__ == "__main__":
        testApp().run()
    

    我已将您的“kv”文件添加为Builder.load_string(),只是为了我自己的方便。

    第一个更改是使introscreen 仅扩展ScreenManager。在同一个Widget 中同时扩展ScreenManangerScreen 不是一个好主意,并且可能会导致问题。

    接下来,您的slips 必须是ListProperty。使用Property 允许kv 创建绑定,当Property (slips) 被修改时,您的Label.text 会自动更新。

    为了访问app.STapp.SO,您需要在testApp 中定义SOST,以引用这些Screens 在您的显示中使用的实际实例.在您的原始代码中,正在创建两组 ScreenstestApp 代码中的那些和“kv”文件中的那些。我的更改从kv 文件中删除了&lt;introscreen&gt; 规则,而只是在build() 方法中构建introscreen

    此外,Flowerscopy() 方法可以简单地将新的List 分配给slips ListProperty。由于slips 的元素已经是字符串,所以不需要str() 调用。

    索引错误是因为你的slips的初始值是一个空列表。我的代码slips = ListProperty(['']*4) 将初始值设置为一个空字符串列表,这样就不会遇到索引错误。

    【讨论】:

    • 我在我的代码中使用了你的建议但是他们给了我警告 [警告] [多个屏幕名为“screenone”] [, ] [警告] [多个名为“screentwo”的屏幕] [, ]
    • 我发布的代码没有给出那个错误。如果您还有其他问题,您应该使用当前代码发布另一个问题。
    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多