tkinter-定义中只有最后一个选项随下拉选择而更改
我完全是tkinter和python编程的新手——一周前才开始编程。任何帮助都将不胜感激 在下拉菜单中进行选择时,在使用以下定义进行选择后,只有“D CAM绿色”的最后一个选项被格式化。从下拉列表中选择其他3个选项后,它们不会更改 如何解决此问题,使下拉列表中的每个选项更改为选中时定义的格式tkinter-定义中只有最后一个选项随下拉选择而更改,tkinter,optionmenu,Tkinter,Optionmenu,我完全是tkinter和python编程的新手——一周前才开始编程。任何帮助都将不胜感激 在下拉菜单中进行选择时,在使用以下定义进行选择后,只有“D CAM绿色”的最后一个选项被格式化。从下拉列表中选择其他3个选项后,它们不会更改 如何解决此问题,使下拉列表中的每个选项更改为选中时定义的格式 from tkinter import * import tkinter as tk from tkinter import ttk root = Tk() root.title("Steve
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()
函数。它很好用
我建议你也读一些基础知识