summaryrefslogtreecommitdiff
path: root/jfr_playoff/gui/variables.py
blob: 7e6989c97181ef1064c651f6857a771399a6eb13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#coding=utf-8

import tkinter as tk

class NotifyVar(tk.Variable):
    def __init__(self, *args, **kwargs):
        tk.Variable.__init__(self, *args, **kwargs)
        self._prevValue = self.get()
        self._root.after(0, self.trace, 'w', self._onChange)

    def _onChange(self, *args):
        if self._prevValue != self.get():
            self._root.event_generate('<<ValueChanged>>', when='tail')
        self._prevValue = self.get()

class NumericVar(tk.StringVar):
    def get(self, default=None):
        try:
            return int(str(tk.StringVar.get(self)).strip())
        except ValueError:
            return default

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 NotifyNumericVar(NumericVar, NotifyVar):
    def __init__(self, *args, **kwargs):
        NotifyVar.__init__(self, *args, **kwargs)
        NumericVar.__init__(self, *args, **kwargs)