Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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 将参数传递给其他属性_Python_Kivy - Fatal编程技术网

Python 将参数传递给其他属性

Python 将参数传递给其他属性,python,kivy,Python,Kivy,我有两个屏幕。 盒子上有一个带名字的按钮。当我点击按钮时,我想将屏幕切换到ChangeText,并在那里看到名称。单击按钮后,Box.name的文本应为文本输入文本。 我找不到解决办法 kv文件 <ChangeText>: BoxLayout: TextInput: id: txt multiline: False text: Box.name Button:

我有两个屏幕。 盒子上有一个带名字的按钮。当我点击按钮时,我想将屏幕切换到ChangeText,并在那里看到名称。单击按钮后,Box.name的文本应为文本输入文本。 我找不到解决办法

kv文件

<ChangeText>:
    BoxLayout:
        TextInput:
            id: txt
            multiline: False
            text: Box.name
        Button:
            text: "Save"
            on_release: Box.name = txt.text

<Box>:
    BoxLayout:
        Button:
            text: root.name
            on_press: root.change_text(root.name)
class ChangeText(Screen):
    pass

class Box(Screen):
    name = StringProperty("sometext")

    def change_text(label):
        sm.current = 'changetext'
        ### ?!?!? ###

所以我找到了一个解决办法,可能是不稳定的,但目前有效。 花了我几个小时

我刚刚将eveyr属性声明为全局变量。然后我编写了一个全局导出函数,覆盖所有全局变量。因此,每当我切换屏幕时,我都会执行导出(道具)。现在,上一个视图中的所有属性都将导出。我需要更新每个屏幕的所有属性。因此,每个屏幕都有update()。Export()由for循环扩展到所有屏幕,调用update()

kv

<Team>:
    id: team
    TextInput:
        id: team_name
        text: root.home_name
        multiline: False
    Button:
        text: "Save"
        on_press: root.save(team_name.text)

<Box>:
     Button:
         id: btn_home_name
         text: root.home_name
         on_press: root.load_team()
:
id:团队
文本输入:
id:团队名称
文本:root.home\u名称
多行:False
按钮:
文本:“保存”
按:root.save(团队名称.text)
:
按钮:
id:btn_home_name
文本:root.home\u名称
按:root.load\u team()
问题-属性、名称 使用Kivy ScreenManager时,不要声明名为name的变量/属性,因为它是用于命名/标识屏幕的保留字

在ScreenManager中必须唯一的屏幕名称。这 ScreenManager.current的名称

name是StringProperty,默认为“”

解决方案 解决办法如下:

  • 将类级属性、
    name
    重命名为
    home\u name
    或除
    name
  • 向每个屏幕添加
    id
  • 使用
    root.manager.ids
    引用另一个屏幕中声明的窗口小部件
  • sm.current
    替换为
    self.manager.current
    ,因为默认情况下,每个屏幕都有一个属性管理器,为您提供所用屏幕管理器的实例
  • 例子 main.py 试验电压(千伏)
    #:kivy 1.11.0
    :
    方框:
    id:盒子
    名称:“盒子”
    更改文本:
    id:changeText
    名称:“changetext”
    :
    盒子布局:
    文本输入:
    id:txt
    多行:False
    文本:root.manager.ids.box.home\u name
    按钮:
    文本:“保存”
    发布时:root.manager.ids.box.home\u name=txt.text
    :
    盒子布局:
    按钮:
    文本:root.home\u名称
    按:root.change\u text()
    
    输出

    我希望有一种更简单的方法。
    <Team>:
        id: team
        TextInput:
            id: team_name
            text: root.home_name
            multiline: False
        Button:
            text: "Save"
            on_press: root.save(team_name.text)
    
    <Box>:
         Button:
             id: btn_home_name
             text: root.home_name
             on_press: root.load_team()
    
    name
    
    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.properties import StringProperty
    
    
    class ScreenManagement(ScreenManager):
        pass
    
    
    class ChangeText(Screen):
        pass
    
    
    class Box(Screen):
        home_name = StringProperty("sometext")
    
        def change_text(self):
            self.manager.current = 'changetext'
    
    
    class TestApp(App):
    
        def build(self):
            return ScreenManagement()
    
    
    if __name__ == "__main__":
        TestApp().run()
    
    #:kivy 1.11.0
    
    <ScreenManagement>:
        Box:
            id: box
            name: 'box'
    
        ChangeText:
            id: changeText
            name: 'changetext'
    
    <ChangeText>:
        BoxLayout:
            TextInput:
                id: txt
                multiline: False
                text: root.manager.ids.box.home_name
            Button:
                text: "Save"
                on_release: root.manager.ids.box.home_name = txt.text
    
    <Box>:
        BoxLayout:
            Button:
                text: root.home_name
                on_press: root.change_text()