How to do it...

As usual, we will trigger the widget configuration with standard buttons—one for each option. The main difference with previous examples is that values can be directly chosen using the askcolor dialog from the tkinter.colorchooser module:

from functools import partial

import tkinter as tk
from tkinter.colorchooser import askcolor

class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Colors demo")
text = "The quick brown fox jumps over the lazy dog"
self.label = tk.Label(self, text=text)
self.fg_btn = tk.Button(self, text="Set foreground color",
command=partial(self.set_color, "fg"))
self.bg_btn = tk.Button(self, text="Set background color",
command=partial(self.set_color, "bg"))

self.label.pack(padx=20, pady=20)
self.fg_btn.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.bg_btn.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

def set_color(self, option):
color = askcolor()[1]
print("Chosen color:", color)
self.label.config(**{option: color})

if __name__ == "__main__":
app = App()
app.mainloop()

If you want to check out the RGB value of a selected color, it is printed on the console when the dialog is confirmed, or none is shown if it is closed without selecting a color.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset