在 Kivy 中创建全局变量
问题描述
几天来,我一直在努力尝试在 Kivy 中为小部件属性创建一个全局变量.我感到非常沮丧(可能是因为我一般是编码新手)并且似乎没有针对此问题的在线帮助.
I've been working on trying to create a global variable in Kivy for a widget property for days. I am getting incredibly frustrated (probably because I an new to coding in general) and there doesn't seem to be any help available online for this problem.
我的代码如下[PYTHON后跟KIVY]:
My code is as follows [PYTHON followed by the KIVY]:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.accordion import Accordion, AccordionItem
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
class ScreensManaged(ScreenManager):
pass
class MainScreen(Screen):
pass
class SettingsScreen(Screen):
pass
class StoryScreen(Screen):
pass
class StoryApp(App):
def build(self):
return ScreensManaged()
if __name__ == '__main__':
StoryApp().run()
还有……
#:kivy 1.8.0
<ScreensManaged>:
MainScreen:
SettingsScreen:
<MainScreen>:
name: "main"
FloatLayout:
Button:
text: "Begin the story!"
size_hint: .25, 0.10
pos_hint: {'x':.70, 'y':.25}
on_release: app.root.current = "settings"
Button:
text: "Settings"
size_hint: .25, 0.10
pos_hint: {'x':.70, 'y': .13}
on_release: app.root.current = "settings"
<SettingsScreen>:
name: "settings"
BoxLayout:
orientation: "vertical"
Accordion:
orientation: "vertical"
AccordionItem:
title: "Main Character"
size_hint:.9, 0.10
pos_hint: {'x':0.05, 'y':0.85}
Label:
text: "First Name"
TextInput:
id: firstName
text: "There's an animal in trouble!"
AccordionItem:
title: "Love Interest"
size_hint: .9, 0.10
pos_hint: {'x':0.05, 'y':0.70}
Button:
text: "What's up"
on_release: app.root.current = "main"
感兴趣的问题领域:
<StoryScreen>:
Label:
text: firstname.text
我知道它不能以这种方式工作,因为小部件的属性仅限于类范围 - 但是当我尝试在 Python 中将 StoryScreen 设为 SettingsScreen 的类时,Kivy 不会打开 - 当我尝试创建一个SettingsScreen 类中的全局变量
I know it isn't working this way because the properties of widgets are limited to the class scope - but when I try to make StoryScreen a class of the SettingsScreen in Python, Kivy won't open - when I try creating a global variable in the SettingsScreen class
class SettingsScreen(Screen):
global first
first = self.ids['first name']
class SettingsScreen(Screen):
first = self.ids['first name']
global first
或者当我尝试在 StoryApp 中创建变量时,程序崩溃了——也许我不需要全局变量,但我使用全局变量使用纯 Python 编写了这个应用程序的基本内部,它只是通过 Kivy 尝试和创建这个应用程序似乎比在 Python 之上学习 Swift 和 Java 更容易(它们太循环了.为什么我必须做 18 件事才能让程序记住 Swift 中的一串文本?).
Or when I try to create the variable in the StoryApp, the program crashes -- Maybe I don't need a global variable, but I coded the essential innards of this app using purely Python using the Global variable, and it just seemed must easier to try and create this application through Kivy than learning Swift and Java on top of the Python (they're just so loopy. Why do I have to do eighteen things to make the program remember one string of text in Swift?).
有什么帮助吗?
附:我确实浏览过 StackOverflow,虽然有很多本地类 Python 解决方案,但没有任何基于 Kivy 语言的解决方案.
P.S. I definitely looked through StackOverflow, and while there was a lot of local class Python solutions, there weren't any Kivy language based solutions.
附言我尝试设置一个全局值(如 API 中所建议的那样),但它也会使程序崩溃......
P.P.S. I tried setting a global value (as suggested in the API), but it would crash the program as well...
P.P.P.S.故事屏幕目前没有连接到任何按钮,因为我正在调试并试图找出代码的哪一部分导致程序崩溃——这就是为什么所有按钮都设置为设置"或主"的原因.
P.P.P.S. The story screen isn't currently attached to any buttons, because I was debugging and trying to figure out what portion of the code was crashing the program - that's why all of the buttons are set to "settings" or "main".
解决方案
不要设置全局变量,搞清楚你的两个类的关系,并在它们之间传递一个直接引用.
Don't set a global variable, work out the relation of your two classes and pass a direct reference between them.
class SettingsScreen(Screen):
global first
first = self.ids['first name']
由于纯粹的 python 原因,这不起作用 - self.ids 为类的每个 instance 填充,但此代码不是按实例运行,而是在类本身的定义处运行..此时它没有这个属性,即使有也不会被填充.
This doesn't work for purely pythonic reasons - self.ids is populated for each instance of the class, but this code runs not per-instance but at the definition of the class itself...at which point it does not have this attribute and it wouldn't be populated even if it did.
但是当我尝试将 StoryScreen 设为 Python 中的 SettingsScreen 类时,Kivy 无法打开
but when I try to make StoryScreen a class of the SettingsScreen in Python, Kivy won't open
您的示例似乎不包括这部分,但选择一种方便的方式在类之间传递变量至关重要.你的意思是 StoryScreen 是 SettingsScreen 的子类吗?
Your example doesn't seem to include this part, but it's vital to picking a convenient way to pass the variable between classes. Do you mean that the StoryScreen would be a subclass of SettingsScreen?
此外,包含程序崩溃时获得的 python 回溯通常很有用.这包括有关问题的信息,并且对于调试比仅仅知道程序崩溃更有用.
Also, it's often useful to include the python traceback that you get when the program crashes. This includes information about the problem, and is far more useful for debugging than just knowing that the program crashed.
相关文章