Seite 1 von 1

Funktionen via Button aufrufen

Verfasst: Donnerstag 25. September 2014, 18:15
von graceflotte
Moin Leute,

ich bräuchte mal wieder eure Hilfe.
Ich bin dabei eine App zu programmieren nur leider bekomm ich es nicht hin, die Argumente mit zu übergeben.
Wenn ich self.sendudp(ip,msg) benutze führt er die Funktion bereits beim starten aus. Hat da jemand eine Idee ?

Code: Alles auswählen

from kivy.app             import App
from kivy.uix.button      import Button
from kivy.uix.gridlayout  import GridLayout
import socket

class AutohomeMobile(App):

  def sendudp(self,widget,ip,msg):
    print '---------------------------------------------------------------'
    sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    sock.sendto( msg , (ip,12345) )
  
  def build(self):
    b_light    = Button(text='lampe',font_size=15,on_press=self.sendudp) #Wie gebe ich die Argumente mit ? :/
    
    layout_master.add_widget(b_light)

    return  layout_master
    
    
if __name__ == '__main__':
  AutohomeMobile().run()

Re: Funktionen via Button aufrufen

Verfasst: Donnerstag 25. September 2014, 20:08
von darktrym
Wie nennt sich denn die Programmiersprache die du da nutzt? Ähnelt Python entfernt.

Re: Funktionen via Button aufrufen

Verfasst: Donnerstag 25. September 2014, 20:10
von graceflotte
Hä? :D

Das ist doch Python?!
Ich versuche via Kivy eine App zu programmieren. Was stimmt denn mit dem Code nicht?

Re: Funktionen via Button aufrufen

Verfasst: Donnerstag 25. September 2014, 20:37
von EmaNymton
http://kivy.org/docs/api-kivy.event.html

Das Beispiel aus der offiziellen Doku sollte die meisten Anwendungen abdecken.

Re: Funktionen via Button aufrufen

Verfasst: Donnerstag 25. September 2014, 21:48
von graceflotte
Super, danke. Mit bind(on_press=lambda x: self.on_event(None)) hats funktioniert.
Ich versteh nur leider nicht ganz, was ich da mache und was die anderen Funktionen bewirken.
Könnte mir das jemand näher bringen?

Code: Alles auswählen

from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.uix.button import Button
from functools import partial


class DemoBox(BoxLayout):
    """
    This class demonstrates various techniques that can be used for binding to
    events. Although parts could me made more optimal, advanced Python concepts
    are avoided for the sake of readability and clarity.
    """
    def __init__(self, **kwargs):
        super(DemoBox, self).__init__(**kwargs)
        self.orientation = "vertical"

        # Next, we bind to a standard property change event. This typically
        # passes 2 arguments: the object and the value
        btn2 = Button(text="Normal binding to a property change")
        btn2.bind(state=self.on_property)

        # Here we use anonymous functions (a.k.a lambdas) to perform binding.
        # Their advantage is that you can avoid declaring new functions i.e.
        # they offer a concise way to "redirect" callbacks.
        btn3 = Button(text="Using anonymous functions.")
        btn3.bind(on_press=lambda x: self.on_event(None))

        # You can also declare a function that accepts a variable number of
        # positional and keyword arguments and use introspection to determine
        # what is being passed in. This is very handy for debugging as well
        # as function re-use. Here, we use standard event binding to a function
        # that accepts optional positional and keyword arguments.
        btn4 = Button(text="Use a flexible function")
        btn4.bind(on_press=self.on_anything)

        # Lastly, we show how to use partial functions. They are sometimes
        # difficult to grasp, but provide a very flexible and powerful way to
        # reuse functions.
        btn5 = Button(text="Using partial functions. For hardcores.")
        btn5.bind(on_press=partial(self.on_anything, "1", "2", monthy="python"))

        for but in [btn, btn2, btn3, btn4, btn5]:
            self.add_widget(but)

    def on_property(self, obj, value):
        print("Typical property change from", obj, "to", value)

    def on_anything(self, *args, **kwargs):
        print('The flexible function has *args of', str(args),
            "and **kwargs of", str(kwargs))


class DemoApp(App):
    def build(self):
        return DemoBox()

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