0

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()
3
Contributors
2
Replies
19
Views
1 Year
Discussion Span
Last Post by Nether_1
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.