通过Kivy按钮在不同类中调用函数 [英] Calling Function in a Different Class Through Kivy Button

查看:409
本文介绍了通过Kivy按钮在不同类中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在kivy中调用按钮按钮的功能,该按钮位于与按钮不同的类屏幕中。我尝试在应用程序类中运行该功能,并在那里遇到问题。这里是我想要调用的函数所在的类:

pre $ #带按钮布局的主屏幕
class LandingScreen (屏幕):
def __init __(self,** kwargs):
super(LandingScreen,self).__ init __(** kwargs)
self.buttons = []#为所有按钮在这里
Clock.schedule_once(self._finish_init)

def ChangePic(self):
self.buttons [1] .background_normal ='folder.png'

以下是我试图用它调用的按钮:

 < InputScreen @ Screen>:
名称:'input_sc'
FloatLayout:
大小:800,480
id: anchor_1
标签:
text:你想绑定到这个按钮上的什么?
size_hint:(1,.15)
text_size:self.size
pos_hint:{'x':0.11,'top':1}
font_size:28
font_name:'Montserrat-Bold.ttf'
按钮:
root:'landing_sc'
id:filebutton
size:150,150
size_hint:None,None
background_normal:'folder.png'
background_down:'opacity.png'
pos_hint:{'x':0.11,'top':.7}
on_release:
root.manager.transition = FadeTransition()
root.manager.transition.duration = 1.5
app.MakeFolder()
root.IfFolder()
root.ChangeToSlide( )

为了从这个位置调用ChangePic(),我需要做些什么? / p>

另外 - 有没有一种方法可以轻松地与th一​​起工作在InputScreen类内的LandingScreen类中的e按钮?



谢谢!

解决方案你可以在你的应用程序类中创建一个变量:

$ $ p $ some_variable = LandingScreen()

,然后在按钮中调用ChangePic(),如下所示:

 on_release:app.some_variable.ChangePic()

另外,这可以帮助你: StackOverflow Kivy的Google小组简介属性


I am trying to call a function on button press in kivy, that is located in a different class screen than the button is located in. I tried running the function in the app class as well and ran into issues there. Here is the class where the function I am trying to call lies:

# Main screen with button layout
class LandingScreen(Screen):
    def __init__(self, **kwargs):
        super(LandingScreen, self).__init__(**kwargs)
        self.buttons = [] # add references to all buttons here
        Clock.schedule_once(self._finish_init)

    def ChangePic(self):
        self.buttons[1].background_normal = 'folder.png'

And here is the button that I am trying to call it with:

<InputScreen@Screen>:
    name: 'input_sc'
    FloatLayout:
        size: 800, 480
        id: anchor_1
        Label: 
            text: "What would you like to bind to this button?"
            size_hint: (1,.15)
            text_size: self.size
            pos_hint: {'x': 0.11, 'top': 1}
            font_size: 28
            font_name: 'Montserrat-Bold.ttf'
        Button:
            root: 'landing_sc'
            id: filebutton
            size: 150, 150
            size_hint: None, None
            background_normal: 'folder.png'
            background_down: 'opacity.png'
            pos_hint: {'x': 0.11, 'top': .7}
            on_release: 
                root.manager.transition = FadeTransition()
                root.manager.transition.duration = 1.5
                app.MakeFolder()
                root.IfFolder()
                root.ChangeToSlide()

What do I have to prefix ChangePic() with in order to call it from this location?

Alternatively- is there a way to easily work with the buttons inside of the LandingScreen class from inside of the InputScreen class?

Thanks!

解决方案

You can create a variable in your App Class:

some_variable = LandingScreen()

and then in your button call ChangePic() like this:

on_release: app.some_variable.ChangePic()

Also, this can help you: StackOverflow, Kivy's Google Group, Introduction to properties

这篇关于通过Kivy按钮在不同类中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