|
| 1 | +import detectlanguage as dtl #Client Library for our API |
| 2 | +import tkinter as tk |
| 3 | +from tkinter import font |
| 4 | +from tkinter import * |
| 5 | +from tkinter import filedialog |
| 6 | +from tkinter import messagebox as mbox |
| 7 | +from tkinter import scrolledtext |
| 8 | +from tkinter import ttk |
| 9 | +import csv |
| 10 | + |
| 11 | +dtl.Configuration.api_key = "YOUR__API__KEY" #Your API Key |
| 12 | + |
| 13 | +##--------BUTTON CALLBACKS--------## |
| 14 | +def clear_input(): |
| 15 | + entry_1.delete(0,END) |
| 16 | + |
| 17 | +def activate_input(): |
| 18 | + entry_1['state'] = "normal" #enables statement entry widget |
| 19 | + if(file_entry.get()!=""):#check if file_entry widget is having file_name in it |
| 20 | + file_entry.delete(0,END) #deletes file name if selected from file_entry widget |
| 21 | + file_entry['state'] = "disabled" #then disables file_entry widget |
| 22 | + |
| 23 | + |
| 24 | +def choose_file(): |
| 25 | + global content |
| 26 | + if(entry_1.get()!=""): #checking if statement entry has text in it |
| 27 | + entry_1.delete(0,END) #deleting text from entry_1 widget |
| 28 | + entry_1['state'] = "disabled" #disabling the statement entry |
| 29 | + file_entry['state'] = "normal" #Making the file entry visible |
| 30 | + file = filedialog.askopenfile(initialdir="/", |
| 31 | + title="Choose File", |
| 32 | + filetypes=(("Text files","*.txt*"), |
| 33 | + ("all files","*.*")), |
| 34 | + mode = 'r+') |
| 35 | + if file != None: |
| 36 | + content = file.read() |
| 37 | + if(file_entry.get()!=""): |
| 38 | + file_entry.delete(0,END) |
| 39 | + file_entry.insert(END,file.name) |
| 40 | + return content |
| 41 | + else: |
| 42 | + mbox.showerror("File Error", "File not selected!!") |
| 43 | + |
| 44 | +def detect(): |
| 45 | + if(entry_1['state']=='normal'): |
| 46 | + if entry_1.get()!= "": #check if the entry has no content |
| 47 | + content1 = entry_1.get() |
| 48 | + stxt['state'] = 'normal' |
| 49 | + if (stxt.get('1.0', 'end-1c') != ""): |
| 50 | + stxt.delete(1.0, END) |
| 51 | + stxt.insert(END,dtl.simple_detect(content1)) #simple_detect function just returns language code |
| 52 | + stxt['state'] = "disabled" |
| 53 | + else: |
| 54 | + mbox.showerror("Input Error","Input can't be empty!") |
| 55 | + else: |
| 56 | + if file_entry.get()!="": #check if the file is selected or not |
| 57 | + content1 = content |
| 58 | + if(content1==""): #check if file has content or not |
| 59 | + mbox.showerror("File Error","File has no content!") |
| 60 | + else: |
| 61 | + stxt['state'] = 'normal' |
| 62 | + if(stxt.get('1.0', 'end-1c')!=""): |
| 63 | + stxt.delete(1.0,END) |
| 64 | + stxt.insert(END,dtl.simple_detect(content1)) |
| 65 | + stxt['state'] = "disabled" |
| 66 | + else: |
| 67 | + mbox.showerror("File Error","File not selected!") |
| 68 | + |
| 69 | +##---------GUI STARTS HERE--------## |
| 70 | +window = tk.Tk() |
| 71 | +window.title("Detect Language") |
| 72 | +window.config(bg="white") |
| 73 | +window.geometry("1000x700") |
| 74 | + |
| 75 | +## SECTION 1 : TAKING STATEMENT INPUT FROM USER |
| 76 | +l1 = Label(window, text="Enter your text here:", bg="white") #label 1 |
| 77 | +l1.place(x=10,y=60,height=20,width=200) |
| 78 | +l1['font'] = font.Font(size=15) |
| 79 | + |
| 80 | +entry_1 = Entry(window, bg="#9EF281") #entry_1 |
| 81 | +entry_1.place(x=230, y=55, height=30, width=300) |
| 82 | +entry_1['font'] = font.Font(size=15) |
| 83 | + |
| 84 | +clear_btn = Button(window, bg="yellow", text="Clear Input", command=clear_input) #clear input button |
| 85 | +clear_btn.place(x=280, y=95) |
| 86 | + |
| 87 | +activate_btn = Button(window, bg="#FFCB64", text="Activate Input", command=activate_input) #clear input button |
| 88 | +activate_btn.place(x=390, y=95) |
| 89 | + |
| 90 | +## SECTION 2 : TAKING FILE INPUT FROM USER |
| 91 | +l2 = Label(window, text="Detect from (.txt) file:", bg="white") #label2 |
| 92 | +l2.place(x=10, y=180, height=20, width=200) |
| 93 | +l2['font'] = font.Font(size=15) |
| 94 | + |
| 95 | +file_entry = Entry(window, bg="#9EF281") #entry_2 |
| 96 | +file_entry.place(x=230, y=175, height=30, width=300) |
| 97 | +file_entry['font'] = font.Font(size=15) |
| 98 | +file_entry['state'] = "disabled" |
| 99 | + |
| 100 | +file_btn = Button(window, bg="yellow", text="Choose File", command=choose_file) #choose file button |
| 101 | +file_btn.place(x=345, y=220) |
| 102 | + |
| 103 | +## SECTION 3 : DETECT BUTTON & OUTPUT WINDOW |
| 104 | +detect_button = Button(window, bg="green", text="Detect Language", fg="white", command=detect) #detect lang button |
| 105 | +detect_button.place(x=750, y=55) |
| 106 | +stxt = scrolledtext.ScrolledText(window, |
| 107 | + wrap = tk.WORD, |
| 108 | + width = 40, |
| 109 | + height = 10, |
| 110 | + font = ("Times New Roman", |
| 111 | + 15)) |
| 112 | +stxt.place(x=650,y=100, height=140, width=300) |
| 113 | +stxt['state'] = "disabled" |
| 114 | + |
| 115 | +## SECTION 4 : REFERENCE SECTION FOR LANGUAGE CODES |
| 116 | +frame = Frame(window) |
| 117 | +frame.place(x=35,y=350,height=260, width=930) |
| 118 | + |
| 119 | +ref_label = Label(window,text="Reference for Language Codes: ", bg='white', fg='green') |
| 120 | +ref_label['font'] = font.Font(size=15) |
| 121 | +ref_label.place(x=20, y=300) |
| 122 | + |
| 123 | + |
| 124 | +# Constructing treeview for displaying codes from csv file in table format in tkinter |
| 125 | +with open(".\Related\languages.csv",newline='\n') as csvfile: #takes filename of languages.csv from "Related" folder |
| 126 | + data_rows = csv.reader(csvfile) |
| 127 | + tree = ttk.Treeview(frame,column=('col1','col2'), show='headings',height=10) |
| 128 | + tree.column("# 1", anchor=CENTER) |
| 129 | + tree.heading("# 1", text="CODE") |
| 130 | + tree.column("# 2", anchor=CENTER) |
| 131 | + tree.heading("# 2", text="LANGUAGE") |
| 132 | + |
| 133 | + for row in data_rows: |
| 134 | + tree.insert('', 'end', text="1", values=(row[0],row[1])) |
| 135 | + tree.place(x=0,y=0,height=260, width=930) |
| 136 | + |
| 137 | +window.mainloop() |
| 138 | +##------GUI ENDS HERE--------## |
0 commit comments