我正在使用python和kivy制作一个简单的应用程序,但是当我为我的应用程序创建多个屏幕时,我无法访问某些类的类属性。
使用屏幕管理器访问方法时遇到问题。
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
class MyGame(Widget):
def __init__(self, **kwargs):
super(MyGame, self).__init__(**kwargs)
def print_a_word(self):
print('a word')
class OptionWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('screen_manager.kv')
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
kivy文件(screen_manager.kv)
#:kivy 2.0.0
# File name: screen_manager.kv
#: import MyGame Widget
WindowManager:
OptionWindow:
SecondWindow:
<OptionWindow>:
name: "main"
GridLayout:
cols:1
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: 0, 0
size: root.width, root.height
GridLayout:
rows: 5
padding: 50, 50
spacing: 20, 20
OptionBtn:
text: 'Americas'
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
MyGame
Button:
on_press: root.MyGame.print_a_word()
<OptionBtn@Button>
background_normal: 'Images/Other/white.png'
color: 0, 0, 0, 1
font_size: 20
canvas.after:
Color:
rgba: 0, 0, 0, 1
Line:
rectangle: self.x, self.y, self.size[0], self.size[1]
一切正常,直到我按下第二个窗口的按钮。我无法从MyGame(Widget)访问该方法。我得到'SecondWindow' object has no attribute 'MyGame'
这是一个较大问题的一部分,因为我制作此文件来解决我的原始问题是... 我有一个很大的程序,其中有两个文件main.py和my.kv,我想添加屏幕。在这个main.py中,所有内容都定义了一个从Widget继承的类,并且build方法返回该类的实例。这就是为什么我从上面制作文件的原因……是为了了解如何从Widget类进行访问。谢谢
答案 0 :(得分:0)
在这里,我修复了示例代码,现在可以访问和执行MyGame()对象中的print_my_word()方法,这足以为您提供帮助。
仅kv文件需要更改:
使用包含而不是在“ MyGame”对象中导入:
#:包括MyGame
在
为其添加了一个ID,以便可以访问它。
three = [word[2] for word in words]
好吧,下面的下一部分是更新,可以在第二条评论中回答您的问题。
main.py
WindowManager:
OptionWindow:
SecondWindow:
<OptionWindow>:
name: "main"
GridLayout:
cols:1
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: 0, 0
size: root.width, root.height
GridLayout:
rows: 5
padding: 50, 50
spacing: 20, 20
OptionBtn:
text: 'Americas'
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
MyGame:
id: my_game
Button:
on_press: my_game.print_a_word()
<OptionBtn@Button>
background_normal: 'Images/Other/white.png'
color: 0, 0, 0, 1
font_size: 20
canvas.after:
Color:
rgba: 0, 0, 0, 1
Line:
rectangle: self.x, self.y, self.size[0], self.size[1]
screen_manager.kv
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
class MyGame(Widget):
def __init__(self, **kwargs):
super(MyGame, self).__init__(**kwargs)
def print_a_word(self):
print('My Game: print method')
class OptionWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('screen_manager.kv')
class MyMainApp(App):
def build(self):
self.my_game = MyGame() # <-- instantiated MyGame in the Main class to access it from any where
return kv
if __name__ == "__main__":
MyMainApp().run()