Kivy:在不同的类访问方法 [英] Kivy: accessing a method on a different class

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

问题描述

让我们pretend我建立一个井字棋游戏(becouse它类似于pretty作为结构)
我想在弹出窗口中显示的结果,一个新的游戏按钮,我想这个弹出让我访问设置(与另一个按钮),并改变它们,在弹出的内总是熬夜,然后离开,最后将其关闭并开始一个新的游戏。

Let's pretend I am building a tic-tac-toe game (becouse it's pretty similar as a structure) I want the result to be shown in a popup, with a new game button, and I want this popup to let me access settings (with another button) and change them, always staying within the popup, then leave and finally close it and start a new game.

我希望我可以让事情变得有序,因此有一个单独的弹出类,我可以建立我的自定义弹出。

I wish i could keep things ordered and therefore have a separate popup class where i can build my custom popup.

我有newgame方法和复位方法,我的比赛网格类的方法,如明显。
用于更改设置方法是,在另一方面,在自定义设置类

I have the newgame method and reset method as method of my game-grid class, as obvious. Methods for changing settings are, on the other hand, on a custom settings class

在设计弹出类我怎么可以绑定它的按钮(例如新游戏),对包含在一个完全地不同类的方法?我看了一些例子KV,他们通常使用root.blabla.method来存取权限是在同一个树的不同位置(在.kv文件)的方法,但在这里,我想达到的方法是出树!

While designing the popup class how can I bind it's buttons (e.g new game) to methods that are contained on a completly different class? I've looked on some kv examples and they usually use root.blabla.method to acces a method that is in a different position of the same tree (in the .kv file) but here the methods I am trying to reach are out of the tree!

我会尽量把一些例如code,使之更加清晰。

I'll try to put some example code to make it more clear

class Settings():

    def changeSettings(self):
        ....

class GmeGrid(GridLayout):

    def newGame(self):
        ....

    def reset(self):
        ...

class customPopup(Popup):

    pass

然后,在一个.kv文件,我希望我能一些弹出的按键绑定到newGame和更改设置方法

Then, on a .kv file I wish I could bind some popup's buttons to the newGame and change settings methods

这里的问题是,我应该绑定在POPOP类按钮来完全地不同类的mothods,我不知道该怎么向(在.kv文件特别)

The problem here is that I should bind buttons on the popop class to the mothods of completly different class and I don't know how to to that (on the .kv file especially)

推荐答案

只要小部件已经完全实例化并添加到控件树,你可以使用 self.parent 进入小部件的父。你可能会考虑引用传递,而不是虽然:

As long as the widget has been fully instantiated and added to the widget tree, you can use self.parent to access the widget's parent. You might look into passing references instead though:

Builder.load_string('''
<CustomPopup>:
    BoxLayout:
        orientation: 'vertical'
        # some settings stuff here
        BoxLayout:
            orientation: 'horizontal'
            Button:
                text: 'New Game'
                on_press: root.do_new_game()
''')

class CustomPopup(Popup):
    settings_widget = ObjectProperty()
    new_game = ObjectProperty()

    def do_new_game(self):
        self.settings_widget.some_property = some_value
        self.dismiss()
        self.new_game()

p = CustomPopup(settings_widget=my_widget, new_game=mygame.newGame)
p.open()

这是最好的假设父母有设置,因为如果你改变,你保留设置,你只需要改变一个参考。

This is better that assuming the parent has the settings, because if you change where you keep the settings, you just need to change one reference.

这篇关于Kivy:在不同的类访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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