Search code examples
tkinteroptionmenu

tkinter - Only last option in definition is changing with dropdown selection


I'm a total newbie to programming in tkinter and python - just started programming a week ago. Any help would be greatly appreciated!

When making a selection in the dropdown menu, only the last option of "D CAM-Green" is being formatted after making a selection with the definition below. The other 3 options do not change after selecting them from the dropdown list.

How do I fix this so that each respective option from the dropdown list changes to the format defined when selected?

from tkinter import *
import tkinter as tk
from tkinter import ttk 

root = Tk()
root.title("Steve's Prototype")
root.geometry("800x800")
root.configure(bg='black')

def background(event):
    if var1.get() == "A CAM-Red":
       drop1.configure(font="Carlito 44 bold",
       highlightbackground="red",
       highlightthickness=10,
       fg="red",
       bg="black",
       anchor="w")
    else:
       drop1.configure(font="Carlito 44 bold",
       highlightbackground="black",
       highlightthickness=0, 
       fg="white",
       bg="black",
       anchor="w")
        
    if var1.get() == "B CAM-Blue":
       drop1.configure(font="Carlito 44 bold",
       highlightbackground="blue",
       highlightthickness=10,
       fg="blue",
       bg="black",
       anchor="w")
    else: 
       drop1.configure(font="Carlito 44 bold",
       highlightbackground="black",
       highlightthickness=0,
       fg="white",
       bg="black", anchor="w")
            
    if var1.get() == "C CAM-Yellow":
       drop1.configure(font="Carlito 44 bold",
       highlightbackground="yellow",
       highlightthickness=10,
       fg="yellow",
       bg="black",
       anchor="w")
    else: 
       drop1.configure(font="Carlito 44 bold",
       highlightbackground="black",
       highlightthickness=0,
       fg="white",
       bg="black",
       anchor="w")
   
    if var1.get() == "D CAM-Green":
       drop1.configure(font="Carlito 44 bold",
       highlightbackground="green",
       highlightthickness=10,
       fg="green",
       bg="black",
       anchor="w")
    else: 
       drop1.configure(font="Carlito 44 bold",
       highlightbackground="black",
       highlightthickness=0,
       fg="white",
       bg="black",
       anchor="w")
        
array1 = ["Camera Colors", "A CAM-Red", "B CAM-Blue", "C CAM-Yellow", "D CAM-Green"]
var1 = StringVar()
var1.set(array1[0])
        
drop1 = OptionMenu(root, var1, *array1, command=background)
drop1.grid(row=5, column=0, sticky=E+W, pady=0)
drop1.configure(font="Carlito 44 bold", fg="white", bg="black", anchor="w")

root.mainloop()

Solution

  • You have nesting issues in your code. And no need to repeat else part this much times as your else part content is same.

    def background(event):
        if var1.get() == "A CAM-Red":
           drop1.configure(font="Carlito 44 bold",
           highlightbackground="red",
           highlightthickness=10,
           fg="red",
           bg="black",
           anchor="w")
        
        elif var1.get() == "B CAM-Blue":
            drop1.configure(font="Carlito 44 bold",
            highlightbackground="blue",
            highlightthickness=10,
            fg="blue",
            bg="black",
            anchor="w")
            
        elif var1.get() == "C CAM-Yellow":
            drop1.configure(font="Carlito 44 bold",
            highlightbackground="yellow",
            highlightthickness=10,
            fg="yellow",
            bg="black",
            anchor="w")
    
        
        elif var1.get() == "D CAM-Green":
            drop1.configure(font="Carlito 44 bold",
            highlightbackground="green",
            highlightthickness=10,
            fg="green",
            bg="black",
            anchor="w")
        else: 
            drop1.configure(font="Carlito 44 bold",
            highlightbackground="black",
            highlightthickness=0,
            fg="white",
            bg="black",
            anchor="w")
    

    Replace your background() function with this. It works fine.

    I will suggest you to read the basics of IF-ELIF-ELSE also