kivy - 我想从弹出窗口中更改屏幕中窗口小部件的属性

时间:2017-02-21 17:12:27

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

我想改变图像的颜色。此图像放置在屏幕上的按钮上。当我按下按钮时,会出现一个弹出窗口以选择颜色。此颜色现在应保存在全局变量中,并设置为(以前的白色)图像的颜色。我的问题是,我无法从弹出窗口的类中访问图像的属性。

.kv的相关部分:

<HomeScreen>:

sm: sm
name: 'ScreenManager'

BoxLayout:
    orientation: 'vertical'
    rows: 2

    ActionBar:
        [...]

    ScreenManager:
        id: sm
        size_hint_y: .935

        Screen1:
            name: "screen_1"
        Screen2:
            name: "screen_2"
        Screen3:
            name: "screen_3"
[...]

<Popup_F>:

size_hint: .75, .5
auto_dismiss: True
BoxLayout
    orientation: 'horizontal'
    padding: 10, 0

    BoxLayout
        orientation: 'vertical'

        Slider:
            id: on_value_slider_r
            on_value:
                root.on_value_slider_rgb()

        Slider:
            id: on_value_slider_g
            on_value:
                root.on_value_slider_rgb()

        Slider:
            id: on_value_slider_b
            on_value:
                root.on_value_slider_rgb()

和.py:

class Screen1(Screen):
[...]

def colorchanger(self):

    farbenw = Popup_F()
    farbenw.open()

def colorchangern(self):
    # next line should change the color (as work around) of the Image but has no effect:
    self.ids.image_color_change.color = (r_R1, g_R1, b_R1, 1)


class Farbe(Popup):

def on_value_slider_rgb(self):
    global r_R1
    global g_R1
    global b_R1
    r_R1 = self.ids.on_value_slider_r.value
    g_R1 = self.ids.on_value_slider_g.value 
    b_R1 = self.ids.on_value_slider_b.value 

    # next line does cause an error
    self.ids.image_color_change.color = (r_R1, g_R1, b_R1, 1)
    Screen1().colorchangern()

我得到的错误:

AttributeError:'super'对象没有属性'__ getattr __'

它只带有标记线...我不知道如何能够毫无错误地实现这一点!我希望我在第一个问题上做的一切都是正确的!

谢谢!

1 个答案:

答案 0 :(得分:1)

例如,您可以将要访问的对象传递给弹出窗口 试试这个例子。这会将按钮文本设置为滑块值:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
from kivy.uix.popup import Popup


Builder.load_string(

'''

<Screen1>:
    BoxLayout:
        Button:
            text: root.button_text
            on_release:
                root.popup.open()

<MyPopup>:
    BoxLayout:
        orientation: "vertical"
        Slider:
            on_value:
                root.screen.button_text = str(self.value)
        Button:
            text: "Okey!"
            on_release:
                root.dismiss()

<MySm>:
    Screen1:


'''

)


class MySm(ScreenManager):
    pass


class MyPopup(Popup):

    def __init__(self,screen,**kwargs):
        super(MyPopup,self).__init__(**kwargs)
        self.screen = screen


class Screen1(Screen):
    button_text = StringProperty("text")
    def __init__(self,**kwargs):
        super(Screen1,self).__init__(**kwargs)
        self.popup = MyPopup(self)


class MyApp(App):

    def build(self):
        return MySm()


MyApp().run()