From c0f4f1210b9686fb9e79935673ca1b8a4e9bf7c8 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 27 Dec 2019 20:58:02 +0100 Subject: Proper change notifications for widget repeater and selection button/frame Fixes #41 --- jfr_playoff/gui/frames/__init__.py | 13 +++++++++---- jfr_playoff/gui/variables.py | 14 ++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) (limited to 'jfr_playoff') diff --git a/jfr_playoff/gui/frames/__init__.py b/jfr_playoff/gui/frames/__init__.py index 052e832..cca19a5 100644 --- a/jfr_playoff/gui/frames/__init__.py +++ b/jfr_playoff/gui/frames/__init__.py @@ -7,8 +7,8 @@ import tkinter as tk from tkinter import ttk import tkMessageBox -from ..variables import NotifyStringVar, NotifyIntVar -from ..variables import NotifyBoolVar, NotifyNumericVar, NumericVar +from ..variables import BoolVar +from ..variables import NotifyStringVar, NotifyNumericVar, NumericVar def setPanelState(frame, state): for child in frame.winfo_children(): @@ -100,6 +100,7 @@ class WidgetRepeater(tk.Frame): self.addButton.grid( row=len(self.widgets)+headeridx, column=0, columnspan=1, sticky=tk.W+tk.N) + self.event_generate('<>', when='tail') def _renderHeader(self): if self.headers: @@ -263,7 +264,7 @@ class WidgetSelectionFrame(ScrollableFrame): addBtn.pack(side=tk.BOTTOM) def renderContent(self, container): - self.value = NotifyIntVar() + self.value = tk.IntVar() for idx, widget in enumerate(self.widgets): (ttk.Radiobutton( container, variable=self.value, value=idx, @@ -298,6 +299,7 @@ class SelectionButton(ttk.Button): kwargs['command'] = self._choosePositions if self.prompt is None: self.prompt = self.defaultPrompt + self._trackChanges = False ttk.Button.__init__(self, *args, **kwargs) self.setPositions([]) @@ -307,6 +309,8 @@ class SelectionButton(ttk.Button): text='[wybrano: %d]' % (len(values))) if self.callback is not None: self.callback(values) + if self._trackChanges: + self.event_generate('<>', when='tail') def _choosePositions(self): options = self.getOptions() @@ -319,6 +323,7 @@ class SelectionButton(ttk.Button): dialog.title(self.title) dialog.grab_set() dialog.focus_force() + self._trackChanges = True selectionFrame = self.dialogclass( dialog, title=self.prompt, options=options, @@ -360,7 +365,7 @@ class SelectionFrame(ScrollableFrame): self.renderHeader(container) for idx, option in enumerate(self.options): key = self._mapValue(idx, option) - self.values[key] = NotifyBoolVar() + self.values[key] = BoolVar() self.renderOption(container, option, idx) if self.selected and key in self.selected: self.values[key].set(True) diff --git a/jfr_playoff/gui/variables.py b/jfr_playoff/gui/variables.py index 7e6989c..ac9e0a6 100644 --- a/jfr_playoff/gui/variables.py +++ b/jfr_playoff/gui/variables.py @@ -20,16 +20,22 @@ class NumericVar(tk.StringVar): except ValueError: return default +class BoolVar(tk.StringVar): + def get(self, *args, **kwargs): + value = tk.StringVar.get(self, *args, **kwargs) + return int(value == '1') + + def set(self, value, *args, **kwargs): + return tk.StringVar.set(self, '1' if value else '0', *args, **kwargs) + class NotifyStringVar(NotifyVar, tk.StringVar): pass class NotifyIntVar(NotifyVar, tk.IntVar): pass -class NotifyBoolVar(NotifyVar, tk.StringVar): - def get(self, *args, **kwargs): - value = tk.StringVar.get(self, *args, **kwargs) - return int(value == '1') +class NotifyBoolVar(NotifyVar, BoolVar): + pass class NotifyNumericVar(NumericVar, NotifyVar): def __init__(self, *args, **kwargs): -- cgit v1.2.3