Funktionen via Button aufrufen

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
graceflotte
User
Beiträge: 25
Registriert: Samstag 8. März 2014, 12:17

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()
Benutzeravatar
darktrym
User
Beiträge: 784
Registriert: Freitag 24. April 2009, 09:26

Wie nennt sich denn die Programmiersprache die du da nutzt? Ähnelt Python entfernt.
„gcc finds bugs in Linux, NetBSD finds bugs in gcc.“[Michael Dexter, Systems 2008]
Bitbucket, Github
graceflotte
User
Beiträge: 25
Registriert: Samstag 8. März 2014, 12:17

Hä? :D

Das ist doch Python?!
Ich versuche via Kivy eine App zu programmieren. Was stimmt denn mit dem Code nicht?
EmaNymton
User
Beiträge: 174
Registriert: Sonntag 30. Mai 2010, 14:07

http://kivy.org/docs/api-kivy.event.html

Das Beispiel aus der offiziellen Doku sollte die meisten Anwendungen abdecken.
graceflotte
User
Beiträge: 25
Registriert: Samstag 8. März 2014, 12:17

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()
Antworten