make views combineable

This commit is contained in:
Jannes Magnusson
2025-10-17 15:26:45 +02:00
parent 2093e43611
commit a1de093d2c
7 changed files with 122 additions and 73 deletions

View File

@@ -1,2 +1,3 @@
from .number import NumberView
from .circle import CircleView
from .circle import CircleView
from .combined_view import CombinedView

View File

@@ -1,32 +1,18 @@
from tkinter import ttk, Frame, Canvas
from PIL import Image, ImageDraw
from tkinter import PhotoImage
import io
from PIL import Image
class View:
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)
def __init__(self, parent, size, center, **kwargs):
self.size = size
self.center = center
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.ui = None
self.init_ui(parent)
self.update_weight(0.0)
self.refresh(0.0)
def _init_ui(self):
def init_ui(self, parent):
pass
def _init_im(self):
@@ -35,17 +21,3 @@ class View(Frame):
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)

View File

@@ -6,12 +6,12 @@ from .base import View
class CircleView(View):
def _init_ui(self):
self.target_frame = tk.Frame(self.actions)
self.target_frame.pack()
self.target_label = ttk.Label(self.target_frame, text="Target (g)")
def init_ui(self, parent):
self.ui = tk.Frame(parent)
self.ui.pack()
self.target_label = ttk.Label(self.ui, text="Target (g)")
self.target_label.pack(side=tk.LEFT)
self.target = ttk.Entry(self.target_frame)
self.target = ttk.Entry(self.ui)
self.target.insert(0, 100.0)
self.target.pack(side=tk.LEFT)

View File

@@ -0,0 +1,69 @@
import io
import tkinter as tk
from tkinter import Frame, Canvas, ttk, PhotoImage
from PIL import Image, ImageChops
from ..config import DISPLAY_TYPES
from . import NumberView, CircleView
class CombinedView(tk.Frame):
def __init__(self, parent,
tare_command=None, calibrate_command=None,
**kwargs):
super().__init__(parent, **kwargs)
self.views = []
self.tare_command = tare_command
self.calibrate_command = calibrate_command
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.im_size = (168, 144)
self.center = (168 // 2, 144 // 2)
self.canvas = Canvas(self, width=168, height=144, background='white',
highlightthickness=1, highlightbackground="black")
self.canvas.pack()
def update_views(self, selected_types: DISPLAY_TYPES):
for v in self.views:
if v.ui is not None:
v.ui.destroy()
self.views.clear()
if selected_types & DISPLAY_TYPES.NUMBER:
number_view = NumberView(self.actions, self.im_size, self.center)
self.views.append(number_view)
if selected_types & DISPLAY_TYPES.CIRCLE:
circle_view = CircleView(self.actions, self.im_size, self.center)
self.views.append(circle_view)
def refresh(self, weight: float):
ims = []
for view in self.views:
im = view.update_weight(weight)
ims.append(im)
self.canvas.delete("all")
# Combine images by logical_and
if ims:
combined_im = ims[0]
for im in ims[1:]:
combined_im = ImageChops.logical_and(combined_im, im)
# Convert PIL image to bytes
buffer = io.BytesIO()
combined_im.save(buffer, format='PNG')
buffer.seek(0)
# Load into PhotoImage and display on canvas
self.photo = PhotoImage(data=buffer.getvalue())
self.canvas.create_image(0, 0, anchor="nw", image=self.photo)

View File

@@ -1,4 +1,4 @@
from PIL import Image, ImageDraw
from PIL import ImageDraw
from .base import View