Opening Tkinter Color Chooser Without Button

183 Views Asked by At

Python: I need to use a color chooser for my coding class right now. However, the button is making it super difficult to incorporate in my code. Is there anyway to open a color chooser without a button in Python? I need to set a variable equal to the color choosers position. This is my code as of now.

1

There are 1 best solutions below

1
Mykola On

Not quite sure how you want to call askcolor() without button or any other widget.

I would suggest simple solution, when command of the button is bound to function which calls askcolour() dialog and assignes colour to variable if value is choosen.

from tkinter import Tk, Button, Canvas
from tkinter.colorchooser import askcolor
    
class AskColor:

    def __init__(self, master):

        self.master = master
        self.colour = (None,None)
        self.button = Button(self.master, text='Colour', command=self.button_command)
        self.indicator_creation()
        self.button.pack()

    def indicator_creation(self):

        self.indicator = Canvas(self.master)
        self.oval = self.indicator.create_oval(50, 50, 100, 100)
        self.indicator.itemconfig(self.oval, fill=self.colour[1])
        self.indicator.pack()

    def button_command(self):

        chosen_colour = askcolor()
        if chosen_colour != (None,None):
            self.colour = chosen_colour
        self.indicator.itemconfig(self.oval,fill=self.colour[1])
if __name__ == '__main__':

    window = Tk()
    AskColor(window)
    window.mainloop()

After colour selection

Before colour selection