當我通過“tab”鍵導航出組合框時,前臺設定丟失了。在組合框內使用“左”和“右”箭頭鍵時也會發生這種情況。當我用“按鈕”替換“條目”小部件時不會發生此問題。我找不到任何發布的此類問題。如何解決這個問題?這是一個示例代碼。
選擇“無效”時,前景色應為“紅色”,直到選擇“有效”。但事實并非如此。附上截圖。

但是,當只使用滑鼠時,它可以作業。所以我懷疑它與組合框、條目、選項卡組合有關。
環境: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()
uj5u.com熱心網友回復:
當您離開(聚焦)組合框時,使用的顏色將是foreground,而不是selectforeground。所以你還需要配置foreground:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323813.html
上一篇:匹配分數的regex運算式
