Python Kivy在类之间引用

时间:2018-05-08 13:49:07

标签: python kivy

以下是相关的.py代码:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.core.image import Image
from kivy.graphics import Color, Rectangle
from kivy.core.audio import SoundLoader
from kivy.core.text import LabelBase

import pygame
from pygame.locals import*

import random

class RootScreen(ScreenManager):
    pass


class LevelOneBedroomScreen(Screen):
    def __init__(self, **kwargs):
        super(LevelOneBedroomScreen, self).__init__(**kwargs)

    def next_text(self):
        if self.ids.level_one_bedroom_text.text == '*YAWN*':
            self.ids.level_one_bedroom_image.source = 'placeholder_background2.png'
            self.ids.level_one_bedroom_text.text = 'Hello'
        elif self.ids.level_one_bedroom_text.text == 'Hello':
            self.manager.current = 'level one bedroom choice'   

class LevelOneBedroomChoiceScreen(Screen):
    def __init__(self, **kwargs):
        super(LevelOneBedroomChoiceScreen, self).__init__(**kwargs)

    def get_image(self):
        #This part is not working
        self.ids.level_one_bedroom_choice_image.source = LevelOneBedroomScreen.ids.level_one_bedroom_image.source


class screensApp(App):
    def build(self):
        return RootScreen()

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

在底部附近,我评论了无效的代码部分。我有两个几乎相同的屏幕,其中一个LevelOneBedroomScreen有文本,LevelOneBedroomChoiceScreen有按钮。我需要两个背景图像是相同的,因为第一个屏幕偶尔更新图像我在.kv文件中写了一些东西,希望从第一个屏幕拉出图像,但它会导致错误。以下是.kv中的相关代码:

#:import FadeTransition kivy.uix.screenmanager.FadeTransition

<RootScreen>:
    transition: FadeTransition() 
    LevelOneBedroomScreen:
    LevelOneBedroomChoiceScreen:

<LevelOneBedroomScreen>:
    name: 'level one bedroom'
    id: level_one_bedroom
    Image:
        id: level_one_bedroom_image
        source: 'placeholder_background.png'
        size: self.size
        pos: self.pos
        allow_stretch: True
        keep_ratio: False
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Image of Bedroom'
        Button:
            text: 'Dialogue'
            background_color: 0,0,0,0       
    BoxLayout:
        size_hint_y: .2
        Label:
            text: 'left arrow'
        Button:
            text: 'choose'  
            on_release: root.manager.current = 'level one bedroom choice'           
        Label:
            text: 'right arrow'         
<LevelOneBedroomChoiceScreen>:
    name: 'level one bedroom choice'
    id: level_one_bedroom_choice
    Image:
        id: level_one_bedroom_choice_image
        source: get_image()
        size: self.size
        pos: self.pos
        allow_stretch: True
        keep_ratio: False
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Image of Bedroom'
        BoxLayout:
            orientation: 'vertical'
            BoxLayout:
                Button:
                    text: 'choice 1'
                Button:
                    text: 'choice 2'
            BoxLayout:
                Button:
                    text: 'choice 3'
                Button:
                    text: 'choice 4'
        BoxLayout:
            size_hint_y: .2
            Label:
                text: 'left arrow'
            Button:
                text: 'home'    
                on_release: root.manager.current = 'levels'         
            Label:
                text: 'right arrow' 

我插入了一条评论,显示代码的哪一部分导致错误。提前感谢您,非常感谢您的反馈。

1 个答案:

答案 0 :(得分:1)

使用Kivy Screen Manager,将 LevelOneBedroomScreen.ids.level_one_bedroom_image.source 替换为 self.manager.ids.level_one_bedroom.ids.level_one_bedroom_image.source

实施例

main.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen


class Home(ScreenManager):
    pass


class LevelOneBedroomScreen(Screen):
    def __init__(self, **kwargs):
        super(LevelOneBedroomScreen, self).__init__(**kwargs)

    def next_text(self):
        if self.ids.level_one_bedroom_text.text == '*YAWN*':
            self.ids.level_one_bedroom_image.source = 'placeholder_background2.png'
            self.ids.level_one_bedroom_text.text = 'Hello'
        elif self.ids.level_one_bedroom_text.text == 'Hello':
            self.manager.current = 'level one bedroom choice'


class LevelOneBedroomChoiceScreen(Screen):
    def __init__(self, **kwargs):
        super(LevelOneBedroomChoiceScreen, self).__init__(**kwargs)

    def on_pre_enter(self, *args):
        self.ids.level_one_bedroom_choice_image.source = self.manager.ids.level_one_bedroom.ids.level_one_bedroom_image.source


class TestApp(App):

    def build(self):
        return Home()


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

test.kv

#:kivy 1.9.1

<Home>:
    LevelOneBedroomScreen:
        id: level_one_bedroom
    LevelOneBedroomChoiceScreen:
        id: level_one_bedroom_choice

<LevelOneBedroomScreen>:
    name: 'level one bedroom'
    BoxLayout:
        orientation: 'vertical'
        Image:
            id: level_one_bedroom_image
            source: 'bedroom.jpeg'  # 'placeholder_background.png'
            size: self.size
            pos: self.pos
            allow_stretch: True
            keep_ratio: False
        Button:
            size_hint: 1, 0.2
            text: 'Bedroom Choice'
            on_release: root.manager.current = 'level one bedroom choice'

<LevelOneBedroomChoiceScreen>:
    name: 'level one bedroom choice'
    Image:
        id: level_one_bedroom_choice_image
        #below is the code that fails
        # source:
        size: self.size
        pos: self.pos
        allow_stretch: True
        keep_ratio: False

输出

Img01 - App Startup Img02 - Bedroom Choice Screen