def set_combobox_background(self, widget): """Set the background color of the Combobox based on its selection state.""" value = widget.get() style = ttk.Style()
if value == "Option 1": # Option 1 selected style.configure("Red.TCombobox", fieldbackground="#ffcccc", background="#ffcccc") widget.configure(style="Red.TCombobox") elif value == "": # No option selected style.configure("CyanLight.TCombobox", fieldbackground="#e0ffff", background="#e0ffff") widget.configure(style="CyanLight.TCombobox") else: # Other options selected style.configure("Default.TCombobox", fieldbackground="white", background="white") widget.configure(style="Default.TCombobox")
def update_combobox_style(self, event=None): """Update the style of the Combobox when selection changes.""" self.set_combobox_background(self.combobox)
# Main application runner if __name__ == "__main__": root = tk.Tk() app = ComboboxColorChanger(root) root.mainloop() [/code] Может кто-нибудь объяснить, почему этот код для динамического изменения цвета фона поля со списком не работает? спасибо