Seite 1 von 1
TKinter und SimpleOSC
Verfasst: Sonntag 3. Mai 2009, 03:03
von mayx
Hey,
ich versuche ein OSC-Signal von simpleOSC mit Tkinter anzuzeigen.
Leider bekomme ich es nicht hin.
Hier mein Code:
Code: Alles auswählen
import osc
from gui import *
from Tkinter import *
def pushbutton(*msg):
print "the oscaddress is ", msg[0][0]
test.draw.place(x=50, y=50, anchor="nw")
def myTest():
""" a simple function that creates the necesary sockets and enters an enless
loop sending and receiving OSC
"""
osc.init()
osc.listen('192.168.1.3', 1337)
osc.bind(pushbutton, "/pushbutton/0/1")
test = Tk()
test.draw = Canvas(test, width="5i", height="5i")
test.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="black")
test.draw.pack(side=LEFT)
test.mainloop()
if __name__ == '__main__': myTest()
Was mach ich nur falls?
Es startet alles wunderbar und wenn ich dann die Taste drücke,
bricht die Verbindung zu OSC ab. Der MainLoop von Test scheint noch zu laufen.
Vielen Dank im Voraus!
Verfasst: Sonntag 3. Mai 2009, 08:55
von wuf
Hallo mayx
In der Datei 'oscAPI.py', welche sich im Modul 'simpleOSC' befindet die ich vom Web heruntergeladen und unter Python2.5 im Verzeichnis 'site_packages' installiert habe ist noch ein Fehler auf Zeile 111 vorhanden:
Code: Alles auswählen
print 'there was an error binding to ip %s and port %i , maybe the port is already taken by another process?' % (ipAddr. port)
Korrigiert:
Code: Alles auswählen
print 'there was an error binding to ip %s and port %i , maybe the port is already taken by another process?' % (ipAddr, port)
Bei mir zeigt sich ein Tk-Fenster mit einem kleinen schwarzen Rechteck in der oberen linken Ecke. Auf der Konsole wird folgende Meldung ausgegeben:
Code: Alles auswählen
there was an error binding to ip 192.168.1.3 and port 1337 , maybe the port is already taken by another process?
Das ist alles was ich bis jetzt festgestellt habe. Eine Verbindung zur im Skript verwendeten IP-Adresse mit Port wird scheinbar nicht aufgebaut.
Gruss wuf

Verfasst: Sonntag 3. Mai 2009, 11:57
von mayx
Hey,
OSC läuft wunderbar.
Erst wenn ich TKinter hinzufüge klappt es nicht mehr, um genau zu sein an der Stelle
Ich will eigentlich nur das Rechteck verschieben, damit man was sieht

Textausgabe funktioniert super. (*OSC-Signal:....*)
Nur wie schon geschrieben, wenn ich die Function aufrufe bricht das Programm ab. TKinter scheint noch weiter im Fenster zu laufen, die Konsole gibt aber nix mehr aus.
An der OSCApi und OSC hab ich nix geändert.
Verfasst: Sonntag 3. Mai 2009, 14:30
von wuf
Hallo mayx
Was ich in deinem Skript nicht verstehe ist folgende Zeile:
Wo befindet sich dieser Button (Schaltfläche)? Im Tk-Fenster sehe ich nichts, welches einer Schaltfläche gleicht.
Wird die Schaltfäche durch dein importiertes Modul:
erzeugt?
Für meinen Tests musste ich diesen import unwirksam machen da bei hier das Modul gui nicht existiert.
Tkinter würde ich nicht als Sternchen-Import implementieren sondern wir folgt:
Code: Alles auswählen
import osc
import Tkinter as tk
from gui import *
def pushbutton(*msg):
print "the oscaddress is ", msg[0][0]
test.draw.place(x=50, y=50, anchor="nw")
def myTest():
""" a simple function that creates the necesary sockets and enters an enless
loop sending and receiving OSC
"""
osc.init()
osc.listen('192.168.1.3', 1337)
osc.bind(pushbutton, "/pushbutton/0/1")
test = tk.Tk()
test.draw = tk.Canvas(test, width="5i", height="5i")
test.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="black")
test.draw.pack(side=tk.LEFT)
test.mainloop()
if __name__ == '__main__': myTest()
Gruss wuf

Verfasst: Sonntag 3. Mai 2009, 20:26
von mayx
hey wuf!
Der Button befindet sich auf meinen OSC-Controller

