问题
I want to change the color of an image. This image is placed on an button on a screen with screenmanager. When I push the button a popup appears to choose a color. This color should now be saved in a global variable and set as color for the (former white) image. My problem is, i cant access the property of the image from the class of the popup.
The relevant part of the .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()
And the .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()
The error I get:
AttributeError: 'super' object has no attribute '__ getattr __'
it only appers with the marked line... I dont know how I can realize this without errors! I hope I´ve done everything correct with my first question!
Thank you!
回答1:
You could for example pass the object you want to access to the popup.
Try this example. This will set the buttons text to the sliders value:
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()
来源:https://stackoverflow.com/questions/42373746/kivy-i-want-to-change-an-attribute-of-a-widget-in-a-screen-out-of-a-popup