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


Messages In This Thread
Insert color in tkinter automatically - by Dwimmerlaik - Jan-06-2019, 02:39 PM
RE: Insert color in tkinter automatically - by wuf - Jan-07-2019, 12:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 4,447 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,781 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,429 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,127 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  tkinter | Button color text on Click Maryan 2 3,376 Oct-09-2020, 08:56 PM
Last Post: Maryan
  tkinter| listbox.insert problem Maryan 3 3,524 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  [tkinter] color change for hovering over button teacher 4 8,533 Jul-04-2020, 06:33 AM
Last Post: teacher
  TKINTER - Change font color for night or day Ayckinn 2 3,884 May-24-2020, 09:25 PM
Last Post: Ayckinn
  Tkinter help (color) Florent 2 2,334 Mar-01-2020, 02:59 PM
Last Post: Florent
  Restoring Tkinter widget background to original color pythonprogrammer 1 2,974 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