|
| 1 | +from newton_command import NEWTON_CMDS_DICT |
| 2 | + |
| 3 | +import dearpygui.dearpygui as dpg |
| 4 | + |
| 5 | + |
| 6 | +def selection_cb(sender, app_data, user_data): |
| 7 | + if user_data[1]: |
| 8 | + print("User selected 'Ok'") |
| 9 | + else: |
| 10 | + print("User selected 'Cancel'") |
| 11 | + |
| 12 | + # delete window |
| 13 | + dpg.delete_item(user_data[0]) |
| 14 | + |
| 15 | + |
| 16 | +def show_info(title, message, selection_callback): |
| 17 | + # Reference: https://github.com/hoffstadt/DearPyGui/discussions/1002 |
| 18 | + |
| 19 | + # guarantee these commands happen in the same frame |
| 20 | + with dpg.mutex(): |
| 21 | + viewport_width = dpg.get_viewport_client_width() |
| 22 | + viewport_height = dpg.get_viewport_client_height() |
| 23 | + |
| 24 | + with dpg.window(tag='popup-window', label=title, modal=True, no_close=True) as modal_id: |
| 25 | + dpg.add_text(message) |
| 26 | + dpg.add_button(label="Ok", width=75, user_data=(modal_id, True), callback=selection_callback) |
| 27 | + dpg.add_same_line() |
| 28 | + dpg.add_button(label="Cancel", width=75, user_data=(modal_id, False), callback=selection_callback) |
| 29 | + |
| 30 | + # guarantee these commands happen in another frame |
| 31 | + dpg.split_frame() |
| 32 | + width = dpg.get_item_width(modal_id) |
| 33 | + height = dpg.get_item_height(modal_id) |
| 34 | + dpg.set_item_pos(modal_id, [viewport_width // 2 - width // 2, viewport_height // 2 - height // 2]) |
| 35 | + |
| 36 | + |
| 37 | +# Callbacks and Helpers |
| 38 | +def on_evaluate(sender, app_data, user_data): |
| 39 | + # Get the Command |
| 40 | + cmd = dpg.get_value('radio-cmds') |
| 41 | + cmd_func = NEWTON_CMDS_DICT[cmd] |
| 42 | + |
| 43 | + # Get the Expression |
| 44 | + expr = dpg.get_value('inp-expr') |
| 45 | + |
| 46 | + if expr.strip() in ['']: |
| 47 | + show_info( |
| 48 | + 'Error', |
| 49 | + 'Please use valid mathematical expressions.', |
| 50 | + selection_cb |
| 51 | + ) |
| 52 | + # Clear Expression |
| 53 | + dpg.set_value('inp-expr', '') |
| 54 | + return |
| 55 | + |
| 56 | + # Evaluate |
| 57 | + response = cmd_func(expr) |
| 58 | + result = response.result |
| 59 | + |
| 60 | + dpg.set_value('label-output', result) |
| 61 | + |
| 62 | + |
| 63 | +dpg.create_context() |
| 64 | +dpg.create_viewport(title='Computer Algebra', width=1300, height=750) |
| 65 | + |
| 66 | +with dpg.window(tag='inp-window', |
| 67 | + label="Input", |
| 68 | + pos=[0, 0], |
| 69 | + autosize=True, |
| 70 | + # width=1150, |
| 71 | + # height=350, |
| 72 | + no_collapse=True, |
| 73 | + no_close=True, |
| 74 | + ): |
| 75 | + # Radio Button for Commands |
| 76 | + dpg.add_radio_button( |
| 77 | + horizontal=True, |
| 78 | + tag='radio-cmds', |
| 79 | + items=[cmd for cmd in NEWTON_CMDS_DICT.keys()] |
| 80 | + ) |
| 81 | + |
| 82 | + # Text Area for Mathematical Expression |
| 83 | + dpg.add_input_text( |
| 84 | + tag='inp-expr', |
| 85 | + width=int(1150 * 0.8), |
| 86 | + ) |
| 87 | + |
| 88 | + # Button for Evaluating Command and Expression |
| 89 | + dpg.add_button(label="Evaluate", callback=on_evaluate) |
| 90 | + |
| 91 | +with dpg.window(tag='out-window', |
| 92 | + pos=[0, 100], |
| 93 | + label="Output", |
| 94 | + # width=700, |
| 95 | + # height=350, |
| 96 | + autosize=True, |
| 97 | + no_collapse=True, |
| 98 | + no_close=True, |
| 99 | + ): |
| 100 | + # Use Label for Output |
| 101 | + dpg.add_text(tag='label-output', |
| 102 | + label='Result', |
| 103 | + show_label=True, |
| 104 | + ) |
| 105 | + |
| 106 | +dpg.setup_dearpygui() |
| 107 | +dpg.show_viewport() |
| 108 | +dpg.start_dearpygui() |
| 109 | +dpg.destroy_context() |
0 commit comments