Aug-23-2023, 01:42 PM
Nope, nope, nope, nope. Your code does not run. You have the same functions defined multiple times. The code is messy and far longer than I want to read. You don't even ask a clear question. If you are asking for assistance, you really need to do better. Post a small, runnable example that demonstrates the problem you are trying to solve, a problem that is clearly described in your post.
I modified the update_plot() function so you can at least get an idea of what your program looks like with the plot appearing right of the form.
I modified the update_plot() function so you can at least get an idea of what your program looks like with the plot appearing right of the form.
def update_plot(): plt.clf() # Clear the previous plot models = [model_vars[col].get() for col in range(num_columns)] cycle_times = [cycle_time_labels[col]['text'] for col in range(num_columns)] plt.barh(models, [float(random.randint(1, 100)) for time in cycle_times]) plt.xlabel("Cycle Time (hours)") plt.ylabel("Model") plt.title("Cycle Time Comparison")The existing program crashed because you called update_plot() when the form was empty. It crashes whenever you change something in the form, because editing the form calls update_plot(), and the form is only partially complete. One way to get around this is catch the exceptions in update_plot. If there is an exception, don't change the plot. Maybe display a message to let the user know why the plot isn't updating.
def update_plot(): plt.clf() # Clear the previous plot models = [model_vars[col].get() for col in range(num_columns)] cycle_times = [cycle_time_labels[col]['text'] for col in range(num_columns)] try: cycle_times = [float(time.split(":")[0]) + float(time.split(":")[1])/60 + float(time.split(":")[2])/3600 for time in cycle_times] except (ValueError, IndexError): # Maybe display message? return plt.barh(models, cycle_times) plt.xlabel("Cycle Time (hours)") plt.ylabel("Model") plt.title("Cycle Time Comparison") # Draw the plot onto the canvas canvas = FigureCanvasTkAgg(plt.gcf(), master=canvas_frame) canvas.draw() canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)calculate_travel_distance() has an error. You pass a list as the travel_distance argument, but you treat travel_distance like it is a float. Not sure what the fix is because I don't know what you are trying to accomplish there. Either you need to modify the function to take a list, or you need to pass a float. I modified the function call.
def update_model_travel_times(col): raw_distance = travel_distance_entries[col].get() load_time_empty = not load_time_entries[col].get().strip() dump_time_empty = not dump_time_entries[col].get().strip() delay_time_empty = not delay_time_entries[col].get().strip() if not raw_distance.strip(): if load_time_empty and dump_time_empty and delay_time_empty: model_travel_time_labels[col].config(text="Input Required") flash_red(model_travel_time_labels[col]) else: model_travel_time_labels[col].config(text="") # Reset the color to the original color if it was flashed model_travel_time_labels[col].config(foreground="black") else: try: travel_distance = float(raw_distance) travel_time = calculate_travel_time(travel_distance[col]) # <-- Pass float, not list formatted_travel_time = format_duration(travel_time * 60) model_travel_time_labels[col].config(text=formatted_travel_time) except ValueError: model_travel_time_labels[col].config(text="Invalid Distance") flash_red(model_travel_time_labels[col])