Combobox-Feld-Hintergrund-Änderung Python
Posted: 22 Dec 2024, 19:26
import tkinter as tk
from tkinter import ttk
class ComboboxColorChanger:
def __init__(self, root):
self.root = root
self.root.title("Combobox Color Change")
self.root.geometry("300x150")
# Create a Combobox
self.combobox = ttk.Combobox(self.root, values=["", "Option 1", "Option 2", "Option 3"], state="readonly")
self.combobox.grid(pady=20, padx=50)
# Bind events to handle user actions
self.combobox.bind("", self.update_combobox_style)
# Initial style configuration
self.set_combobox_background(self.combobox)
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()
Kann mir bitte jemand erklären, warum dieser Code zum dynamischen Ändern der Hintergrundfarbe einer Combobox nicht funktioniert? Danke
from tkinter import ttk
class ComboboxColorChanger:
def __init__(self, root):
self.root = root
self.root.title("Combobox Color Change")
self.root.geometry("300x150")
# Create a Combobox
self.combobox = ttk.Combobox(self.root, values=["", "Option 1", "Option 2", "Option 3"], state="readonly")
self.combobox.grid(pady=20, padx=50)
# Bind events to handle user actions
self.combobox.bind("", self.update_combobox_style)
# Initial style configuration
self.set_combobox_background(self.combobox)
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()
Kann mir bitte jemand erklären, warum dieser Code zum dynamischen Ändern der Hintergrundfarbe einer Combobox nicht funktioniert? Danke