0

I'm trying to create a selection frame, where the user could choose the database to be used and based on that choice, it will show the logo of that database.

My issue comes from the fact that when I select the first time, it works perfectly but if I choose another one, it keeps the first image instead of changing. I've tried with pack_forget() and delete(), but it doesn't seem to work. What should I do to make it work?

db_var = tk.StringVar(settings_window,"0")
n = 0
databases = {"Sql Server"   : "Sql Server",
             "Sqlite 3"     : "Sqlite 3",
             "MySQL"        : "MySQL"}

def db_logo():

    global label_db_image
    label_db_image = tk.Label(settings_right_frame,bg=col_rf)

    logo_mssql  = ImageTk.PhotoImage(Image.open(r"*****SqlServer.png").resize((250,250),Image.ANTIALIAS))
    label_mssql   = tk.Label(settings_right_frame, image = logo_mssql,bg=col_rf)

    logo_sqlite = ImageTk.PhotoImage(Image.open(r"*****Sqlite3.png").resize((250,250),Image.ANTIALIAS))
    label_sqlite  = tk.Label(settings_right_frame, image = logo_sqlite,bg=col_rf)

    logo_mysql = ImageTk.PhotoImage(Image.open(r"*****MySQL.png").resize((250,250),Image.ANTIALIAS))
    label_mysql  = tk.Label(settings_right_frame, image = logo_mysql,bg=col_rf)

    if db_var.get() == "Sql Server":
        label_db_image.destroy()
        label_db_image = label_mssql
        label_db_image.image = logo_mssql
        label_db_image.config(image=logo_mssql)
        label_db_image.pack(pady=70)
    elif db_var.get() == "Sqlite 3":
        label_db_image.destroy()
        label_db_image = label_sqlite
        label_db_image.image = logo_sqlite
        label_db_image.config(image=logo_sqlite)
        label_db_image.pack(pady=70)
    elif db_var.get() == "MySQL":
        label_db_image.destroy()
        label_db_image = label_mysql
        label_db_image.image = logo_mysql
        label_db_image.config(image=logo_mysql)
        label_db_image.pack(pady=70)

label_select_db= tk.Label(settings_top_frame,text="Please select your database software:",bg=col_tf).grid(pady=10,sticky="nw")

label_db_image = tk.Label(settings_right_frame,bg=col_rf)

for (text,value) in databases.items():
    radio=tk.Radiobutton(settings_top_frame,text=text,value=value,variable = db_var, command=db_logo, bg=col_tf).grid(row=2,column=n,rowspan=2,padx=50)
    n += 1
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    It would be a better solution to use only one label and exchange only the image it displays. – Wups Sep 27 '20 at 18:24
  • Take a look at [this](https://stackoverflow.com/questions/63998217/how-to-make-buttons-out-of-list/63999499#63999499), just that there its buttons, here its radiobuttons, try using `lambda` – Delrius Euphoria Sep 27 '20 at 18:30
  • 1
    You can use the universal widget [`config()`](http://effbot.org/tkinterbook/tkinter-widget-configuration.htm) method to change the `image` option of the existing labels instead of trying to replace them. Note that you will need to add a reference to the `PhotoImage` images you specify — see [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function). – martineau Sep 27 '20 at 19:42

0 Answers0