Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox foreground color setting is lost when navigating out through "tab" key

When I navitagate out of a combobox through the "tab" key, the foreground settings is lost. This also happens when "left" and "right" arrow keys are used while inside the combobox. This problem does not occur when I replace "entry" widget with a "button". I could not find any such problem posted. How to resolve this? Here is a sample code.

When "Invalid" is selected, the foreground color should be "red" untill "Valid" is selected. But it does not. Screenshot attached.enter image description here enter image description here

However, when only mouse is used, it works. So i suspect it has something to do with combobox, entry, tabkey combination.

Environment: Python 3.9.6 Windows 21H1 (build 19043.1165)

import tkinter as objTK
from tkinter import ttk as objTTK

# Combobox dropdown event handler
def HandlerValidate(objEvent):
    strValue = vCombobox.get()
    
    if strValue == "Invalid":
        objStyle = objTTK.Style()
        objStyle.map("myCombobox.TCombobox", selectforeground=[('readonly', '!focus', 'red'), \
                                                ('readonly', 'focus', 'red')])
        print("Invalid")
    else:
        objStyle = objTTK.Style()
        objStyle.map("myCombobox.TCombobox", selectforeground=[('readonly', '!focus', 'black'), \
                                                    ('readonly', 'focus', 'white')])        
        print("Valid")
    # End of if
# End of HandlerValidate()

objWindow = objTK.Tk()

objStyle = objTTK.Style()
objStyle.theme_use("clam")

# Set black foreground for entry of combobox
objStyle.map("TCombobox", selectforeground=[('readonly', '!focus', 'black')], \
            selectbackground=[('readonly', '!focus', '#DCDAD5')])

# Create style for combobox
objStyle = objTTK.Style()
objStyle.configure("myCombobox.TCombobox")

# Add combobox widget
vCombobox = objTK.StringVar()
objCombobox = objTTK.Combobox(master=objWindow, width=10, state="readonly", \
                    textvariable=vCombobox, style="myCombobox.TCombobox", values=["Valid", "Invalid"])
objCombobox.current(0)
objCombobox.bind("<<ComboboxSelected>>", HandlerValidate)
objCombobox.place(x=10, y=10)

# Add entry widget
txtEntry = objTK.Entry(master=objWindow, width=5)
txtEntry.place(x=10, y=40)

objWindow.bind("<Escape>", lambda _: objWindow.destroy())
objWindow.geometry("110x90")
objWindow.mainloop()
like image 883
Sandeep S D Avatar asked Oct 27 '22 10:10

Sandeep S D


1 Answers

When you leave (focus out) the combobox, the color used will be foreground, not selectforeground. So you need to configure foreground as well:

def HandlerValidate(objEvent):
    strValue = vCombobox.get()

    if strValue == "Invalid":
        objStyle.map("myCombobox.TCombobox", selectforeground=[('readonly', 'red')],
                                             foreground=[('readonly', 'red')])
    else:
        objStyle.map("myCombobox.TCombobox",
                     selectforeground=[('readonly', '!focus', 'black'), ('readonly', 'focus', 'white')],
                     foreground=[('readonly', '!focus', 'black'), ('readonly', 'focus', 'white')])
    print(strValue)
like image 136
acw1668 Avatar answered Jan 02 '23 19:01

acw1668