Python Forum
Insert color in tkinter automatically - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Insert color in tkinter automatically (/thread-15162.html)



Insert color in tkinter automatically - Dwimmerlaik - Jan-06-2019

Hi,

I've been trying to insert a hexadecimal number in an image made in tkinter, but I just can't seem to get it right. The idea is that a random sized square appears, then the user is asked which color the square should be via a button and finally the color should be inserted. This is what I have thus far:

from tkinter import *
import random
tk = Tk()
canvas = Canvas(tk, width=400,height=400)
canvas.pack()

def random_sqaure(width, height, fill_color):
    x1 = random.randrange(width)
    y1 = random.randrange(height)
    x2 = random.randrange(x1 + random.randrange(width))
    y2 = random.randrange(y1 + random.randrange(height))
    canvas.create_rectangle(x1, y1, x2, y2, fill=fill_color)


from tkinter.colorchooser import *
def getColor():
    color = askcolor() 
    print (color)
Button(text = 'Choose color', command=getColor).pack()

c = getColor()
random_square(400, 400, c)
Can somebody help fix this or am I going about this the wrong way?


RE: Insert color in tkinter automatically - woooee - Jan-06-2019

Where do you actually try to change the color the of square? You first have to save a reference to the rectangle, and second use itemconfigure and that reference to change the color (many, many examples on the web).


RE: Insert color in tkinter automatically - Dwimmerlaik - Jan-07-2019

(Jan-06-2019, 05:40 PM)woooee Wrote: Where do you actually try to change the color the the square? You first have to save a reference to the rectangle, and second use itemconfigure and that reference to change the color (many, many examples on the web).



RE: Insert color in tkinter automatically - wuf - Jan-07-2019

Hi Dwimmerlaik

Here one of many solutions:
import random
import tkinter as tk
from tkinter.colorchooser import *

APP_TITLE = "Choose Color"

APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 350
APP_HEIGHT = 200


class Application(object):

    def __init__(self, main_win):
        self.main_win = main_win
        
        self.build()
        
    def build(self):
        main_frame = tk.Frame(self.main_win)
        main_frame.pack(fill='both', expand=True)
        
        tk.Button(main_frame, text = 'Choose color', command=self.getColor
            ).pack(pady=4)

        self.canvas = tk.Canvas(main_frame, width=400, height=400)
        self.canvas.pack()

    def random_sqaure(self, width, height, fill_color):
        x1 = random.randrange(width)
        y1 = random.randrange(height)
        x2 = random.randrange(x1 + random.randrange(width))
        y2 = random.randrange(y1 + random.randrange(height))
        canvas.create_rectangle(x1, y1, x2, y2, fill=fill_color)

    def getColor(self):
        color = askcolor()[1]
        print(color) 
        self.canvas['bg'] = color
        

def main():
    main_win = tk.Tk()
    main_win.title(APP_TITLE)
    #main_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    #main_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    
    app = Application(main_win)
    
    main_win.mainloop()
 
 
if __name__ == '__main__':
    main()
wuf Wink


RE: Insert color in tkinter automatically - Dwimmerlaik - Jan-07-2019

Thank you wuf
This code doesn't do precisely what I wanted, but I think I can correct my mistakes now.

Greetings,
Dwimmerlaik