在Kivy中创建全局变量 [英] Create Global Variable in Kivy

查看:324
本文介绍了在Kivy中创建全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在Kivy中创建一个全局变量,以获取widget的属性几天。我受到了令人难以置信的挫折(可能是因为我通常是一位新手),并且在网上似乎没有任何帮助可以解决这个问题。



我的代码如下[PYTHON后跟KIVY]:

  from kivy.app从kivy.uix.floatlayout导入App 
从kivy.uix.accordion导入FloatLayout
从kivy.uix.boxlayout导入Accordion,AccordionItem
导入BoxLayout
from kivy.uix.textinput从kivy.uix.label导入TextInput
import从kivy.uix.button导入标签
从kivy.uix.screenmanager导入按钮
导入ScreenManager,屏幕
from kivy.properties import ObjectProperty


$ b class ScreensManaged(ScreenManager):
pass
$ b $ class MainScreen(Screen):
通过

class SettingsScreen(屏幕):
通过

class StoryScreen(屏幕):
通过

class StoryApp (App):
def build(self):
return ScreensManaged()
$ b $ if if __name__ =='__main__':
StoryApp()。run()

和....

 #:kivy 1.8.0 

< ScreensManaged>:
MainScreen:
SettingsScreen:

< MainScreen>:
名称:main
FloatLayout:
按钮:
文字:开始故事!
size_hint:.25,0.10
pos_hint:{'x':。70,'y':。25}
on_release:app.root.current =settings
按钮:
文本:设置
size_hint:.25,0.10
pos_hint:{'x':。70,'y':.13}
on_release:app。 root.current =设置

< SettingsScreen>:
名称:设置
BoxLayout:
方向:vertical
Accordion:
orientation:vertical

AccordionItem:
title:主要字符
size_hint:.9,0.10
pos_hint:{'x': 0.05,'y':0.85}

标签:
text:名字
TextInput:
id:firstName
text:There's an麻烦的动物!

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>:
标签:
text:firstname.text

我知道这不是以这种方式工作的,因为属性的小部件仅限于类范围 - 但是当我尝试使StoryScreen成为Python中的SettingsScreen类时,Kivy将无法打开 - 当我尝试在SettingsScreen类中创建全局变量时

  class SettingsScreen(Screen):
全球第一
first = self.ids ['名字']

class SettingsScreen(Screen):
first = self.ids ['first name']
全球第一个

或者当我尝试在StoryApp中创建变量时,程序崩溃 - 也许我不需要全局变量,但我使用全局变量使用纯粹的Python编写了这个应用程序的重要内容,似乎通过Kivy尝试和创建这个应用程序比在Python之上学习Swift和Java更容易(它们只是很糟糕。为什么我必须做十八件事才能让程序记住Swift中的一串文本?)。

有什么帮助吗?

我绝对是通过StackOverflow查看的,虽然有很多本地类的Python解决方案,但没有任何基于Kivy语言的解决方案。

我尝试设置一个全局值(如API所示),但它也会使程序崩溃......

故事屏幕当前没有附加到任何按钮,因为我正在调试并试图找出哪部分代码会使程序崩溃 - 这就是为什么所有按钮都设置为settings或main的原因。

解决方案

不要设置全局变量,找出两个类的关系并在它们之间传递直接引用。 / p>

  class SettingsScreen(屏幕):
全球第一个
first = self.ids ['first name']]

这不适用于纯pythonic原因 - 为每个实例填充self.ids ,但是这段代码并不是按每个实例运行,而是在类本身的定义处运行......此时它没有这个属性,即使它有这个属性也不会被填充。 p>


但是当我尝试使StoryScreen成为Python中的SettingsScreen类时,Kivy不会打开
blockquote>

您的示例似乎没有包含此部分,但选择在类之间传递变量的便捷方式至关重要。你的意思是StoryScreen是SettingsScreen的一个子类吗?



另外,包含程序崩溃时获得的python traceback通常很有用。这包括有关这个问题的信息,而且对于调试更有用,而不仅仅是知道程序崩溃。


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".

解决方案

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.

这篇关于在Kivy中创建全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