-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
54 lines (42 loc) · 1.48 KB
/
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
import os
import tkinter as tk
from tkinter import filedialog
def is_valid_filename(filename):
return os.path.isfile(filename)
def autocomplete(event):
current_text = entry.get()
matching_files = [file for file in os.listdir(".") if file.startswith(current_text)]
if len(matching_files) == 1:
entry.delete(0, tk.END)
entry.insert(0, matching_files[0])
def validate_filename(event):
filename = entry.get()
if is_valid_filename(filename):
entry.config({"background": "SpringGreen"})
else:
entry.config({"background": "Salmon"})
def browse_file():
file_path = filedialog.askopenfilename()
entry.delete(0, tk.END)
entry.insert(0, file_path)
validate_filename(None)
if __name__ == '__main__':
# Initilize the Tiknter window
root = tk.Tk()
root.title("File Selection")
# Size and add padding to the window
root.geometry("350x175")
root.config(padx=10, pady=10)
# Label
label = tk.Label(root, text="Enter the filename:", font=("Helvetica", 16))
label.pack(pady=10)
# File name entry box
entry = tk.Entry(root, width=50, font=("Calibri 12"), validate="focusout")
entry.pack(pady=10)
entry.bind("<FocusOut>", validate_filename)
entry.bind("<Tab>", autocomplete)
# File dialog button
browse_button = tk.Button(root, text="Browse for File", font=("Helvetica", 12), command=browse_file)
browse_button.pack(pady=10)
# Start the main event loop
root.mainloop()