und entschuldige gui war noch nicht gebaut, da sollte eigentich der ganze TKinter kram rein.
Code: Alles auswählen
import osc
import Tkinter as tk
def pushbutton(*msg):
print "the oscaddress is ", msg[0][0]
test.draw.place(x=500, y=500, anchor="nw")
def myTest():
""" a simple function that creates the necesary sockets and enters an enless
loop sending and receiving OSC
"""
osc.init()
osc.listen('192.168.1.3', 1337)
osc.bind(pushbutton, "/pushbutton/0/1")
test = tk.Tk()
test.draw = tk.Canvas(test, width="5i", height="5i")
test.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="black")
test.draw.pack(side=tk.LEFT)
osc.sendMsg("/pushbutton/0/1", [444], "192.168.1.3", 1337)
test.mainloop()
if __name__ == '__main__':
myTest()
#osc.sendMsg("/pushbutton/0/1", [444], "192.168.1.3", 1337)
mit osc.sendMsg kann ein Signal gesendet werden, bei mir kommt der vom Controller.
und wie schon gesagt, wenn ich kein place mache geht es, aber bewegen tut sich natürlich dann nix
Code: Alles auswählen
import osc
import Tkinter as tk
def pushbutton(*msg):
print "function pushbutton -> the oscaddress is ", msg[0][0]
# test.draw.place(x=500, y=500, anchor="nw")
def myTest():
""" a simple function that creates the necesary sockets and enters an enless
loop sending and receiving OSC
"""
osc.init()
osc.listen('192.168.1.3', 1337)
osc.bind(pushbutton, "/pushbutton/0/1")
test = tk.Tk()
test.draw = tk.Canvas(test, width="5i", height="5i")
test.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="black")
test.draw.pack(side=tk.LEFT)
osc.sendMsg("/pushbutton/0/1", [444], "192.168.1.3", 1337)
test.mainloop()
if __name__ == '__main__':
myTest()
# osc.sendMsg("/pushbutton/0/1", [444], "192.168.1.3", 1337)
Verfasst: Sonntag 3. Mai 2009, 21:29
von wuf
Hallo mayx
Danke für deine Antwort. Noch ein Tipp ich würde für den Pythontext in deinen Beiträgen nicht den *Plaintext* Tag sondern den *Python* nehmen.
Hier habe ich dein Skript ein bisschen umgemodelt. Unten hast du ein Text-Pushbutton mit welchem du simulieren kannst, was passieren würde, wenn du ein Signal von deinem Controller empfangen würdes. Ich gehe richtig in der Annahme dass du das Canvas und nicht das kleine Rechteck auf der Canvas verschieben möchtest?
Hier der frisierte Code:
Code: Alles auswählen
# Skriptname maxy_simple_oec_01_01.py (wuf)
import osc
import Tkinter as tk
def pushbutton(*msg):
if msg != ():
print "function pushbutton -> the oscaddress is ", msg[0][0]
test.draw.place(x=500, y=500, anchor="nw")
def myTest():
""" a simple function that creates the necesary sockets and enters an
endless loop sending and receiving OSC
"""
osc.init()
osc.listen('192.168.1.3', 1337)
osc.bind(pushbutton, "/pushbutton/0/1")
test.draw = tk.Canvas(test, width="5i", height="5i", bg="lightgoldenrod")
test.draw.pack(side=tk.LEFT)
test.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="black")
osc.sendMsg("/pushbutton/0/1", [444], "192.168.1.3", 1337)
if __name__ == '__main__':
test = tk.Tk()
test.geometry("600x600+0+0")
#~~ Nur fuer Test
button_frame = tk.Frame(test)
button_frame.pack(side='bottom')
test_pushbutton = tk.Button(button_frame, text='Test-Push-Button',
bg='pink', fg='red', activebackground='pink', command=pushbutton)
test_pushbutton.pack(side='left')
myTest()
# osc.sendMsg("/pushbutton/0/1", [444], "192.168.1.3", 1337)
test.mainloop()
Wishing you good testing!
Gruss wuf

