[英]Python/Kivy - Sending variables from one class to another
I'm not that new to Python so I have a basic understanding but I wouldn't say that I'm all that great either.我对 Python 并不是那么陌生,所以我有一个基本的了解,但我不会说我也很棒。
I've been having a tonne of problems with a brazilian jiujitsu app that I'm trying to make in Kivy.我一直在尝试在 Kivy 中制作的巴西柔术应用程序出现大量问题。 I am trying to get the text from a button that has been pressed in one window, and then use that text in a label for the next window that shows.我试图从一个 window 中按下的按钮获取文本,然后将 label 中的文本用于显示的下一个 window。 The former window I have given the class name GuardWindow()
and the latter window is called SweepSubTranPage()
.前者 window 我给了 class 名称GuardWindow()
,后者 window 被称为SweepSubTranPage()
。
I have managed to send the name of the button from my kivy file to the class GuardsWindow()
easily enough, and the line self.guardlabel = instance.text
works fine and retrieves the button name perfectly.我已经设法将我的 kivy 文件中的按钮名称轻松地发送到 class GuardsWindow()
,并且self.guardlabel = instance.text
行工作正常并且可以完美地检索按钮名称。 My main problem however is sending that name over to the SweepSubTranPage()
so that i can access it in my kivy file.然而,我的主要问题是将该名称发送到SweepSubTranPage()
以便我可以在我的 kivy 文件中访问它。
Here is the Python code:这是 Python 代码:
class GuardsWindow(Screen):
guardButtonName = ObjectProperty(None)
def send_guard_type(self, instance, **kwargs):
self.guardButtonName = instance.text # retrieves the button name
def sender(self): # my attempt at sending it to the other class
return self.guardButtonName
class SweepSubTranPage(Screen):
pageTitle = ObjectProperty(None)
gw = GuardsWindow()
pageTitle.text = gw.sender()
kv = Builder.load_file("my.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
And here is the Kivy file code:这是 Kivy 文件代码:
<GuardsWindow>
name: "GuardsHomepage"
ScrollView:
bar_width: 10
do_scroll_x: False
do_scroll_y: True
scroll_type: ["bars", "content"]
BoxLayout:
orientation: "vertical"
size_hint: 1, 2
padding: 10
spacing: 10
Button: # these are the buttons that i am getting the text from
text: "Full Guard"
on_press:
root.send_guard_type(self) # this sends the button
on_release:
app.root.current = "SweepSubTranPage"
root.manager.transition.direction = "left"
Button:
text: "Half Guard"
on_press:
root.send_guard_type(self) # this sends the button
on_release:
app.root.current = "SweepSubTranPage"
root.manager.transition.direction = "left"
<SweepSubTranPage>
name: "SweepSubTranPage"
pageTitle: title
BoxLayout:
orientation: "horizontal"
size_hint: 1, 0.1
Label: # this is the label that I need the text for
id: title
font_size: 40
size_hint: 1, 1
When I run the code above, I get the error:当我运行上面的代码时,我得到了错误:
File "C:/Users/bensw/PycharmProjects/BJJ Tracker/main.py", line 36, in <module>
class SweepSubTranPage(Screen):
File "C:/Users/bensw/PycharmProjects/BJJ Tracker/main.py", line 40, in SweepSubTranPage
pageTitle.text = gw.sender()
AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'
I hope I have been clear enough in explaining my issue.我希望我在解释我的问题时已经足够清楚了。 If you have any questions please ask!如果您有任何问题,请询问! Thank you so much!!太感谢了!!
In your send_guard_type()
method you can access another Screen
using the manager
property of any Screen
, and the get_screen()
method of the ScreenManager
.在您的send_guard_type()
方法中,您可以使用任何Screen
的manager
属性和ScreenManager
的get_screen()
方法访问另一个Screen
。 Something like this:像这样的东西:
def send_guard_type(self, instance, **kwargs):
self.guardButtonName = instance.text # retrieves the button name
self.manager.get_screen("SweepSubTranPage").ids.title.text = instance.text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.