kivy多屏找不到id

Posted

技术标签:

【中文标题】kivy多屏找不到id【英文标题】:kivy multiscreen can not find the id 【发布时间】:2019-01-19 15:29:08 【问题描述】:

所有,我无法在我的 kivy 代码中将 goi 传递给 VQCIA 类中的函数 submit_goi。如果有人可以修复代码,我将不胜感激。 print self.goi.text() 语句报告了一个问题,它说 AttributeError: 'NoneType' object has no attribute 'text'。这真的很奇怪。

# ---------- vqcia.py  ----------
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty 
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

Builder.load_string("""
<HelloVQCIA>:
    BoxLayout:
        Button:
            text: "Welcome to VQCIA"
            on_press:
                # You can define the duration of the change
                # and the direction of the slide
                root.manager.transition.direction = 'left'
                root.manager.transition.duration = 1
                root.manager.current = 'screen_two'

<VQCIA>:
    BoxLayout:
        orientation: "vertical"
        goi: g_o_i
        padding: 10
        spacing: 10
        size: 400, 200
        pos: 200, 200
        size_hint:None,None

        BoxLayout:
            Label:
                text: "Enter gene of interest with TAIR ID:"
                font_size: '25sp'

        BoxLayout: 
            TextInput:
                id: g_o_i
                hint_text: 'AT3G20770'
                multiline: False
                font_size: '25sp'

        BoxLayout:
            Button:
                text: "Submit"
                size_hint_x: 15
                on_press: 
                    root.submit_goi()
""")

class HelloVQCIA(Screen):
    pass

class VQCIA(Screen):

    # Connects the value in the TextInput widget to these
    # fields
    goi = ObjectProperty()

    def submit_goi(self):
        print self.goi.text

# The ScreenManager controls moving between screens
screen_manager = ScreenManager()

# Add the screens to the manager and then supply a name
# that is used to switch screens
screen_manager.add_widget(HelloVQCIA(name="screen_one"))
screen_manager.add_widget(VQCIA(name="screen_two"))

class VQCIAApp(App):
    def build(self):
        return screen_manager


dbApp = VQCIAApp()
dbApp.run()

我想做一个多屏应用,第一页是欢迎页面。第二页是用户可以提交表单的主页。谢谢。

【问题讨论】:

【参考方案1】:

在 kv 文件中,将 goi: g_o_i 移动到课后规则 &lt;VQCIA&gt;:

<VQCIA>:
    goi: g_o_i
    BoxLayout:
        orientation: "vertical"

【讨论】:

以上是关于kivy多屏找不到id的主要内容,如果未能解决你的问题,请参考以下文章

Kivy:[错误] [AudioGstplayer] 找不到资源

找不到 TableRecycleGridLayout 来修复 Kivy 代码

Kivy安装找不到GL/gl.h?

Kivy - windows - 根本找不到任何有价值的窗口提供程序

Kivy 错误 - 找不到任何有价值的窗口提供程序

切换驱动程序后 Kivy 找不到 Window 提供程序