Replace <<ComboboxSelected>> with var.trace_add
Mar 25, 2021 19:08:22 GMT -8
Post by Uncle Buddy on Mar 25, 2021 19:08:22 GMT -8
I put this together with examples from StackOverflow.com. I was looking into how to create something like a <<ComboboxSelected>> virtual event for my new custom combobox. Having no experience with creating virtual events, I couldn't see a path to the goal, but when I looked for information about tracking changes to an Entry or Combobox entry, trace() is what came up. This is pretty slick. What a <<ComboboxSelected>> virtual event does is to run a callback when you select something from the dropdown list. This trace procedure goes a step further and runs the callback if you type into the entry. I don't know if that's good of if it just muddies the waters, because I haven't tried to use if for anything serious yet. But it does interesting things in this example, which changes the background color of the GUI.
This shows that a virtual event is not needed to run a command when the combobox content changes. You can type a color or select one, it works the same either way. The trace() method is deprecated so I'm using trace_add() which replaces it. This example uses a lambda because of the required 3 parameters which are formally name, index, and mode, but can be anything as shown by a, b, c. Similar to the required "event" parameter in an event callback, if you want to add another parameter you have to use a lambda or other trick.
Using the trace, you can either select or type colors, either way. My favorite is goldenrod, because it turns gold when you type the first d, but keep typing and when you type the second d it changes color again. Backspace and when it says "gold" again it changes back to gold.
This shows that a virtual event is not needed to run a command when the combobox content changes. You can type a color or select one, it works the same either way. The trace() method is deprecated so I'm using trace_add() which replaces it. This example uses a lambda because of the required 3 parameters which are formally name, index, and mode, but can be anything as shown by a, b, c. Similar to the required "event" parameter in an event callback, if you want to add another parameter you have to use a lambda or other trick.
Using the trace, you can either select or type colors, either way. My favorite is goldenrod, because it turns gold when you type the first d, but keep typing and when you type the second d it changes color again. Backspace and when it says "gold" again it changes back to gold.
import tkinter as tk
from tkinter import ttk
'''
To use this like <<ComboboxSelected>>, uncomment the trace_add line. To use it with the button, comment the trace_add line.
'''
color_strings = [
'AliceBlue',
'AntiqueWhite',
'Aqua',
'Aquamarine',
'Azure',
'Beige',
'Bisque',
'Black',
'BlanchedAlmond',
'Blue',
'BlueViolet',
'Brown',
'BurlyWood',
'CadetBlue',
'Chartreuse',
'Chocolate',
'Coral',
'CornflowerBlue',
'Cornsilk',
'Crimson',
'Cyan',
'DarkBlue',
'DarkCyan',
'DarkGoldenRod',
'DarkGray',
'DarkGrey',
'DarkGreen',
'DarkKhaki',
'DarkMagenta',
'DarkOliveGreen',
'DarkOrange',
'DarkOrchid',
'DarkRed',
'DarkSalmon',
'DarkSeaGreen',
'DarkSlateBlue',
'DarkSlateGray',
'DarkSlateGrey',
'DarkTurquoise',
'DarkViolet',
'DeepPink',
'DeepSkyBlue',
'DimGray',
'DimGrey',
'DodgerBlue',
'FireBrick',
'FloralWhite',
'ForestGreen',
'Fuchsia',
'Gainsboro',
'GhostWhite',
'Gold',
'GoldenRod',
'Gray',
'Grey',
'Green',
'GreenYellow',
'HoneyDew',
'HotPink',
'IndianRed',
'Indigo',
'Ivory',
'Khaki',
'Lavender',
'LavenderBlush',
'LawnGreen',
'LemonChiffon',
'LightBlue',
'LightCoral',
'LightCyan',
'LightGoldenRodYellow',
'LightGray',
'LightGrey',
'LightGreen',
'LightPink',
'LightSalmon',
'LightSeaGreen',
'LightSkyBlue',
'LightSlateGray',
'LightSlateGrey',
'LightSteelBlue',
'LightYellow',
'Lime',
'LimeGreen',
'Linen',
'Magenta',
'Maroon',
'MediumAquaMarine',
'MediumBlue',
'MediumOrchid',
'MediumPurple',
'MediumSeaGreen',
'MediumSlateBlue',
'MediumSpringGreen',
'MediumTurquoise',
'MediumVioletRed',
'MidnightBlue',
'MintCream',
'MistyRose',
'Moccasin',
'NavajoWhite',
'Navy',
'OldLace',
'Olive',
'OliveDrab',
'Orange',
'OrangeRed',
'Orchid',
'PaleGoldenRod',
'PaleGreen',
'PaleTurquoise',
'PaleVioletRed',
'PapayaWhip',
'PeachPuff',
'Peru',
'Pink',
'Plum',
'PowderBlue',
'Purple',
'Red',
'RosyBrown',
'RoyalBlue',
'SaddleBrown',
'Salmon',
'SandyBrown',
'SeaGreen',
'SeaShell',
'Sienna',
'Silver',
'SkyBlue',
'SlateBlue',
'SlateGray',
'SlateGrey',
'Snow',
'SpringGreen',
'SteelBlue',
'Tan',
'Teal',
'Thistle',
'Tomato',
'Turquoise',
'Violet',
'Wheat',
'White',
'WhiteSmoke',
'Yellow']
caseless_colors = []
for color in color_strings:
color = color.lower()
caseless_colors.append(color)
def callback():
print(var.get())
color = var.get()
if color in caseless_colors:
root.config(bg=color)
root = tk.Tk()
root.geometry('400x700+500+300')
root.title('Simulate <<ComboboxSelected>>')
var = tk.StringVar()
var.trace_add('write', lambda a, b, c, var=var: callback())#
t = ttk.Combobox(root, values=caseless_colors, textvariable=var)
t.pack()
b = tk.Button(root, text='PRESS', command=callback)
b.pack()
root.mainloop()