I'm trying to Add a method to create a random size and position rectangle and add it to the Python list created in my constructor, The colour of the rectangles are based on the rainbow colours (‘red’, ‘orange’ ‘yellow’ ‘green’, ‘blue’ and ‘purple’)
this is the current code I have, any help would be great thanks ! new here.

from tkinter import *
import random

root = Tk()

class Recta:
    def __init__(self, height, width, colours = []):
        self.height=80
        self.width=100

    def randomRects(self,canvas):
        w = random.randrange(100)
        h = random.randrange(80)
        canvas.create_rectangle(0,0,h,w,fill='green')
        frame = Frame(root, bg='grey', width=400, height=40)
        frame.pack(fill='x')
        frame = Frame(root, bg='grey', width=400, height=40)
        frame.pack(fill='x')
        button1 = Button(frame, text='Add Rect')
        button1.pack(side='left', padx=10)
        button2 = Button(frame, text='Remove Rect')
        button2.pack(side='left')
        button3 = Button(frame, text = 'Add Circle')
        button3.pack(side = 'left')
        button4 = Button(frame, text = 'Remove Circle')
        button4.pack(side= 'left')
        button5 = Button(frame, text= 'Add Arc')
        button5.pack(side = 'left')
        button6 = Button(frame, text = 'Remove Arc')
        button6.pack(side ='left') 

c = Canvas(root)
c.pack()

tes = Recta(10,20)
tes.randomRects(c)

root.mainloop()

Very nice program for python.

This may not work for the kind of program you're writing (I rarely use Tkinter, more often Pygame) but one thing you could do is use random.randint() and then convert the numbers gained from that into a variable, like so:

import tkinter
import random

leftcoords = random.randint(1000)
topcoords = random.randint(1000)

Obviously you would have to edit this to fit it into your program and Tkinter itself ( like I said, not a master) but this should give you a general idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.