2016-07-27 34 views
0

on_dropfileを複数のオブジェクトにバインドすることはできますか?それとも、常に唯一のバインドですか?kivy on_dropfile複数のバインディング

私はクラスが

class dropFile(Label): 
    def __init__(self, **kwargs): 
     super(dropFile, self).__init__(**kwargs) 
     Window.bind(mouse_pos=lambda w, p: setattr(helper, 'mpos', p)) 
     Window.bind(on_dropfile=self.on_dropfile) 

    def on_dropfile(self, *args): 
     print ("ding") 
     if (self.center_x - self.width/2 < helper.mpos[0] < self.center_x + self.width/2 and 
       self.center_x - self.height/2 < helper.mpos[1] < self.center_y + self.height/2): 
      print('dong') 
      self.text = str(args[1]) 

とKVに、私はちょうど

dropFile: 
    text: "Please drop file1" 
dropFile: 
    text: "Please drop file2" 

としてそれを使用するしかし、唯一の最初のフィールド上で動作して(それだけにドロップされたファイルを見てdecalredました」 Drop1をドロップしてください。それ以外の場合はドロップを受け取りますが、最初のオブジェクトのon_dropfileの機能のみをバインドしているかのように、2番目のフィールドの境界にあることを確認できません。

複数のオブジェクトに対してそれを実装する上品な方法はありますか?

答えて

0

今、それは私にとって意味があります。この場合、リストを作成して、好きな機能を実行してみてください。Window.on_dropfile

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.label import Label 
from kivy.core.window import Window 
from kivy.uix.boxlayout import BoxLayout 
Builder.load_string(''' 
<DropFile>: 
<Box>: 
    DropFile: 
     text: 'left' 
    DropFile: 
     text: 'right' 
''') 

class Box(BoxLayout): 
    pass 

class Test(App): 
    def build(self): 
     self.drops = [] 
     Window.bind(on_dropfile=self.handledrops) 
     return Box() 
    def handledrops(self, *args): 
     for i in self.drops: 
      i(*args) 

class Helper: 
    pass 

class DropFile(Label): 
    def __init__(self, **kwargs): 
     super(DropFile, self).__init__(**kwargs) 
     Window.bind(mouse_pos=lambda w, p: setattr(Helper, 'mpos', p)) 
     app = App.get_running_app() 
     app.drops.append(self.on_dropfile) 

    def on_dropfile(self, *args): 
     print ("ding") 
     if (self.center_x - self.width/2 < Helper.mpos[0] < self.center_x + self.width/2 and 
       self.center_x - self.height/2 < Helper.mpos[1] < self.center_y + self.height/2): 
      print('dong') 
      self.text = str(args[1]) 

Test().run() 

かなりうまく動作しているようです。 Windowの例外はAppクラスのハンドルon_dropfileに直接関連し、その他は対応する関数に含まれています。

+0

返信いただきありがとうございます。 私はクラスの仕組みを知っています。 ヘルパーは、マウスの位置を保持するための空の 'クラスヘルパー 'です。後でメインウィンドウに' Window.bind(mouse_pos = lambda w、p:setattr(helper、' mpos '、p))という行を移動しました。それは複数回実行されません。 'on_dropfile'はウィンドウイベントであり、残念なことにウィジェットイベントではないので、' on_release'のように動作しません。例外をスローするときにkvからバインドできません。 私はデスクトップアプリケーションに適しているようにPyQtに移りました(Kivyはそれはすべてのためだと言えるかもしれませんが、モバイル向けに合理化されています)。 しかし、質問はまだ有効です – sanki

+0

@ sanki私はそれを編集したので、それは働いた – KeyWeeUsr

+0

ありがとう、新しい編集のため、それははるかに優れているようです:-) – sanki

関連する問題