I am currently working on a project, a windows operating system software created using python/SQL. Basically, the GUI of the software has features to add, remove, modify and search users that are being stored into a local MySQL database. The connection between the software and the database seems all right, I can add users, delete them, update / modify specific data like their birth date, address, email address, mobile phone, post code and so on, and also it has a feature where I can search specific user in the database from the GUI using either their reference number or their mobile number.

I recently had problem with the search functionality only, where the problem was assigning wrong values in this line of code:

combobox_search_by["value"] = ("Mobile Number", "Reference Number")

which is part of the following block with now fixed values that actually work:

self.variable_search_by = StringVar()
        combobox_search_by = ttk.Combobox(table_frame, textvariable = self.variable_search_by, font = ("times new roman", 12, "bold"), width = 12, state = "readonly")
        combobox_search_by["value"] = ("mobile_no_column_mysql", "reference_no_column_mysql")
        combobox_search_by.current(0)
        combobox_search_by.grid(row = 0, column = 1, padx = 2)

The code block shown above is the one that is finally functioning properly after changing "Mobile Number" and "Reference Number" into the actual respective table columns in MySql table, but there is one minor follow up problem to this fix, now in my GUI when I am willing to select between searching user with its mobile number or its reference number, I see 2 choices as it should "mobile_no_column_mysql" and "reference_no_column_mysql" but I would like to see shorter names than these 2, something like "Mobile Number", and "Reference Number". If I just change the values directly, it brakes search functionality so its not that simple. Can anyone tell me if there is a way to assign different name to the Combo Box values without braking search functionality?

I will also provide picture of the Combo Box selector to clarify how it looks now.enter image description here

I was trying to find a solution to this online, but wasn't able to get a proper result, and was afraid to play much with it and break yet another functionality. I am new to programming so please do understand if the solution is easy, I am still learning the essentials.

1

There are 1 best solutions below

0
On

Can anyone tell me if there is a way to assign different name to the Combo Box values without braking search functionality?

code block / method that is using the Combobox valuse: my_cursor.execute("select * from customer_table where "+str(self.variable_search_by.get())+" LIKE '%"+str(self.variable_text_search_by.get())+"%' ")

You can assign the so-called "wrong" values (which aren't wrong, since you want them displayed) to combobox_search_by["value"], and translate the selected value to the needed column name by replacing str(self.variable_search_by.get()) with

{   "Mobile Number": "mobile_no_column_mysql",
 "Reference Number": "reference_no_column_mysql"}[variable_search_by.get()]

or

dict(zip(combobox_search_by["value"], ("mobile_no_column_mysql",
                                    "reference_no_column_mysql")))[variable_search_by.get()]

(whatever you like better).