I am trying to create something similar to Google search. I have created dynamic checkboxes based on the search result from Entry widget and trying to print content of the selected checkbox on the text widget. But all the file's content of the search result are getting printed altogether when pressed enter on the Entry field. And nothing happens when clicked on any of the checkboxes.
Output :-
file data issue — Postimage.org[
^]
Batch Rollback(file 1) content -->
pyt_batch_id :- pyt_batch_id
Here multilines of
sql statements
the main pyt_batch_id
task is to
replace pyt_batch_id
from its second
pyt_batch_id occurence
Tiaflex batch rollback(file2) content -->
pyt_batch_id :- pyt_batch_id
pyt_ply_no :- pyt_ply_no
pyt_clm_no :- pyt_clm_no
----------------END OF INPUTS---------------
The provided batch ids are (pyt_batch_id)
The provided ply numbers are (pyt_ply_no)
The provided clm numbers are (pyt_clm_no)
(pyt_batch_id) are in process.
(pyt_batch_id) completed.
(pyt_ply_no) are in process.
(pyt_ply_no) completed.
(pyt_clm_no) are in process.
(pyt_clm_no) completed.
In the statement
chk = Checkbutton(search_result, text = sheet.row_values(result[0])[1],variable = sheet.row_values(result[0])[0], onvalue = 1, offvalue = 0, command = show(sheet.row_values(result[0])[2]))
, the
command = show(sheet.row_values(result[0])[2])
should execute only when a checkbox is checked, but it is executing at the time of checkbox creation
In short I am trying to print content of file only when a checkbox is checked but it is getting displayed at the time of checkbox creation. Kindly assist me with this.
What I have tried:
from tkinter import *
import shutil
import os
from datetime import datetime
import xlrd
from os import listdir
from os.path import isfile, join
import pyperclip
check_box_list = []
def show(file_path):
with open(file_path, 'r') as f:
file_data.insert(INSERT, f.read())
def search_datafix(search_string_parm):
path = "DATAFIX_LIST.xlsx"
search_string = search_string_parm
open_file(path, search_string)
def open_file(path, search_string):
wb = xlrd.open_workbook(path)
sheet = wb.sheet_by_index(0)
search_string_list = search_string.split()
results = []
for row_num in range(sheet.nrows):
row_value = sheet.row_values(row_num)
keyword_match = 0
for keyword in search_string_list:
if keyword.lower() in row_value[1].lower():
keyword_match += 1
if keyword_match:
results.append([row_num, keyword_match])
results.sort(key = lambda results: results[1], reverse=True)
i = 0
checkbox_dict = dict()
for result in results:
sheet.row_values(result[0])[0] = IntVar()
chk = Checkbutton(search_result, text = sheet.row_values(result[0])[1],variable = sheet.row_values(result[0])[0], onvalue = 1, offvalue = 0, command = show(sheet.row_values(result[0])[2]))
chk.grid(row = i, column = 0)
check_box_list.append(chk)
i = i+1
def search_handle_focus_in(_):
search_box.delete(0, END)
search_box.config(fg='black')
def search_handle_focus_out(_):
search_box.delete(0, END)
search_box.config(fg='grey')
search_box.insert(0, "Example: Batch Rollback")
def search_handle_enter(txt):
for i in check_box_list:
i.destroy()
search_datafix(search_box.get())
window = Tk()
window.title("Datafix Generator")
window.geometry('1300x660')
window.resizable(0,0)
menu_frame = Frame(window, width = 400, height = 100)
menu_frame.grid(row = 0, column = 0)
search_icon = Button(menu_frame, text = 'Search Datafix', borderwidth = 0, relief=RAISED)
search_img = PhotoImage(file ='search_datafix_7.png')
search_icon.config(image=search_img)
search_icon.grid(row = 0, column = 0, padx = (100,20))
main_search_frame = LabelFrame(window, text = 'Main Search Frame', width=200, height=100)
main_search_frame.grid(row = 1, column = 0, pady = 10)
search_frame = LabelFrame(main_search_frame, text = 'Search Bar Frame', width=200, height=100)
search_frame.grid(row = 0, column = 0, pady = 10)
search_box = Entry(search_frame, bd = 5, bg='white', width=100, fg='grey')
search_box.grid(row = 0, column = 0, columnspan=2)
search_box.insert(0, "Example: Batch Rollback")
search_box.bind("<FocusIn>", search_handle_focus_in)
search_box.bind("<FocusOut>", search_handle_focus_out)
search_box.bind("<Return>", search_handle_enter)
search_result = LabelFrame(main_search_frame, text = 'Search Results Frame', width = 400, height = 400)
search_result.grid(row = 1, column = 0, pady = 10)
file_content_frame = LabelFrame(main_search_frame, text = 'File Content Frame', width = 400, height = 400)
file_content_frame.grid(row = 1, column = 1, padx = 10, pady = 10)
file_data = Text(file_content_frame)
file_data. grid(row = 0, column = 0)
window.mainloop()