kivy-我想从弹出窗口中更改屏幕中小部件的属性 [英] kivy - I want to change an attribute of a widget in a screen out of a popup
问题描述
我想更改图像的颜色.该图像放置在带有screenmanager的屏幕上的按钮上.当我按下按钮时,会出现一个弹出窗口来选择一种颜色.现在,应将此颜色保存在全局变量中,并将其设置为(以前的白色)图像的颜色.我的问题是,我无法从弹出窗口的类访问图像的属性.
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.
.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:超级"对象没有属性"__ getattr __"
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!
谢谢!
推荐答案
例如,您可以将要访问的对象传递给弹出窗口.
试试这个例子.这会将按钮文本设置为滑块值:
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()
这篇关于kivy-我想从弹出窗口中更改屏幕中小部件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!