Verfasst: Sonntag 3. Mai 2009, 21:44
von mayx
Hey wuf,
ich hab mal ein Button in TKinter hinzugefügt, damit du es direkt testen kannst. Vielen Dank für die Hilfe!!!
Code: Alles auswählen
import osc
import Tkinter as tk
def pushbutton(*msg):
print "function pushbutton -> the oscaddress is ", msg[0][0]
# 1. Versuch
#test.draw.place(x=500, y=500, anchor="nw")
# 2. Versuch
#str = float(500)
#str = "%ri" % (str,)
#print str
#test.draw.move("thing", str, str)
def sendeMsgButton():
print "osc.sendMsg"
osc.sendMsg("/pushbutton/0/1", [444], "192.168.1.3", 1337)
def myTest():
osc.init()
osc.listen('192.168.1.3', 1337)
osc.bind(pushbutton, "/pushbutton/0/1")
test = tk.Tk()
test.draw = tk.Canvas(test, width="5i", height="5i")
test.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="black")
test.draw.pack(side=tk.LEFT)
test.bstart = tk.Button(test, text="send", command=sendeMsgButton)
test.bstart.pack()
test.mainloop()
if __name__ == '__main__':
myTest()
Ausgabe ohne place 4x Send-Button gedrückt:
osc.sendMsg
function pushbutton -> the oscaddress is /pushbutton/0/1
osc.sendMsg
function pushbutton -> the oscaddress is /pushbutton/0/1
osc.sendMsg
function pushbutton -> the oscaddress is /pushbutton/0/1
osc.sendMsg
function pushbutton -> the oscaddress is /pushbutton/0/1
Ausgabe mit place 4x Send-Button gedrückt:
osc.sendMsg
function pushbutton -> the oscaddress is /pushbutton/0/1
osc.sendMsg
osc.sendMsg
osc.sendMsg[/code]
Verfasst: Sonntag 3. Mai 2009, 21:49
von mayx
cool, du hast schon geantwortet

ich teste mal kurz ....
Verfasst: Sonntag 3. Mai 2009, 22:39
von mayx
sehr cool!
Code: Alles auswählen
import osc
import Tkinter as tk
def pushbutton(*msg):
if msg != ():
print "function pushbutton -> the oscaddress is ", msg[0][0]
# test.draw.place(x=50, y=50, anchor="nw")
str = float(1) #/ 1000.0
str = "%ri" % (str,)
test.draw.move("thing", str, str) # geht
# test.draw.place("thing", str, str) # geht nicht
def pushMsg():
osc.sendMsg("/pushbutton/0/1", [444], "192.168.1.3", 1337)
def myTest():
""" a simple function that creates the necesary sockets and enters an
endless loop sending and receiving OSC
"""
osc.init()
osc.listen('192.168.1.3', 1337)
osc.bind(pushbutton, "/pushbutton/0/1")
test.draw = tk.Canvas(test, width="5i", height="5i", bg="lightgoldenrod")
test.draw.pack(side=tk.LEFT)
test.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="black")
if __name__ == '__main__':
test = tk.Tk()
test.geometry("600x600+0+0")
#~~ Nur fuer Test
button_frame = tk.Frame(test)
button_frame.pack(side='bottom')
test_pushbutton = tk.Button(button_frame, text='Test-Push-Button',
bg='pink', fg='red', activebackground='pink', command=pushMsg)
test_pushbutton.pack(side='left')
myTest()
test.mainloop()
Und hast recht, ich hab versucht die Canvas zu verschieben, eigentlich wollte ich den Punkt verschieben. Move funktioniert, aber place nicht. Wie sind die Parameter. Danke.
Verfasst: Montag 4. Mai 2009, 07:55
von wuf
Hallo mayx
Habe dein Skript leicht an PEP-8 angepasst und ein wenig erweitert.Im Skripttext sind noch Informationen reingeschrieben betreffs verschieben von Tk-Widget-Objekten bzw. grafischen Canvas-Objekten. Wenn du verstanden hast was ich sagen wollte kannst du diese zusaetzlichen Informationen wieder entfernen.
ACHTUNG: Jetzt wird beim aktivieren der Test-Schaltfläche 'Test: Push-Button' beim ersten mal auch das Canvas-Widget um 10 Pixel in x- und y-Richtung verschoben plus das kleine Rechteck auf der Canvas gleichzeitig um 1 Inch. Bei weiterer Betätigung der Schaltfläche verschiebt sich nur noch das kleine Rechteck auf der Canvas um Inch-Inkremente.
Hier das modifizierte Skript:
http://paste.pocoo.org/show/115619/
Nachtrag: Sorry! Natürlich verschiebt sich das Canvas-Widget nicht um 10 sondern um 50 Pixels!
Gruss wuf
