-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython - Find Specific Digits in Pi GUI.txt
More file actions
65 lines (47 loc) · 2.14 KB
/
Python - Find Specific Digits in Pi GUI.txt
File metadata and controls
65 lines (47 loc) · 2.14 KB
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
import tkinter as tk
def search_pi(event=None):
search_number = entry.get()
if not search_number.isdigit():
result_text.delete("1.0", tk.END)
result_text.insert(tk.END, "Invalid input. Please enter a number.")
return
with open('C:/Users/botoole/Videos/BX STUFF/CODING DOCS/Pi_Billion.txt', 'r') as file:
pi_digits = file.read().replace('\n', '')
if search_number not in pi_digits:
result_text.delete("1.0", tk.END)
result_text.insert(tk.END, "Number not found in pi.")
return
index = pi_digits.index(search_number)
start_index = max(0, index - 25)
end_index = min(index + 25 + len(search_number), len(pi_digits))
highlighted_digits = pi_digits[start_index:end_index]
highlighted_digits = highlighted_digits.replace(search_number, f"[{search_number}]", 1)
result_text.config(state=tk.NORMAL)
result_text.delete("1.0", tk.END)
result_text.insert(tk.END, f"3.14...{highlighted_digits}")
# Highlight the found string
start_tag = "1.0 + {}c".format(index - start_index + 8)
end_tag = "1.0 + {}c".format(index - start_index + len(search_number) + 8)
result_text.tag_add("highlight", start_tag, end_tag)
result_text.tag_configure("highlight", background="green", foreground="white")
result_text.config(state=tk.DISABLED)
position_label.config(text=f"Found at position: {index + 1}")
# Create the GUI window
window = tk.Tk()
window.title("Pi Digit Search")
window.geometry("800x400") # Doubled the window size
# Create an entry box for number input
entry = tk.Entry(window)
entry.pack()
entry.bind("<Return>", search_pi) # Bind the Enter key to the search function
# Create a button to initiate the search
search_button = tk.Button(window, text="Search", command=search_pi)
search_button.pack()
# Create a text widget to display the search result
result_text = tk.Text(window, height=10)
result_text.pack()
result_text.config(state=tk.DISABLED)
# Create a label to display the position of the result
position_label = tk.Label(window, text="")
position_label.pack()
window.mainloop()