问题
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.
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()
And....
#: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"
The problematic area of interest:
<StoryScreen>:
Label:
text: firstname.text
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
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?).
Any help?
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.
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. 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".
回答1:
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']
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.
but when I try to make StoryScreen a class of the SettingsScreen in Python, Kivy won't open
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?
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.
来源:https://stackoverflow.com/questions/28143199/create-global-variable-in-kivy