【发布时间】:2021-10-13 03:34:07
【问题描述】:
当我通过“tab”键导航出组合框时,前台设置会丢失。当在组合框内使用“左”和“右”箭头键时,也会发生这种情况。当我用“按钮”替换“条目”小部件时,不会发生此问题。我找不到发布的任何此类问题。如何解决这个问题?这是一个示例代码。
选择“无效”时,前景色应为“红色”,直到选择“有效”。但事实并非如此。附上截图。
但是,当只使用鼠标时,它可以工作。所以我怀疑它与组合框、条目、tabkey 组合有关。
环境:Python 3.9.6 Windows 21H1(内部版本 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()
【问题讨论】:
标签: python-3.x tkinter