Click here to Skip to main content
14,973,021 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Python
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
Python
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:

Python
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 = [] #variable to hold check box results


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 = [] # holding row_num and keyword match frequency
    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():
                # increasing keyword match frequency if it matches with a record
                keyword_match += 1

        # appending a list containing row_num and no of keywords matched to our
        # list if atleast one keyword is matched
        if keyword_match:
            results.append([row_num, keyword_match])    

    # sorting our results list in descending order of keyword match count
    results.sort(key = lambda results: results[1], reverse=True)

    # printing rows of our "DATAFIX_LIST.xlsx" file on basis of first index 
    # (row num) of all the rows stored
    i = 0
    checkbox_dict = dict()
    for result in results:
        #------------------------------------Dynamic checkbox creation----------------------------------------------#
        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) #adding current check boxes list
        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: #Deleting previous search result
         i.destroy()

    search_datafix(search_box.get())


window = Tk() #Window Creation

#Window Decoration
window.title("Datafix Generator")
window.geometry('1300x660')
window.resizable(0,0)

#Menu Icons
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 Frames
main_search_frame = LabelFrame(window, text = 'Main Search Frame', width=200, height=100)
main_search_frame.grid(row = 1, column = 0, pady = 10)

#Search Field
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 Results
search_result = LabelFrame(main_search_frame, text = 'Search Results Frame', width = 400, height = 400)
search_result.grid(row = 1, column = 0, pady = 10)

#File Display Result
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 Field
file_data = Text(file_content_frame)
file_data. grid(row = 0, column = 0)


window.mainloop()
Posted
Updated 21-Jan-21 7:41am
v2
Comments
Richard MacCutchan 2-Jan-20 5:33am
   
You need to provide more details of which checkbox is used by your print process (which I cannot find in the code above).
Vasudha Dixit 2-Jan-20 7:42am
   
Here the checkbox is getting created dynamically using variable "chk" and print should only happen when a checkbox is being checked. But in my case, the file is getting printed at the time of search process(i.e. when enter is pressed on Entry widget)
Richard MacCutchan 2-Jan-20 9:21am
   
Sorry, but unless you show the actual code and explain where the problem occurs, we cannot offer suggestions.
Vasudha Dixit 2-Jan-20 9:38am
   
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.

I have updated my question content. Kindly revert if the issue is still not clear.
Richard MacCutchan 2-Jan-20 9:57am
   
I can only assume that the command is being called when the button is first created, for some reason. I think you will need to run a few basic tests to see exactly how theses widgets are implemented. You could try a different command that just tests the state of the checkbutton.
Vasudha Dixit 2-Jan-20 22:04pm
   
Thank you very much

1 solution

As far as my understanding goes you cannot declare a function in the command parameter explicitly you need to use lambda to call the function.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900