Python Forum
Insert color in tkinter automatically
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert color in tkinter automatically
#1
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?
Reply
#2
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).
Reply
#3
(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).
Reply
#4
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
Reply
#5
Thank you wuf
This code doesn't do precisely what I wanted, but I think I can correct my mistakes now.

Greetings,
Dwimmerlaik
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 4,194 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,629 May-23-2022, 03:44 PM
Last Post: bigmac
  Can't get tkinter button to change color based on changes in data dford 4 3,361 Feb-13-2022, 01:57 PM
Last Post: dford
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 5,055 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  tkinter | Button color text on Click Maryan 2 3,314 Oct-09-2020, 08:56 PM
Last Post: Maryan
  tkinter| listbox.insert problem Maryan 3 3,437 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  [tkinter] color change for hovering over button teacher 4 8,325 Jul-04-2020, 06:33 AM
Last Post: teacher
  TKINTER - Change font color for night or day Ayckinn 2 3,817 May-24-2020, 09:25 PM
Last Post: Ayckinn
  Tkinter help (color) Florent 2 2,278 Mar-01-2020, 02:59 PM
Last Post: Florent
  Restoring Tkinter widget background to original color pythonprogrammer 1 2,909 Dec-16-2019, 04:59 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020