Reputation: 145
So I've looked around but there are few questions on styles but non of them answer it.
I can not get style mapping to work. I don't know what I am missing. If you could correct me that would be great, thanks.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
s = ttk.Style()
s.map("C.TFrame",
foreground=[("pressed", "red"), ("active", "blue")],
background=[("pressed", "!disabled", "black"), ("active", "white")])
frame = ttk.Frame(root, style="C.TFrame")
text = ttk.Label(frame, text="This is some really long text\n123...")
frame.grid()
text.grid()
root.mainloop()
Upvotes: 1
Views: 1616
Reputation: 2690
Frame styles do not respond to click events and mouse-over (hover) events. Buttons do. See the code below and try it out. I also made it so that the text widget responds to events in the way you had tried to make the frame respond. Because the text widget is not a themed widget, it cannot be configured using styles, but you can use the tk options
to configure your application similarly to this and keep it in a separate file. But that's a different story.
def configureTextWindow(**kwargs):
for avp in kwargs.items():
attrib, value = avp
text[attrib] = value
s = ttk.Style()
# This won't work because frames don't respond to style events.
s.map("C.TFrame",
foreground=[("pressed", "red"), ("active", "blue")],
background=[("pressed", "!disabled", "black"), ("active", "white")])
# Does work because buttons DO respond to style events.
s.map("C.TButton",
foreground=[("pressed", "red"), ("active", "blue")],
background=[("pressed", "!disabled", "black"), ("active", "white")])
frame = ttk.Frame(root, style="C.TFrame")
button = ttk.Button(frame, style='C.TButton', text='Press or Hover')
button.grid()
text = ttk.Label(frame, text="This is some really long text\n123...")
frame.grid()
text.grid()
# Force configuration on the text widget that mimics the frame style above.
text.bind('<Enter>', lambda event: configureTextWindow(foreground='blue', background='white'))
text.bind('<Leave>', lambda event: configureTextWindow(foreground='black', background=''))
text.bind('<Button-1>', lambda event: configureTextWindow(foreground='red', background='black'))
text.bind('<ButtonRelease-1>', lambda event: configureTextWindow(foreground='blue', background='white'))
root.mainloop()
Upvotes: 1