Jump to content
View in the app

A better way to browse. Learn more.

Tinkerunity

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Geschrieben

Hallo liebe TinkerUnity,

 

ich möchte gerne eine kleine Temperatur-Anzeige programmieren. Dazu verwenden ich einen Master-Brick und ein Temperatur-Bricklet. Programmiert habe ich mit Python und als GUI-Bibiliothek verwende ich Tkinter. Meine Frage ist nun, wie ich die Werte des Sensors automatisch und regelmässig updaten kann, ohne dass mein Programm in irgendeiner Schleife hängen bleibt?

 

Vielen Dank für Eure Hilfe!

 

Hier den Code denn ich bisher habe (er funktioniert, der Wert temp wird jedoch noch nicht geupdatet)

 

HOST = "localhost"
PORT = 4223
UID = "6xh9Dx"
UID_tempbr = "dyS"


from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_master import Master
from tinkerforge.bricklet_temperature import Temperature

import tkinter as tk
import time

def ipcon_entfernen():
    ipcon.disconnect()
    root.destroy()

if __name__ == "__main__":
    ipcon = IPConnection()
    master = Master(UID, ipcon)
    tempbr = Temperature(UID_tempbr, ipcon)

    ipcon.connect(HOST, PORT)

    voltage = master.get_stack_voltage()
    current = master.get_stack_current()


    root = tk.Tk()
    
    temp = tk.StringVar()
    temp.set(tempbr.get_temperature()/100)

    tk.Label(root, textvariable=temp).pack()

    root.protocol("WM_DELETE_WINDOW", ipcon_entfernen)
    root.mainloop()

Geschrieben

Hey,

 

also ich selber programmier nicht in Python aber guck dir mal folgendes in der API an. Dort kannst du Callbacks programmieren, also eine Art Listener, der Callback wird dann z.B. nur ausgelöst, wenn sich die Temperatur ändern.

 

http://www.tinkerforge.com/de/doc/Software/Bricklets/Temperature_Bricklet_Python.html#konfigurationsfunktionen-fur-callbacks

 

Viele Grüße

 

Ritchi

Geschrieben

Bei tk gibt es "after" um Funktionen nach einer bestimmten Zeit aufzurufen, als Alternative zu Callbacks. Also z.B.:

 

class App():
    def __init__(self):
        self.ipcon = IPConnection()
        self.master = Master(UID, self.ipcon)
        self.tempbr = Temperature(UID_tempbr, self.ipcon)

        self.root = tk.Tk()
        self.label = tk.Label(text="")
        self.label.pack()
        self.update()
        self.root.mainloop()

    def update(self):
        t = self.tempbr.get_temperature()/100.0
        self.label.configure(str(t))
        self.root.after(1000, self.update)

app=App()
app.mainloop()

 

(ungetestet)

Geschrieben
  • Autor

Hallo allerseits,

 

Callback war das perfekte Stichwort! Danke vielmals für die Nachhilfe, habe diesen Teil jeweils überlesen, da ich dachte das ist was für Profis :)

 

Danke an die TinkerUntity

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.