[Python] New to Tkinter and Threading, Program is freezing up at an odd moment without...

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have a GUI for scraping PDF's and one or two other functionalities. I've tested each individually, and when I query a local database and update the GUI with the results the program works. I can do this multiple times without issue. When I use the other functionality and scrape the PDF and update the GUI with the results that works fine and can be done repeatedly. I can scrape the PDF and then search and both work fine. But for some reason when I search and then scrape the program freezes and I have to force it closed. I think I've narrowed it down to if the search updates the GUI with labels. Because if I do a search that yields no results, the program will continue to run fine. These are my function calls:

    def product_search(self):
    global products
    try:
    if self.old_file_name.get() != 'Old SDS':
    self.disable_input()
    #Add clearing the GUI first here
    for child in self.Products_display.winfo_children(): # Iterate over existing children
    if isinstance(child, ctk.CTkLabel): # Check if the child is a CTkLabel
    child.destroy() # Destroy (delete) the label
    with concurrent.futures.ThreadPoolExecutor() as executor_search:
    try:
    #self.add_bar()
    future_search = executor_search.submit(search, self.old_file_name.get())
    products = future_search.result()
    print("Succesfully Searched and closed")
    except Exception as e:
    self.item_label1 = ctk.CTkLabel(self.Products_display, text = 'Product Name')
    self.item_label1.grid(row=0, column=0, padx=10, pady=10, sticky='nsew')
    self.item_label2 = ctk.CTkLabel(self.Products_display, text = 'PNUM')
    self.item_label2.grid(row=0, column=1, padx=10, pady=10, sticky='nsew')
    self.item_label3 = ctk.CTkLabel(self.Products_display, text = 'PMAST')
    self.item_label3.grid(row=0, column=2, padx=10, pady=10, sticky='nsew')
    messagebox.showerror("Error", "Failed to search products: " + str(e))
    if isinstance(products, str):
    messagebox.showerror("Error", "Failed to search products: " + str(products))
    self.enable_input()
    #self.remove_bar()
    return
    elif products.empty:
    messagebox.showinfo("Results", "No Products Found")
    self.item_label1 = ctk.CTkLabel(self.Products_display, text = 'Product Name')
    self.item_label1.grid(row=0, column=0, padx=10, pady=10, sticky='nsew')
    self.item_label2 = ctk.CTkLabel(self.Products_display, text = 'PNUM')
    self.item_label2.grid(row=0, column=1, padx=10, pady=10, sticky='nsew')
    self.item_label3 = ctk.CTkLabel(self.Products_display, text = 'PMAST')
    self.item_label3.grid(row=0, column=2, padx=10, pady=10, sticky='nsew')
    self.enable_input()
    #self.remove_bar()
    return
    else:
    print('found products')
    self.item_label1 = ctk.CTkLabel(self.Products_display, text = 'Product Name')
    self.item_label1.grid(row=0, column=0, padx=10, pady=10, sticky='nsew')
    self.item_label2 = ctk.CTkLabel(self.Products_display, text = 'PNUM')
    self.item_label2.grid(row=0, column=1, padx=10, pady=10, sticky='nsew')
    self.item_label3 = ctk.CTkLabel(self.Products_display, text = 'PMAST')
    self.item_label3.grid(row=0, column=2, padx=10, pady=10, sticky='nsew')
    index = 1
    if len(products) > 0:
    for row in products.itertuples():
    self.item_label1 = ctk.CTkLabel(self.Products_display, text = row[1])
    self.item_label1.grid(row=index, column=0, padx=10, pady=10, sticky='nsew')
    self.item_label2 = ctk.CTkLabel(self.Products_display, text = row[2])
    self.item_label2.grid(row=index, column=1, padx=10, pady=10, sticky='nsew')
    self.item_label3 = ctk.CTkLabel(self.Products_display, text = row[3])
    self.item_label3.grid(row=index, column=2, padx=10, pady=10, sticky='nsew')
    index += 1
    else:
    self.item_label1 = ctk.CTkLabel(self.Products_display, text = 'None Found')
    self.item_label1.grid(row=1, column=1, padx=10, pady=10, sticky='nsew')
    del index
    else:
    messagebox.showwarning("Warning", "Silly Goose, please select/enter an old SDS file first.")
    except Exception as e:
    messagebox.showerror("Error", "Failed to search products: " + str(e))

    self.enable_input()
    #self.remove_bar()
    print('Done with product search')

    def scrape(self):
    global results
    self.disable_input()
    #self.add_bar()

    try:
    print('Deleting old labels')
    for child in self.Products_display.winfo_children(): # Iterate over existing children
    if isinstance(child, ctk.CTkLabel): # Check if the child is a CTkLabel
    child.destroy() # Destroy (delete) the label

    if self.new_file_path.get() != 'New SDS':
    print('Starting the executor')
    with concurrent.futures.ThreadPoolExecutor() as executor_scrape:
    #self.add_bar()
    future_scrape = executor_scrape.submit(scrape_method, self.new_file_path.get(),word_list,pictodf)
    results = future_scrape.result()
    executor_scrape.shutdown()
    self.update_values(results)
    #self.remove_bar()
    self.enable_input()
    #self.remove_bar()
    self.enable_input()
    except Exception as e:
    messagebox.showerror("Error", "Failed to scrape: " + str(e))


    From what I'm reading, it seems to be a threading issue. I've tried adding shutdown() to be sure the thread is closed and forcing the program to wait a few seconds at different points in the scrape function. But it has no helpful affect and I've tried removing different portions of code. Removing the sections where I add item_label1,2,3 gets the program working. Even if products gets updated. So it seems to be the GUI portion that's causing the problem.

    Continue reading...

Compartilhe esta Página