-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloatvarwithdimension.py
More file actions
102 lines (72 loc) · 3.19 KB
/
floatvarwithdimension.py
File metadata and controls
102 lines (72 loc) · 3.19 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import tkinter as tk
from tkinter import ttk
from valuewithdimension import ValueWithDimension
class Tet: # 测试用的类 没什么实际的用处
def __init__(self,x=0.0):
self.x=x
class FloatVarWithDimension(tk.DoubleVar):
"""继承doublevar 主要为了实现绑定功能(将控件的floatvar与ValueWithDimension绑定) 重载它的set方法
另外据观察:变量从控件中的更新 在 执行DoubleVar.get()以后"""
def __init__(self, master=None, value=None, name=None,dimension_text=''):
super().__init__(master,value,name)
if value is None:
value=0
self.__number_with_dimension=ValueWithDimension(value,dimension_text)
def set(self,v):
if isinstance(v,(float,int)):
super().set(v)
# 改变number_with_dimension
self.__number_with_dimension.value = v
elif isinstance(v,ValueWithDimension):
self.__number_with_dimension=v
super().set(v.value)
else:
raise Exception("未知类型")
def get(self):
v=super().get()
self.set(v)
return v
def update_value(self):
'''从__number_with_dimension更新值'''
self.set(self.__number_with_dimension.value)
@property
def value(self):
return self.__number_with_dimension.value
@property
def dimension(self):
return self.__number_with_dimension.dimension_text
@property
def value_and_dimension(self):
return "%f %s"%(self.value,self.dimension)
def switch_dimension(self,*args):
self.__number_with_dimension.switch_dimension(*args)
self.update_value()
def format(self,erase_dim='force'):
self.__number_with_dimension.format(erase_dim=erase_dim)
self.update_value()
def __add__(self, other):
return self.__number_with_dimension.__add__(other.__number_with_dimension)
def __sub__(self, other):
return self.__number_with_dimension.__sub__(other.__number_with_dimension)
def __mul__(self, other):
if isinstance(other,(float,int,ValueWithDimension)):
return self.__number_with_dimension.__mul__(other)
elif isinstance(other,FloatVarWithDimension):
return self.__number_with_dimension.__mul__(other.__number_with_dimension)
return Exception("未知类型")
def __truediv__(self, other):
if isinstance(other,(float,int,ValueWithDimension)):
return self.__number_with_dimension.__truediv__(other)
elif isinstance(other,FloatVarWithDimension):
return self.__number_with_dimension.__truediv__(other.__number_with_dimension)
return Exception("未知类型")
def __pow__(self, power, modulo=None):
return self.__number_with_dimension.__pow__(power,modulo)
def __eq__(self, other):
if isinstance(other,(int,float)):
return self.__number_with_dimension==other
if isinstance(other,FloatVarWithDimension):
return self.__number_with_dimension==other.__number_with_dimension
if isinstance(other,ValueWithDimension):
return self.__number_with_dimension==other
raise Exception("未知类型")