Skip to content

Commit 2484db2

Browse files
authored
UPDATE - 020424
Nothing much with this release, couple of changes: - Got rid of some useless code that did not much. - Added heaps of comments, probably a ridiculous amount. _ More fluid now, GUI code somewhat works better.
1 parent 030b7f5 commit 2484db2

File tree

1 file changed

+103
-119
lines changed

1 file changed

+103
-119
lines changed

Py-GUI.py

Lines changed: 103 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,116 @@
1-
import time # Importing the time module for time-related functions
2-
import tkinter as tk # Importing the tkinter module for GUI
3-
from tkinter import messagebox # Importing messagebox module from tkinter for displaying messages
4-
from colorama import init, Fore, Style # Importing colorama module for colored text
1+
import time # Import the time module for adding delays
2+
import tkinter as tk # Import the tkinter module for GUI
3+
from tkinter import messagebox # Import the messagebox module from tkinter
54

6-
# Initialize colorama for cross-platform ANSI color support
7-
init(autoreset=True)
5+
simarray1 = [42, 32, 23, 12, 19, 54] # Define a list of integers
86

9-
# Global variable to hold the array
10-
simarray1 = [42, 32, 23, 12, 19, 54]
7+
root = tk.Tk() # Create a tkinter root window
8+
root.title("Standard Algorithms Simulator") # Set the title of the root window
119

12-
# Create Tkinter window
13-
root = tk.Tk()
14-
15-
# Set the title of the Tkinter window
16-
root.title("Standard Algorithms Simulator")
17-
18-
# Function to convert an array into a string for printing
1910
def print_array(arr):
20-
return "[" + " ".join(map(str, arr)) + "]"
11+
return "[" + " ".join(map(str, arr)) + "]" # Convert the array to a string representation
2112

22-
# Function to perform bubble sort algorithm
2313
def bubble_sort(arr):
24-
n = len(arr)
25-
for i in range(n):
26-
for j in range(0, n - i - 1):
27-
if arr[j] > arr[j + 1]:
28-
arr[j], arr[j + 1] = arr[j + 1], arr[j] # Swap elements if they are in the wrong order
29-
array_label.config(text=print_array(arr)) # Update array_label in GUI
14+
n = len(arr) # Get the length of the array
15+
for i in range(n): # Iterate over the array
16+
is_sorted = True # Flag to check if the array is already sorted
17+
for j in range(0, n - i - 1): # Iterate over the unsorted part of the array
18+
if arr[j] > arr[j + 1]: # If the current element is greater than the next element
19+
arr[j], arr[j + 1] = arr[j + 1], arr[j] # Swap the elements
20+
is_sorted = False # Set the flag to False
21+
array_label.config(text=print_array(arr)) # Update the label with the sorted array
3022
root.update() # Update the GUI
31-
time.sleep(1) # Pause for 1 second
32-
33-
# Function to perform binary search algorithm
34-
def binary_search(arr, x):
35-
low = 0
36-
high = len(arr) - 1
37-
while low <= high:
38-
mid = (low + high) // 2
39-
if arr[mid] == x:
40-
return mid # Return the index if the element is found
41-
elif arr[mid] < x:
42-
low = mid + 1 # Continue searching in the right half
43-
else:
44-
high = mid - 1 # Continue searching in the left half
45-
return -1 # Return -1 if the element is not found
46-
47-
# Function to perform bubble sort and display the sorted array in the GUI
23+
time.sleep(0.5) # Add a delay to visualize the sorting process
24+
if is_sorted: # If the array is already sorted, break the loop
25+
break
26+
return arr # Return the sorted array
27+
28+
def binary_search(arr, x, low, high):
29+
while low <= high: # Perform binary search until the low index is less than or equal to the high index
30+
mid = (low + high) // 2 # Calculate the middle index
31+
if arr[mid] == x: # If the middle element is equal to the search value
32+
return mid # Return the index of the middle element
33+
elif arr[mid] < x: # If the middle element is less than the search value
34+
low = mid + 1 # Update the low index to search in the right half of the array
35+
else: # If the middle element is greater than the search value
36+
high = mid - 1 # Update the high index to search in the left half of the array
37+
return -1 # Return -1 if the search value is not found in the array
38+
4839
def perform_bubble_sort():
49-
bubble_sorted_array = simarray1.copy()
50-
bubble_sort(bubble_sorted_array)
51-
sorted_array_label.config(text=print_array(bubble_sorted_array))
40+
bubble_sort(simarray1) # Call the bubble_sort function to sort the array
41+
sorted_array_label.config(text=print_array(simarray1)) # Update the label with the sorted array
5242

