tkinter - 定义中只有最后一个选项随着下拉选择而改变

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

我是 tkinter 编程的新手,python - 一周前才开始编程。任何帮助将不胜感激!

在下拉菜单中进行选择时,在使用以下定义进行选择后,只有最后一个选项“D CAM-Green”被格式化。其他3个选项在从下拉列表中选择后不会改变。

如何解决此问题,以便下拉列表中的每个选项都更改为选择时定义的格式?

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()

您的代码中存在嵌套问题。并且不需要重复 else 部分那么多次,因为您的 else 部分内容相同。

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")

用这个替换你的 background() 函数。效果很好。

我建议您也阅读 IF-ELIF-ELSE 的基础知识