-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainApplication.py
152 lines (123 loc) · 5.04 KB
/
MainApplication.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'''
Main application of the package. It displays a set of physics fields,
each one containing a bunch of examples.
@Author: Gonzalo M.
@Date: 23/11/2014
'''
import Tkinter
class Application( Tkinter.Tk ):
'''
Redefine Tkinter's Tk class in order to use contain every object associated to it.
'''
def __init__( self, name, **kargs ):
Tkinter.Tk.__init__( self, screenName = name )
self.elements = {}
for k in kargs:
exec( 'self.configure( {0} = {1} ) )'.format( k, kargs[k] ) )
self.update()
def Add( self, kind, name, *args, **kargs ):
'''
Add a widget of specified kind.
'''
if args:
exec( "self.elements['{1}'] = Tkinter.{0}( self, *args, **kargs )".format(kind,name))
else:
exec( "self.elements['{1}'] = Tkinter.{0}( self, **kargs )".format(kind,name))
def Get( self, name ):
'''
Return element identified named name.
'''
return self.elements.get( name )
def GetValue( self, name ):
'''
Return current value of some element named name. It must be a variable (and have a get method).
'''
return self.Get( name ).get()
def Pack( self, name, **packing_opt ):
'''
Pack widget named @name.
'''
self.elements[name].pack( **packing_opt )
def Size( self ):
'''
Get size of the application.
'''
return tuple(int(npx) for npx in self.geometry().split('+')[0].split('x'))
def Resize( self, width, height ):
'''
Change dimensions.
'''
self.geometry( '{0}x{1}'.format( width, height ) )
self.update()
def Repos( self, xpos, ypos ):
'''
Change position on screen.
'''
self.geometry( '+{0}+{1}'.format( xpos, ypos ) )
self.update()
def Center( self ):
'''
Center application on screen.
'''
w = self.winfo_screenwidth()
h = self.winfo_screenheight()
xsize, ysize = self.Size()
x = w/2 - xsize/2
y = h/2 - ysize/2
self.Repos( x, y )
def Calling(arg):
'''
Dummy printing
'''
print 'Calling ' + arg
def CheckSelection():
'''
Check the selected option and behave accordingly to it.
'''
if MainWindow.GetValue('gravity_var') != Gravity_labels[0]:
Calling( MainWindow.GetValue('gravity_var') )
elif MainWindow.GetValue('electro_var') != Electromagnetism_labels[0]:
Calling( MainWindow.GetValue('electro_var') )
elif MainWindow.GetValue('thermo_var') != Thermodynamics_labels[0]:
Calling( MainWindow.GetValue('thermo_var') )
elif MainWindow.GetValue('optics_var') != Optics_labels[0]:
Calling( MainWindow.GetValue('optics_var') )
elif MainWindow.GetValue('quantum_var') != QuantumMechanics_labels[0]:
Calling( MainWindow.GetValue('quantum_var') )
if __name__ == '__main__':
# Create a new window
MainWindow = Application( 'VisualPhysics' )
MainWindow.Resize( 600, 300 )
MainWindow.Center()
# Load and insert logo
photofile = Tkinter.PhotoImage( file = 'pylogo.gif' )
MainWindow.Add( 'Label', 'logo', image = photofile, height = 300, width = 300 )
MainWindow.Pack( 'logo', side = Tkinter.RIGHT )
# Now we want to create a number of sections for our physics examples.
# Label for each field
Gravity_labels = ['Gravity']
Electromagnetism_labels = ['Electromagnetism']
Thermodynamics_labels = ['Thermodynamics']
Optics_labels = ['Optics']
QuantumMechanics_labels = ['Quantum mechanics']
# We can add a few examples for each field
Gravity_labels += ['Solar system','Moon']
Electromagnetism_labels += ['Dipole']
# Create variables
MainWindow.Add( 'StringVar', 'gravity_var', Gravity_labels[0] )
MainWindow.Add( 'StringVar', 'electro_var', Electromagnetism_labels[0] )
MainWindow.Add( 'StringVar', 'thermo_var' , Thermodynamics_labels[0] )
MainWindow.Add( 'StringVar', 'optics_var' , Optics_labels[0] )
MainWindow.Add( 'StringVar', 'quantum_var', QuantumMechanics_labels[0] )
# Add option menus to Main window
MainWindow.Add( 'OptionMenu', 'Gravity', MainWindow.Get('gravity_var'), *Gravity_labels )
MainWindow.Add( 'OptionMenu', 'Electro', MainWindow.Get('electro_var'), *Electromagnetism_labels )
MainWindow.Add( 'OptionMenu', 'Thermo' , MainWindow.Get('thermo_var' ), *Thermodynamics_labels )
MainWindow.Add( 'OptionMenu', 'Optics' , MainWindow.Get('optics_var' ), *Optics_labels )
MainWindow.Add( 'OptionMenu', 'Quantum', MainWindow.Get('quantum_var'), *QuantumMechanics_labels )
# Add a button to run the selected option
MainWindow.Add( 'Button', 'GO', text = 'GO!', command = CheckSelection )
# Packing
[ MainWindow.Pack( name, fill = Tkinter.X ) for name in [ 'GO', 'Gravity', 'Electro', 'Thermo', 'Optics', 'Quantum' ] ]
# Mainloop
MainWindow.mainloop()