0

Let me give you some context first.
I am making a project using tkinter, python and MySQL. In my project, every time the user clicks on create button in the tkinter window(which i will be calling root henceforth), button is created and displayed in the root window.

#Creating the button for that specific date
btn = tk.Button(root, text=dateoftable)       #dateoftable contains some string value
btn.number=btnnumber                          #btnnumber is a unique number for each button that increments by one everytime a new button is created
btn.place(x=x, y=y, width=100, height=100)    #x and y are predefined variables

#Adding the button object to the dictionary
tables[btnnumber]=btn                         #tables is a dictionary

So all the created button objects are stored in the tables dictionary.
Now, every time a button is added onto the root window, the tkinter button object is inserted int the MySQl table which has btnobject(Primary Key) anddateoftable as the names of the columns of the table timetablebtn. Just assume dateoftable to contain non-unique arbitrary string values.

lastelem = list(tables.values())[-1]     #gets the most recently added button object on the root window
#**The problem is in the below statement**
config.cursorobj.execute("INSERT INTO timetablebtn VALUES ('{0}','{1}')".format(str(lastelem), dateoftable))
config.mysqlobj.commit()

As you can see, I am storing the button object as a string in the MySQL table, which is a problem because, I cannot reference it again when I call it from the database, it just becomes a dummy string value.
So i want to know if there is a way to store objects onto MySQL. If that's not possible, I want to know if there is a way to reference the same button object when I retrieve the value from my table in the database.
If any more information is required, I will edit the question and code and post it. Thanks in advance.

5
  • Why not just store the values of all attributes you need to fully recreate the object? Plenty of options to do so. Commented Sep 21, 2021 at 18:11
  • Another option - stackoverflow.com/q/8150078/4046632 Commented Sep 21, 2021 at 18:13
  • @buran how do I store the attributes separately and later on link it to the btnobject that I am recieving from my database. Since I am recieving the object as a string literal how do I convert it back to an object and also how do I make sure that it is pointing to the same button as it was, when it was created. Also I am not aware of OOP concepts in python. Commented Sep 21, 2021 at 18:17
  • If you are not familiar with OOP it will be difficult. Every object has attributes, e.g. text, potion, size, etc. - store all the values for all attributes in a database and then reconstruct the button object. Or look at the other option that uses pickle Commented Sep 21, 2021 at 18:33
  • Saving the button object (assume it can be) is not practical because some of the options may not be the same (or even does not exists) when you load back the object, for example references to function, image and tkinter variable, etc. Commented Sep 22, 2021 at 2:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.