-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolar_main.py
145 lines (115 loc) · 4.29 KB
/
solar_main.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
# coding: utf-8
# license: GPLv3
import tkinter
from tkinter.filedialog import *
from solar_vis import *
from solar_model import *
from solar_input import *
perform_execution = False
"""main loop flag"""
physical_time = 0
"""true tiem since the very beginning: float"""
displayed_time = None
"""displayed time: tkinter time type"""
time_step = None
"""modelling time step: float"""
space_objects = []
"""Objects being modelled: list"""
def execution():
"""Computes object positions and views on the screen in the main loop.
Looping depends on perform_execution.
if perform_execution == True, this function calls itself every 1 ms up to 100 times.
"""
global physical_time
global displayed_time
recalculate_space_objects_positions(space_objects, time_step.get())
for body in space_objects:
update_object_position(space, body)
physical_time += time_step.get()
displayed_time.set("%.1f" % physical_time + " seconds gone")
if perform_execution:
space.after(101 - int(time_speed.get()), execution)
def start_execution():
""" Start button press handler.
Calls execution() function.
"""
global perform_execution
perform_execution = True
start_button['text'] = "Pause"
start_button['command'] = stop_execution
execution()
print('Started execution...')
def stop_execution():
"""Start button press handler.
Stops execution() function.
"""
global perform_execution
perform_execution = False
start_button['text'] = "Start"
start_button['command'] = start_execution
print('Paused execution.')
def open_file_dialog():
"""Opens file choice dialog and calls read_space_objects_data_from_file function to parse it.
Parsed object data saved to space_objects
"""
global space_objects
global perform_execution
perform_execution = False
for obj in space_objects:
space.delete(obj.image) # удаление старых изображений планет
in_filename = askopenfilename(filetypes=(("Text file", ".txt"),))
space_objects = read_space_objects_data_from_file(in_filename)
max_distance = max([max(abs(obj.x), abs(obj.y)) for obj in space_objects])
calculate_scale_factor(max_distance)
for obj in space_objects:
if obj.type == 'star':
create_star_image(space, obj)
elif obj.type == 'planet':
create_planet_image(space, obj)
else:
raise AssertionError()
def save_file_dialog():
"""Opens file choice dialog and calls write_space_objects_data_to_file function
"""
out_filename = asksaveasfilename(filetypes=(("Text file", ".txt"),))
write_space_objects_data_to_file(out_filename, space_objects)
def main():
"""Main function.
Generates tkinter objects: window, GUI(with buttons), canvas.
"""
global physical_time
global displayed_time
global time_step
global time_speed
global space
global start_button
print('Modelling started!')
physical_time = 0
root = tkinter.Tk()
# everythng will be drawn on Canvas
space = tkinter.Canvas(root, width=window_width, height=window_height, bg="black")
space.pack(side=tkinter.TOP)
# lower control panel with buttons
frame = tkinter.Frame(root)
frame.pack(side=tkinter.BOTTOM)
start_button = tkinter.Button(frame, text="Start", command=start_execution, width=6)
start_button.pack(side=tkinter.LEFT)
time_step = tkinter.DoubleVar()
time_step.set(1)
time_step_entry = tkinter.Entry(frame, textvariable=time_step)
time_step_entry.pack(side=tkinter.LEFT)
time_speed = tkinter.DoubleVar()
scale = tkinter.Scale(frame, variable=time_speed, orient=tkinter.HORIZONTAL)
scale.pack(side=tkinter.LEFT)
load_file_button = tkinter.Button(frame, text="Open file...", command=open_file_dialog)
load_file_button.pack(side=tkinter.LEFT)
save_file_button = tkinter.Button(frame, text="Save to file...", command=save_file_dialog)
save_file_button.pack(side=tkinter.LEFT)
displayed_time = tkinter.StringVar()
displayed_time.set(str(physical_time) + " seconds gone")
time_label = tkinter.Label(frame, textvariable=displayed_time, width=30)
time_label.pack(side=tkinter.RIGHT)
root.mainloop()
print('Modelling finished!')
if __name__ == "__main__":
main()