Page 1 of 1

Python Revpimodio2 und Tkinter: rvi.mainloop() und mainloop()

Posted: 15 Apr 2021, 14:08
by Maxim3
Hallo Zusammen,

ich möchte ein Python Programm schreiben, welches eine Variable in einem Fenster anzeigt und diese automatisch aktualisiert, nachdem ein Eingangssignal (eine Lichtschranke, reg_event RISING) erhalten wird. Das Problem ist, dass ich einmal die rvi.mainloop() und zum anderen die mainloop() für das tkinter-Fenster habe. Wenn ich das Programm starte öffnet sich kein Fenster aber das Signal wird erkannt (in Python Shell wird die Variable ausgegeben und nach jedem Flankenanstieg aufsummiert). Lasse ich "rpi.mainloop()" weg, erscheint das Fenster aber das Signal wird nicht erkannt.

Hier mein Code:
-----------------------------
import revpimodio2
from tkinter import *

x = 0

root = Tk()
root.title("Testfenster")
root.geometry("400x600")

test_label= Label(root, text=(x))
test_label.pack()

def update():
test_label["text"]=x+1
root.after(100, update)

def myevent(ioname, iovalue):
global x
x += 1

rpi=revpimodio2.RevPiModIO(autofresh=True)
rpi.io.DInBit_1.reg_event(myevent, edge=revpimodio2.RISING)
rpi.mainloop()

update()
mainloop()
-----------------------
Hat jemand eine Idee wie ich das gelöst bekomme?

Ich muss dazu sagen, dass ich kein Python bzw. generell kein Programmierer bin. Mit dem Raspberry Pi hat das funktioniert, da reicht die "GPIO.add_event_detect(1, GPIO.RISING, callback=myevent) Funktion, also ohne "rpi.mainloop()".

Liebe Grüße und Danke im Voraus!

Re: Python Revpimodio2 und Tkinter: rvi.mainloop() und mainloop()

Posted: 18 Apr 2021, 10:06
by RevPiModIO
Hi Maxim3!

Versuche mal bei deinem "rpi.mainloop()" noch den Parameter "blocking=False" anzugeben "rpi.mainloop(blocking=False)", dann läuft das Eventsystem im Hintergrund.

Am Ende von deinem Code, also nach dem mainloop von TK-Inter auf jeden Fall noch ein "rpi.exit()" einsetzen. Damit wird das Eventsystem beendet und alle IOs synchronisiert, bevor das Programm am Ende ist ;)

Gruß
Sven

Re: Python Revpimodio2 und Tkinter: rvi.mainloop() und mainloop()

Posted: 19 Apr 2021, 11:25
by Maxim3
Hallo Sven,

danke für die schnelle Antwort!

Das hat geholfen, es funktioniert. :)

Re: Python Revpimodio2 und Tkinter: rvi.mainloop() und mainloop()

Posted: 06 Aug 2021, 21:00
by LEOMENA
Hi Sven,

Similar situation here, I got around it by using the following:
threading.Thread(target=rpi.mainloop).start()

It works fine, however I am concerned about the little memory left on the Revpi to cause me trouble, Do you see a problem with my code?

Also, Should I still use the rpi.exit() command right after my Tkinter line: root.mainloop() ??

Thanks!