问题
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.
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
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!
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
Then, on a .kv file I wish I could bind some popup's buttons to the newGame and change settings methods
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)
回答1:
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.
来源:https://stackoverflow.com/questions/23770703/kivy-accessing-a-method-on-a-different-class