53-
# Function to perform binary search and display the search result in a message box
5443
def perform_binary_search():
55-
search_value = int(search_entry.get())
56-
bubble_sorted_array = sorted(simarray1)
57-
index = binary_search(bubble_sorted_array, search_value)
58-
if index != -1:
59-
messagebox.showinfo("Binary Search Result", f"Value {search_value} found at index {index}")
60-
else:
61-
messagebox.showwarning("Binary Search Result", f"Value {search_value} not found in the array")
62-
63-
# Function for bubble sort in command-line interface (CLI)
64-
def cli_bubble_sort():
65-
bubble_sorted_array = simarray1.copy()
66-
bubble_sort(bubble_sorted_array)
67-
print("\nArray sorted using Bubble Sort:", bubble_sorted_array)
68-
69-
# Function for binary search in command-line interface (CLI)
70-
def cli_binary_search():
71-
search_value = int(input("Enter the value to search for: "))
72-
bubble_sorted_array = sorted(simarray1)
73-
index = binary_search(bubble_sorted_array, search_value)
74-
if index != -1:
75-
print(Fore.GREEN + f"Value {search_value} found at index {index}")
76-
else:
77-
print(Fore.RED + f"Value {search_value} not found in the array")
78-
79-
# GUI Elements
80-
array_label = tk.Label(root, text=print_array(simarray1)) # Label to display the array in the GUI
81-
array_label.pack() # Pack the array label into the window
82-
83-
bubble_sort_button = tk.Button(root, text="Bubble Sort", command=perform_bubble_sort) # Button to trigger bubble sort
84-
bubble_sort_button.pack() # Pack the bubble sort button into the window
85-
86-
sorted_array_label = tk.Label(root, text="") # Label to display the sorted array in the GUI
87-
sorted_array_label.pack() # Pack the sorted array label into the window
88-
89-
search_frame = tk.Frame(root) # Frame to contain search-related elements
90-
search_frame.pack() # Pack the search frame into the window
91-
92-
search_label = tk.Label(search_frame, text="Enter value to search:") # Label for the search entry
93-
search_label.pack(side="left") # Pack the search label into the search frame
94-
95-
search_entry = tk.Entry(search_frame) # Entry widget to enter the search value
96-
search_entry.pack(side="left") # Pack the search entry into the search frame
97-
98-
binary_search_button = tk.Button(root, text="Binary Search", command=perform_binary_search) # Button to trigger binary search
99-
binary_search_button.pack() # Pack the binary search button into the window
100-
101-
exit_button = tk.Button(root, text="Exit", command=root.destroy) # Button to exit the program
102-
exit_button.pack() # Pack the exit button into the window
103-
104-
# Main function for command-line interface (CLI)
105-
def main():
106-
print("Welcome to Standard Algorithms Simulator")
107-
while True:
108-
print("\nMain Menu:")
109-
print("1. Bubble Sort")
110-
print("2. Binary Search")
111-
print("3. Exit")
112-
choice = input("Enter your choice: ")
113-
114-
if choice == "1":
115-
print("\nBubble Sort:")
116-
cli_bubble_sort()
117-
118-
elif choice == "2":
119-
print("\nBinary Search:")
120-
cli_binary_search()
121-
122-
elif choice == "3":
123-
print("Exiting the program. Goodbye!")
124-
break
44+
if not sorted_array_label.cget("text"): # If the sorted array label is empty
45+
messagebox.showerror("Error", "Please run Bubble Sort first to sort the array.") # Show an error message
46+
return
12547

