Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 奇维罐头';无法使用ScreenManager获取文本输入_Python_Python 3.x_Kivy_Kivy Language_Kivymd - Fatal编程技术网

Python 奇维罐头';无法使用ScreenManager获取文本输入

Python 奇维罐头';无法使用ScreenManager获取文本输入,python,python-3.x,kivy,kivy-language,kivymd,Python,Python 3.x,Kivy,Kivy Language,Kivymd,我在做一个项目,我在用kivy 我想创建一个应用程序,我需要多个页面,因此我正在使用屏幕管理。 我还需要在其中一个页面中获取用户输入并保存它,因此我使用MDTextField获取文本,并使用按钮保存数据。 当我按下按钮时,应用程序应该从文本字段中获取数据并将其保存在带有sqlite3的文件中,但当我按下按钮时,它会给我一个非常奇怪的错误。 我试着在没有ScreenManager的情况下只重写应用程序的那一页,结果成功了。 我怎样才能使它与屏幕管理器一起工作 (如何使用MDTextField和Sc

我在做一个项目,我在用kivy

我想创建一个应用程序,我需要多个页面,因此我正在使用屏幕管理。 我还需要在其中一个页面中获取用户输入并保存它,因此我使用MDTextField获取文本,并使用按钮保存数据。 当我按下按钮时,应用程序应该从文本字段中获取数据并将其保存在带有sqlite3的文件中,但当我按下按钮时,它会给我一个非常奇怪的错误。 我试着在没有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
KeyError: 'account_link'
请注意,报告中说:

ID被添加到根小部件的ids字典中

糟糕的文档,因为它们在别处将“根部件”称为整个GUI的根。但在本例中,“根小部件”是定义
id
的规则的根。在您的情况下,这可能是
AddWindow
规则(由于
kv
代码段的缩进,无法100%确定)。如果是这种情况,则需要对GUI中出现的
AddWindow
实例的引用:

data = addwindow_instance.ids["account_link"].text
在没有看到更多代码的情况下,我只能猜测访问
AddWindow
实例的适当方法

随着一个完整的代码添加,我现在可以帮助你。以下是您的
add\u 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\u帐户\u名称”

这是我的更多代码:

# 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, 0, 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()
#屏幕
类主窗口(屏幕):
通过
类添加窗口(屏幕):
通过
类WindowManager(屏幕管理器):
通过
KV=”“”
WindowManager:
主窗口:
添加窗口:
:
名称:“主要”
MDRoundFlatButton:
正文:“添加”
pos_提示:{“center_x”:0.5,“center_y”:0.7}
新闻界:
app.root.current=“添加”
root.manager.transition.direction=“左”
MDRoundFlatButton:
文字:“显示”
pos_提示:{“center_x”:0.5,“center_y”:0.6}
新闻界:
app.root.current=“显示”
MDTEXT按钮:
正文:“账户”
pos_提示:{“center_x”:0.5,“center_y”:0.1}
新闻界:
app.root.current=“设置”
root.manager.transition.direction=“向上”
:
名称:“添加”
MDRaisedButton:
文本:“返回”
md_背景_颜色:0,0,0,1
pos_提示:{“x”:0.01,“y”:0.93}
发布时:
app.root.current=“main”
root.manager.transition.direction=“右”
MDTextField:
id:帐户链接
提示文字:“链接”
helper_text:“插入要从中输入网站的网站链接
此应用程序“
帮助器\u文本\u模式:“打开焦点”
线条颜色正常:app.theme\u cls.accent\u颜色
pos_提示:{“center_x”:0.5,“center_y”:0.8}
大小提示:无
宽度:1200
MDTextField:
id:“md\u帐户\u名称”
提示文字:“帐户”
helper_text:“插入要保存的帐户的名称”
帮助器\u文本\u模式:“打开焦点”
线条颜色正常:app.theme\u cls.accent\u颜色
pos_提示:{“center_x”:0.5,“center_y”:0.7}
大小提示:无
宽度:1200
MDTextField:
id:“md\u帐户\u昵称”
提示文字:“昵称”
helper_text:“在帐户中插入您的昵称”
帮助器\u文本\u模式:“打开焦点”
线条颜色正常:app.theme\u cls.accent\u颜色
pos_提示:{“center_x”:0.5,“center_y”:0.6}
大小提示:无
宽度:1200
MDTextField:
id:“md_电子邮件”
提示文字:“电子邮件”
helper_text:“插入您创建帐户时使用的电子邮件”
帮助器\u文本\u模式:“打开焦点”
线条颜色正常:app.theme\u cls.accent\u颜色
pos_提示:{“center_x”:0.5,“center_y”:0.5}
大小提示:无
宽度:1200
MDTextField:
id:“md_passwd”
提示文字:“密码”
helper_文本:“插入您的帐户密码”
帮助器\u文本\u模式:“打开焦点”
线条颜色正常:app.theme\u cls.accent\u颜色
pos_提示:{“center_x”:0.5,“center_y”:0.4}
大小提示:无
宽度:1200
MDFillRoundFlatButton:
案文:“提交”
pos_提示:{“center_x”:0.5,“center_y”:0.1}
按:app.add\u passwd()
"""
类应用程序(MDApp):
def生成(自):
self.title=“Safed”#应用程序的名称为“Safed”:“保存”+“保存”
self.theme_cls.theme_style=“深色”#浅色
self.theme\u cls.primary\u palete=“蓝色”
返回生成器。加载字符串(KV)
def添加路径(自身):
account\u link=AddWindow\u istance.ids[“account\u link”]。text
account\u name=self.root.ids[“md\u account\u name”].text
account\u昵称=self.root.ids[“md\u account\u昵称”].text
email=self.root.ids[“md_email”].text
passwd=self.root.ids[“md_passwd”].text
#试验
打印(帐户链接)
打印(帐户名称)
打印(帐户\昵称)
打印(电子邮件)
打印(passwd)
如果名称=“\uuuuu main\uuuuuuuu”:
App().run()

感谢您的帮助,非常感谢。但是我可以问你更多的信息吗?我对kivy非常陌生,我不太擅长理解文档。例如,我应该将和实例添加到AddWindow(如何像您的示例中那样创建che AddWindow\u实例)?提前感谢您的帮助正如我所说的,如果不看更多的代码,我真的无法提出任何进一步的建议。请发布一个。好的,我会发布更多类似答案的代码,让你更好地理解我的问题谢谢你的帮助我终于解决了我的问题谢谢你!!!这对我来说真的很重要。非常感谢!
# 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, 0, 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()