Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 如何在Kivy中禁用小部件?_Python_User Interface_Widget_Kivy - Fatal编程技术网

Python 如何在Kivy中禁用小部件?

Python 如何在Kivy中禁用小部件?,python,user-interface,widget,kivy,Python,User Interface,Widget,Kivy,我阅读了Kivy教程,但找不到如何禁用小部件(例如,按钮) 我用functools.partial绑定foo 正确的参数是什么 它是禁用的,而不是禁用的 把它设为真 例如: from kivy.uix.button import Button from kivy.app import App from functools import partial class ButtonTestApp(App): def foo(self, instance, *args): in

我阅读了Kivy教程,但找不到如何禁用小部件(例如,按钮)

我用
functools.partial
绑定
foo

正确的参数是什么

  • 它是
    禁用的
    ,而不是
    禁用的
  • 把它设为真
  • 例如:

    from kivy.uix.button import Button
    from kivy.app import App
    from functools import partial
    
    class ButtonTestApp(App):
        def foo(self, instance, *args):
            instance.disabled = True
    
        def build(self):
            btn = Button()
            btn.bind(on_press=partial(self.foo, btn));
            return btn
    
    if __name__ == '__main__':
        ButtonTestApp().run()
    

    如果您使用的是kivy版本>=1.8,那么您可以只执行widget.disabled=True。如果在以前的版本中,您可以自己简单地管理禁用按钮,只需确保它不会对触摸做出反应,并在禁用时显示另一种外观。

    在下面的示例中
    MyButton
    遵循@quanon idea。它使用
    布尔属性
    来更改其
    背景颜色
    颜色
    。更重要的是,如果启用了self.enabled,它会在触摸屏上的
    中添加一个条件
    。如果向下触摸时没有
    ,则移动时没有
    ,向上触摸时没有
    ,\u向上触摸时没有
    ,\u按下时没有
    ,\u释放时没有
    。因此,我们可以考虑<代码>按钮<代码>禁用。
    我使用名称
    enabled
    而不是
    disabled
    ,通过使用Kivy 1.8.0的相同属性来避免将来可能出现的问题

    from kivy.app import App
    from kivy.uix.gridlayout import GridLayout
    from kivy.properties import BooleanProperty
    from kivy.uix.button import Button
    from kivy.lang import Builder
    
    Builder.load_string("""
    <Example>:
        cols: 3
        Button:
            text: "Disable right button"
            on_press: my_button.enabled = False
        Button:
            text: "enabled right button"
            on_press: my_button.enabled = True
        MyButton:
            id: my_button
            text: "My button"
            on_press: print "It is enabled"
    """)
    
    class MyButton(Button):
        enabled = BooleanProperty(True)
    
        def on_enabled(self, instance, value):
            if value:
                self.background_color = [1,1,1,1]
                self.color = [1,1,1,1]
            else:
                self.background_color = [1,1,1,.3]
                self.color = [1,1,1,.5]
    
        def on_touch_down( self, touch ):
            if self.enabled:
                return super(self.__class__, self).on_touch_down(touch)
    
    class Example(GridLayout):    
        pass
    
    class MyApp(App):
        def build(self):
            return Example()
    
    if __name__=="__main__":
        MyApp().run()
    
    从kivy.app导入应用
    从kivy.uix.gridlayout导入gridlayout
    从kivy.properties导入布尔属性
    从kivy.uix.button导入按钮
    从kivy.lang导入生成器
    生成器。加载\u字符串(“”)
    :
    科尔斯:3
    按钮:
    文本:“禁用右按钮”
    按下:my_button.enabled=False
    按钮:
    文本:“已启用的右按钮”
    按时:my_button.enabled=True
    我的按钮:
    我的按钮
    文本:“我的按钮”
    按:打印“已启用”
    """)
    类MyButton(按钮):
    enabled=布尔属性(True)
    def on_已启用(自身、实例、值):
    如果值:
    self.background_color=[1,1,1,1]
    self.color=[1,1,1,1]
    其他:
    self.background_color=[1,1,1,3]
    self.color=[1,1,1,5]
    def on_触控向下(自身,触控):
    如果自启用:
    返回超级(自我.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
    类示例(GridLayout):
    通过
    类别MyApp(应用程序):
    def生成(自):
    返回示例()
    如果名称=“\uuuuu main\uuuuuuuu”:
    MyApp().run()
    
    我添加了
    def foo(self,instance,*args):print instance.disabled instance.disabled=True
    并获得了print instance.disabled AttributeError:“Button”对象没有属性“disabled”
    disabled
    属性在版本1.8.0中引入。如果你想使用它,你需要实现你的框架。但是如何实现呢?他写道:“当前版本是1.7.2,于2013年8月4日发布。你从哪里得到新版本的?”?最新版本是开发版本。您可以在GitHub项目页面上找到它:
    from kivy.app import App
    from kivy.uix.gridlayout import GridLayout
    from kivy.properties import BooleanProperty
    from kivy.uix.button import Button
    from kivy.lang import Builder
    
    Builder.load_string("""
    <Example>:
        cols: 3
        Button:
            text: "Disable right button"
            on_press: my_button.enabled = False
        Button:
            text: "enabled right button"
            on_press: my_button.enabled = True
        MyButton:
            id: my_button
            text: "My button"
            on_press: print "It is enabled"
    """)
    
    class MyButton(Button):
        enabled = BooleanProperty(True)
    
        def on_enabled(self, instance, value):
            if value:
                self.background_color = [1,1,1,1]
                self.color = [1,1,1,1]
            else:
                self.background_color = [1,1,1,.3]
                self.color = [1,1,1,.5]
    
        def on_touch_down( self, touch ):
            if self.enabled:
                return super(self.__class__, self).on_touch_down(touch)
    
    class Example(GridLayout):    
        pass
    
    class MyApp(App):
        def build(self):
            return Example()
    
    if __name__=="__main__":
        MyApp().run()