from tkinter import Tk, Variable, Label, Button from snap7 import Client, Area from snap7.util import get_real, get_int class PLC: def __init__(self): self.plc = Client() self.plc.connect("192.168.0.10", 0, 1) def __enter__(self): return self def __exit__(self, exc_type, exc, tb): self.plc.disconnect() self.plc.destroy() def update_variables(self, root: Tk, mm: Variable, raw: Variable): val_raw = get_int(self.plc.read_area(Area.PE, 0, 4, 2), 0) val_mm = get_real(self.plc.read_area(Area.MK, 0, 50, 4), 0) raw.set(f"Rohwert: {val_raw:>5d}") mm.set(f"Höhe: {val_mm:>4.1f} mm") root.after(10, self.update_variables, root, mm, raw) def main(plc: PLC): root = Tk() root.title("Höhenmesser") mm = Variable(root, 0.0) raw = Variable(root, 0) Label(root, textvariable=mm, font=("JetBrains Mono", 36)).pack() Label(root, textvariable=raw, font=("JetBrains Mono", 36)).pack() Button(root, text="Verlassen", command=root.destroy, font=("Arial", 24)).pack() root.after(500, plc.update_variables, root, mm, raw) root.mainloop() if __name__ == "__main__": with PLC() as plc: main(plc)