Using .get() to pull a barcode from a customtkinter.CTkEntry box

ghz 8months ago ⋅ 49 views

This is part of a much larger project I'm working on, but I'm trying to break it down to the most basic question I could ask. How do I write the following line?

mycursor = mydb.cursor()
UPC_entry = customtkinter.CTkEntry(root, placeholder_text = "Barcode")

search = "SELECT Description FROM doitbest WHERE PrimaryUPC = '%s'" 
mycursor.execute(search, [UPC_entry.get()])
myresult = mycursor.fetchall()
for x in myresult:
        print(x)

select_button = customtkinter.CTkButton(root, text="Search", command=search)

I'm trying to use Python to search my MySQL database by using CustomTkinter as a GUI. Long story short, I want to scan a barcode into a CTkEntry box (UPC_entry), press the CTkButton that I made, have it pull the barcode from the CTkEntry box, and then use that barcode to search my database for the relevant information. No matter how I try to write the above line, I keep getting errors. It all comes down to the last part "UPC_entry.get()". I don't know how to get the barcode out of the CTkEntry box (UPC_entry) and use that to search the database. If I replace UPC_entry.get() with a regular barcode (eg. 077922843671), it will come up with the correct information. I just don't understand what I'm doing wrong. Is .get() even the right thing I should be using? I greatly appreciate any suggestions you guys might have.

Answers

It seems like you're trying to retrieve the text entered into the CTkEntry widget (UPC_entry) and use it as a parameter in your SQL query. However, you're encountering issues with getting the text from the CTkEntry widget.

In CustomTkinter, you typically use the get() method to retrieve the text from an CTkEntry widget. However, it seems like the issue might be related to when you're trying to retrieve the text. You're attempting to get the text immediately after creating the CTkEntry widget, which means it will be empty at that time.

To fix this, you should retrieve the text from the CTkEntry widget when the search button is clicked. You can modify your code like this:

import customtkinter

def search():
    # Retrieve the text from the CTkEntry widget when the search button is clicked
    barcode = UPC_entry.get()
    
    # Execute the SQL query with the retrieved barcode
    search_query = "SELECT Description FROM doitbest WHERE PrimaryUPC = %s"
    mycursor.execute(search_query, (barcode,))
    myresult = mycursor.fetchall()
    
    # Process the results
    for x in myresult:
        print(x)

# Assuming mydb and mycursor are already initialized

# Create the CTkEntry widget
UPC_entry = customtkinter.CTkEntry(root, placeholder_text="Barcode")

# Create the CTkButton widget for searching
select_button = customtkinter.CTkButton(root, text="Search", command=search)

In this modified version of your code, I've defined a function called search() that will be executed when the search button is clicked. Inside this function, I retrieve the text from the CTkEntry widget using UPC_entry.get() and then execute the SQL query with this retrieved barcode.

Make sure to bind the search() function to the search button using the command parameter, as shown in your original code snippet. This way, when the search button is clicked, the search() function will be called, and the SQL query will be executed with the retrieved barcode.