Kivy无法使用ScreenManager获得文本输入

如何解决Kivy无法使用ScreenManager获得文本输入

我正在研究一个项目,并且正在使用kivy。

我想创建一个应用程序,并且需要多个页面,所以我正在使用 ScreenManages 。 我还需要在其中一个页面中输入“用户输入”并将其保存,因此我使用了 MDTextField 来获取文本和一个按钮来保存数据。 当我按下按钮时,应用程序应该从文本字段中获取数据,并使用sqlite3将其保存在文件中,但是当我按下按钮时,它给了我一个非常奇怪的错误。 我尝试只重写应用程序的该页面而不使用ScreenManager,并且它可以正常工作。 如何使它也可以与ScreenManager一起使用?

如何使用MDTextField和ScreenManager获取用户输入

我将向您展示一些代码行,以使您更好地理解:

这是Kivy代码:

<AddWindow>:
    name: "add"
        
MDTextField:
    id: account_link
    hint_text: "Link"
    helper_text: "Insert the Link of the WebSite to enter in the website from this app"
    helper_text_mode: "on_focus"
    line_color_normal: app.theme_cls.accent_color
    pos_hint: {"center_x": 0.5,"center_y": 0.8}
    size_hint_x: None
    width: 1200

这是从“文本字段”中获取数据的代码(当用户按下“提交”按钮时执行的那部分代码):

data = self.root.ids["account_link"].text

这是我按下按钮时遇到的错误:

data = self.root.ids["account_link"].text
KeyError: 'account_link'

解决方法

请注意,documentation表示:

id被添加到根小部件的id字典中。

措辞不佳的文档,因为它们在其他地方将“ root widget”称为整个GUI的根。但是在这种情况下,“根小部件”是定义ids的规则的根。在您的情况下,这可能是AddWindow规则(由于您的kv代码段缩进,因此无法100%确定)。如果是这种情况,那么您需要引用出现在GUI中的AddWindow实例:

data = addwindow_instance.ids["account_link"].text

没有看到更多的代码,我只能猜测访问AddWindow实例的适当方法。

通过添加完整的代码,我现在可以为您提供帮助。这是您的add_passwd()方法的修改版本:

def add_passwd(self):

    # get a reference to the AddWindow Screen
    addwindow_instance = self.root.get_screen('add')

    # use that instance to access the MDTextFields
    account_link = addwindow_instance.ids["account_link"].text
    account_name = addwindow_instance.ids["md_account_name"].text
    account_nickname = addwindow_instance.ids["md_account_nickname"].text
    email = addwindow_instance.ids["md_email"].text
    passwd = addwindow_instance.ids["md_passwd"].text

    #TEST
    print(account_link)
    print(account_name)
    print(account_nickname)
    print(email)
    print(passwd)

请注意,这还需要对kv进行一些更正。无论您在何处:

id: "some_id"

应更改为:

id: some_id

一个例子是id: "md_account_name"

,

这是我的更多代码:

# Screens
class MainWindow(Screen):
    pass
class AddWindow(Screen):
    pass
class WindowManager(ScreenManager):
    pass

KV = """
WindowManager:
MainWindow:
AddWindow:

<MainWindow>:
    name: "main"

MDRoundFlatButton:
    text: "Add"
    pos_hint: {"center_x": 0.5,"center_y": 0.7}
    on_press: 
        app.root.current = "add"
        root.manager.transition.direction = "left"
    
MDRoundFlatButton:
    text: "Show"
    pos_hint: {"center_x": 0.5,"center_y": 0.6}
    on_press: 
        app.root.current = "show"
    
MDTextButton:
    text: "Account"
    pos_hint: {"center_x": 0.5,"center_y": 0.1}
    on_press: 
        app.root.current = "settings"
        root.manager.transition.direction = "up"

<AddWindow>:
    name: "add"
    MDRaisedButton:
    text: "BACK"
    md_bg_color: 0,1
    pos_hint: {"x": 0.01,"y": 0.93}
    on_release: 
        app.root.current = "main"
         root.manager.transition.direction = "right"
            
MDTextField:
    id: account_link
    hint_text: "Link"
    helper_text: "Insert the Link of the WebSite to enter in the website from 
    this app"
    helper_text_mode: "on_focus"
    line_color_normal: app.theme_cls.accent_color
    pos_hint: {"center_x": 0.5,"center_y": 0.8}
    size_hint_x: None
    width: 1200

MDTextField:
    id: "md_account_name"
    hint_text: "Account"
    helper_text: "Insert the Name of the Account You Want to Save"
    helper_text_mode: "on_focus"
    line_color_normal: app.theme_cls.accent_color
    pos_hint: {"center_x": 0.5,"center_y": 0.7}
    size_hint_x: None
    width: 1200
    
MDTextField:
    id: "md_account_nickname"
    hint_text: "Nickname"
    helper_text: "Insert the Nickname You Have in the Account"
    helper_text_mode: "on_focus"
    line_color_normal: app.theme_cls.accent_color
    pos_hint: {"center_x": 0.5,"center_y": 0.6}
    size_hint_x: None
    width: 1200
    
MDTextField:
    id: "md_email"
    hint_text: "Email"
    helper_text: "Insert the Email You Created the Account with"
    helper_text_mode: "on_focus"
    line_color_normal: app.theme_cls.accent_color
    pos_hint: {"center_x": 0.5,"center_y": 0.5}
    size_hint_x: None
    width: 1200
    
MDTextField:
    id: "md_passwd"
    hint_text: "Password"
    helper_text: "Insert Your Password of the Account"
    helper_text_mode: "on_focus"
    line_color_normal: app.theme_cls.accent_color
    pos_hint: {"center_x": 0.5,"center_y": 0.4}
    size_hint_x: None
    width: 1200
    
MDFillRoundFlatButton:
    text: "Submit"
    pos_hint: {"center_x": 0.5,"center_y": 0.1}
    on_press: app.add_passwd()
"""

class App(MDApp):

def build(self):
    self.title = "Safed" #The Name of the App is "Safed": "Save" + "Saved"
    self.theme_cls.theme_style = "Dark" # Light
    self.theme_cls.primary_palette = "Blue"
    return Builder.load_string(KV)

def add_passwd(self):
    account_link = AddWindow_istance.ids["account_link"].text
    account_name = self.root.ids["md_account_name"].text
    account_nickname = self.root.ids["md_account_nickname"].text
    email = self.root.ids["md_email"].text
    passwd = self.root.ids["md_passwd"].text

    #TEST
    print(account_link)
    print(account_name)
    print(account_nickname)
    print(email)
    print(passwd)


if __name__ == "__main__":
    App().run()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-