Bottom Page

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?
Quote
#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).
Quote
#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).
Quote
#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
Quote
#5
Thank you wuf
This code doesn't do precisely what I wanted, but I think I can correct my mistakes now.

Greetings,
Dwimmerlaik
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [WxPython] How to change font color of SetHint or SetDescriptiveText? anistorian 5 149 Jun-19-2019, 05:30 PM
Last Post: Yoriz
  [Tkinter] text widgets-insert problem genc74 3 153 Apr-27-2019, 04:00 PM
Last Post: Yoriz
  how to insert image into Text widget Tkinter atlass218 5 353 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] So what do I need to do change the color this label? jpezz 10 380 Apr-03-2019, 09:49 PM
Last Post: jpezz
  [Tkinter] color a string of characters in tkinter atlass218 6 313 Mar-28-2019, 03:17 PM
Last Post: atlass218
  [PyQt] pyqt5 comobox insert into mysql Weballround 2 367 Jan-11-2019, 05:56 AM
Last Post: Weballround
  Tkinter Gui ScrolledText Insert From Different Class whisperquiet 1 388 Jan-08-2019, 09:25 PM
Last Post: Larz60+
  [Tkinter] Treeview automatically adjust it's size when pack inside frame Prince_Bhatia 1 3,175 Jul-25-2018, 03:24 AM
Last Post: Larz60+
  GUI insert textbox value into a Class dimvord 0 545 Jul-04-2018, 06:49 PM
Last Post: dimvord
  [Tkinter] insert table columns data in different textboxes dimvord 4 766 Jun-15-2018, 07:40 AM
Last Post: dimvord

Forum Jump:


Users browsing this thread: 1 Guest(s)