【问题标题】:Kivy - on_press bind event to btn not workKivy - on_press 绑定事件到 btn 不起作用
【发布时间】:2021-02-16 17:45:36
【问题描述】:
import re
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from engine.common.modifycation import AlignedTextInput
from kivy.uix.popup import Popup
from kivy.uix.colorpicker import ColorPicker

class EditorOperationAdd():

    def __init__(self, **kwargs):
        super(EditorOperationAdd, self).__init__(**kwargs)

        content = BoxLayout(orientation="vertical")
        clr_picker = ColorPicker()

        infoBtn = Button(text='Add new button')
        content.add_widget(Label(text='color'))
        content.add_widget(clr_picker)
        content.add_widget(Label(text='Text'))
        #content.add_widget(AlignedTextInput(text='My Button', halign="middle", valign="center"))
       
        self.popup = Popup(title='Add new button editor box', content=content, auto_dismiss=False)

        content.add_widget(infoBtn)
        infoBtn.bind(on_press=self.localCall)
        clr_picker.bind(color=self.on_color)
        #infoBtn.bind(on_press=self.operationAdd)
        
        self.popup.open()

        infoBtn2 = Button(text='Add new button 2', on_press=self.localCall )
        content.add_widget(infoBtn2)
        ####################################################
        # Operation `Add`
        ####################################################

        self.addNewButtonGUIOperation()

    def localCall(self, obj, value):
        print("works man", obj)
        print("works man", value)

    # To monitor changes, we can bind to color property changes
    def on_color(self, instance, value):
        print( "RGBA = ", str(value) ) #  or instance.color
        print( "HSV = ", str(instance.hsv))
        print( "HEX = ", str(instance.hex_color))

    def operationAdd(self):
        print("Operation add.")
        self.popup.dismiss()

    def addNewButtonGUIOperation(self):
        print("empty")
        # self.localCall()

有什么建议吗?

【问题讨论】:

    标签: python-3.x kivy


    【解决方案1】:

    我认为你需要在添加小部件之前绑定,试试这个:

    infoBtn2 = Button(text='Add new button 2' )
    infoBtn2.bind(on_press=self.localCall())
    content.add_widget(infoBtn2)
    

    【讨论】:

      【解决方案2】:

      您的localCall 方法与按钮按下提供的参数不匹配。它只传递Button 实例(除非您另外指定)。只需将您的 localCall 方法更改为:

      def localCall(self, button):
          print("works man", button)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-07
        相关资源
        最近更新 更多