126-
else:
127-
print("Invalid choice. Please enter a valid option.")
48+
search_value = search_entry.get().strip() # Get the search value from the entry widget and remove leading/trailing spaces
49+
if not search_value: # If the search value is empty
50+
messagebox.showerror("Error", "Please enter a value to search.") # Show an error message
51+
return
12852

129-
if __name__ == "__main__":
130-
main()
53+
try:
54+
search_value = int(search_value) # Convert the search value to an integer
55+
except ValueError: # If the search value is not a valid integer
56+
messagebox.showerror("Error", "Please enter a valid integer.") # Show an error message
57+
return
58+
59+
index = binary_search(simarray1, search_value, 0, len(simarray1) - 1) # Call the binary_search function to search for the value
60+
if index != -1: # If the value is found in the array
61+
messagebox.showinfo("Binary Search Result", f"Value {search_value} found at index {index}") # Show an info message
62+
else: # If the value is not found in the array
63+
messagebox.showwarning("Binary Search Result", f"Value {search_value} not found in the array") # Show a warning message
64+
65+
def show_main_screen():
66+
homescreen_frame.pack_forget() # Hide the homescreen frame
67+
main_frame.pack() # Show the main frame
68+
69+
def show_homescreen():
70+
main_frame.pack_forget() # Hide the main frame
71+
homescreen_frame.pack() # Show the homescreen frame
72+
73+
homescreen_frame = tk.Frame(root) # Create a frame for the homescreen
74+
homescreen_frame.pack() # Pack the homescreen frame
75+
76+
homescreen_label = tk.Label(homescreen_frame, text="Welcome to Standard Algorithms Simulator") # Create a label for the homescreen
77+
homescreen_label.pack() # Pack the homescreen label
78+
79+
begin_button = tk.Button(homescreen_frame, text="Begin", command=show_main_screen) # Create a button to begin the simulation
80+
begin_button.pack() # Pack the begin button
13181

132-
root.mainloop() # Start the Tkinter event loop
82+
main_frame = tk.Frame(root) # Create a frame for the main screen
83+
84+
back_button = tk.Button(main_frame, text="Back", command=show_homescreen) # Create a button to go back to the homescreen
85+
back_button.pack() # Pack the back button
86+
87+
array_label = tk.Label(main_frame, text=print_array(simarray1)) # Create a label to display the array
88+
array_label.pack() # Pack the array label
89+
90+
bubble_sort_button = tk.Button(main_frame, text="Bubble Sort", command=perform_bubble_sort) # Create a button to perform bubble sort
91+
bubble_sort_button.pack() # Pack the bubble sort button
92+
93+
sorted_array_label = tk.Label(main_frame, text="") # Create a label to display the sorted array
94+
sorted_array_label.pack() # Pack the sorted array label
95+
96+
search_frame = tk.Frame(main_frame) # Create a frame for the search functionality
97+
search_frame.pack() # Pack the search frame
98+
99+
search_label = tk.Label(search_frame, text="Enter value to search:") # Create a label for the search entry
100+
search_label.pack(side="left") # Pack the search label to the left
101+
102+
search_entry = tk.Entry(search_frame) # Create an entry widget for the search value
103+
search_entry.pack(side="left") # Pack the search entry to the left
104+
105+
binary_search_button = tk.Button(main_frame, text="Binary Search", command=perform_binary_search) # Create a button to perform binary search
106+
binary_search_button.pack() # Pack the binary search button
107+
108+
exit_button = tk.Button(main_frame, text="Exit", command=root.destroy) # Create a button to exit the program
109+
exit_button.pack() # Pack the exit button
110+
111+
def main():
112+
show_homescreen() # Show the homescreen
113+
root.mainloop() # Start the tkinter event loop
114+
115+
if __name__ == "__main__":
116+
main() # Call the main function to start the program

0 commit comments

Comments
 (0)