如何通过python访问另一个屏幕上的更改小部件属性

时间:2017-10-05 12:29:56

标签: python-3.x widget kivy

我有3个屏幕的应用程序。对我们来说,我有一个无法使用的按钮,根据我在py文件中的IF语句中的某些条件,我无法使用该按钮。

这是我的main.kv

#:import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    ProjectScreen:
    ScopeScreen:

<MainScreen>:
    name: 'main'
    scope_button : scope_bttn
    BoxLayout:
        orientation: "vertical"
        spacing:10
        padding:10

#some wigedt..

        Button:
            on_press: app.root.current = 'project'
            text: 'Project Specification'
        Button:
            on_press: app.root.current = 'scope'
            text: 'Project Scope'
            disabled: True
            id: scope_bttn


<ProjectScreen>:
    name: 'project'      

        Button:
            on_release: app.root.current = 'main'
            text: 'Back'

        Button:
            text: 'Process'
            on_press: root.create_project()
            on_release: app.root.current = 'main'

<ScopeScreen>:
    name: 'scope'
#some widgets

这是我的main.py

的相关部分
class MainScreen(Screen):
    pass

class ProjectScreen(Screen):
    def create_project(self):
        If something True:
          here I would like to change property of scope_bttn.disable to true

class ScopeScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("main.kv")

class MainApp(App):
    def build(self):
        return presentation

ConfiguratorApp = MainApp()
ConfiguratorApp.run()

我知道我应该使用id但是我找不到合适的教程如何去做。我是kivy和python的新手。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

使用Kivy ObjectProperty 连接到kv文件中定义的id。有关详细信息,请参阅以下示例。

实施例

main.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty


class ScreenManagement(ScreenManager):
    main_scrn = ObjectProperty(None)


class MainScreen(Screen):
    scope_button = ObjectProperty(None)


class ProjectScreen(Screen):

    def create_project(self, root_manager):
        print("scope_button.disabled={}".format(root_manager.main_scrn.scope_button.disabled))
        if root_manager.main_scrn.scope_button.disabled:
            root_manager.main_scrn.scope_button.disabled = False
            print("scope_button.disabled={}".format(root_manager.main_scrn.scope_button.disabled))


class ScopeScreen(Screen):
    pass


class ConfiguratorApp(App):
    def build(self):
        return ScreenManagement()


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

configurator.kv

#:kivy 1.10.0
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

<ScreenManagement>:
    transition: FadeTransition()
    main_scrn: main_screen
    MainScreen:
        id: main_screen
    ProjectScreen:
    ScopeScreen:

<MainScreen>:
    name: 'main'
    scope_button : scope_bttn
    BoxLayout:
        orientation: "vertical"
        spacing:10
        padding:10

#some wigedt..

        Button:
            text: 'Project Specification'
            on_press: root.manager.current = 'project'
        Button:
            text: 'Project Scope'
            disabled: True
            id: scope_bttn
            on_press: root.manager.current = 'scope'


<ProjectScreen>:
    name: 'project'
    BoxLayout:
        orientation: "vertical"
        spacing:10
        padding:10

        Button:
            on_release: root.manager.current = 'main'
            text: 'Back'

        Button:
            text: 'Process'
            on_press: root.create_project(root.manager)
            on_release: root.manager.current = 'main'

<ScopeScreen>:
    name: 'scope'
    #some widgets

输出

enter image description here