52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from tkinter import ttk, Frame, Canvas
|
|
from PIL import Image, ImageDraw
|
|
from tkinter import PhotoImage
|
|
import io
|
|
|
|
class View(Frame):
|
|
|
|
def __init__(self, *args, tare_command=None, calibrate_command=None, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.actions = Frame(self)
|
|
self.actions.pack()
|
|
self.calibrate_button = ttk.Button(self.actions, text="Calibrate", command=calibrate_command)
|
|
self.calibrate_button.pack()
|
|
self.tare_button = ttk.Button(self.actions, text="Tare", command=tare_command)
|
|
self.tare_button.pack()
|
|
|
|
self.size = (168, 144)
|
|
self.center = (168 // 2, 144 // 2)
|
|
self.bkg_im = self._init_im()
|
|
self._init_ui()
|
|
|
|
self.canvas = Canvas(self, width=168, height=144, background='white',
|
|
highlightthickness=1, highlightbackground="black")
|
|
self.canvas.pack()
|
|
|
|
self.refresh(0.0)
|
|
|
|
def _init_ui(self):
|
|
pass
|
|
|
|
def _init_im(self):
|
|
return Image.new('1', self.size, 'white')
|
|
|
|
def update_weight(self,
|
|
weight: float) -> None:
|
|
raise NotImplementedError()
|
|
|
|
def refresh(self, weight: float):
|
|
# draw weight on bkg_im
|
|
im = self.update_weight(weight)
|
|
|
|
# Convert PIL image to bytes
|
|
buffer = io.BytesIO()
|
|
im.save(buffer, format='PNG')
|
|
buffer.seek(0)
|
|
|
|
# Load into PhotoImage and display on canvas
|
|
self.photo = PhotoImage(data=buffer.getvalue())
|
|
self.canvas.delete("all")
|
|
self.canvas.create_image(0, 0, anchor="nw", image=self.photo)
|