6 Replies - 1220 Views - Last Post: 03 February 2015 - 06:12 PM Rate Topic: -----

#1 The Chief  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

tkinter drawing rectangles

Posted 01 February 2015 - 02:25 PM

I'm trying to do a problem in my book that tells me to create a random list of distinct integers from 1 to 20 and draw them in the form of rectangles on a canvas. I have to add a sort and reset method that sorts the rectangles in size order and the rest method that just wipes the list.

Here is what I have so far

from tkinter import *
import random

class Histogram:
    def __init__(self):
        self.height = 300
        self.width = 600
        #Create window
        window = Tk()
        window.title = ("Histogram")

        self.canvas = Canvas(window, bg = "white", width = self.width, height = self.height)
        self.canvas.pack()

        #Create frame
        frame = Frame(window)
        frame.pack()

        #Create buttons
        buttonSort = Button(frame, text = "Sort", command = self.sort)
        buttonSort.pack(side = LEFT)
        buttonreset = Button(frame, text = "Reset", command = self.reset)
        buttonreset.pack(side = LEFT)
        buttonCreateList = Button(frame, text = "Create List", command = self.createList)
        buttonCreateList.pack(side = LEFT)

        window.mainloop()


    def createList(self):
        self.lst = [x for x in range(1,21)]
        random.shuffle(self.lst)
        #for i in range(len(self.lst)):
            #self.canvas.create_rectangle(self.lst[i], 10, self.lst[i], 10)
        print(self.lst)

    def sort(self):
        self.lst.sort()
        print("List sorted")
        print(self.lst)

    def reset(self):
        self.lst = []

        print("List reset")
        print(self.lst)

Histogram()



I'm having some serious issues when it comes to drawing the rectangles on the screen. How can I draw them so that each rectangle drawn is the length of the corresponding integer?

So a random list might look like this

[1,3,5,7,9,2,4,6,8.....]
So rectangle 1 would be length 1 width 0.5
rectangle 2 would be length 3 width 0.5
etc

The when I press the sort button it sorts them in size order and repaints them. This is probably out of my depth but I thought I would have a bash at the problem anyway.

This post has been edited by The Chief: 01 February 2015 - 07:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: tkinter drawing rectangles

#2 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 165
  • View blog
  • Posts: 258
  • Joined: 19-July 09

Re: tkinter drawing rectangles

Posted 02 February 2015 - 05:36 AM

create_rectangle takes four arguments plus some optional arguments. Those four arguments denote the position of the upper left and lower right corner: create_rectangle(x1, y1, x2, y2).
What you did with the comented out self.canvas.create_rectangle(self.lst[i], 10, self.lst[i], 10) is create a rectangle for which the upper left and lower right corner are the same (no wonder you see no rectangle).

An example:
from Tkinter import *

master = Tk()

w = Canvas(master, width=200, height=100)
w.pack()

x, y = 10, 10
width = 25
width2 = 50
height = 5
dist = 10
w.create_rectangle(x, y, x + width, y + height, outline='blue', fill="red")
w.create_rectangle(x, y + height + dist, x + 50, y + height + dist + height) 

mainloop()


Was This Post Helpful? 0
  • +
  • -

#3 The Chief  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

Re: tkinter drawing rectangles

Posted 02 February 2015 - 09:57 AM

That's not really helping me I'm still struggling. Could you perhaps use my code and insert your code so that when I click the createList button it creates 20 rectangles all touching with each rectangle corresponding to it's height? Like this Posted Image

I'm sure I can figure out the sort function to sort them in number of size.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 131
  • Joined: 04-July 11

Re: tkinter drawing rectangles

Posted 02 February 2015 - 07:10 PM

I can see that you're already in the right track.
Your problem is how you use create_rectangle function that's why you commented it.

I edited your createList function in order for it to print the rectangle.

    def createList(self):
        self.lst = [x for x in range(1,21)]
        random.shuffle(self.lst)
        y1 = 250 #constant (base of the histogram)
        x = 20 #constant (width of the histogram)
        for i in range(len(self.lst)):
            self.canvas.create_rectangle(x * (i + 1), y1 - self.lst[i] * 2, x * (i + 2), y1)
        print(self.lst)


Now, as Nallo mentioned, create_rectangle accepts arguments which specifies the position of your rectangle,
x0, y0, x1, y1. x0 and y0 takes the upper left corner of the rectangle and x1 and y1 is for the lower right corner. Use these information to specify the position of your rectangle.

This post has been edited by CreamDelight: 02 February 2015 - 07:11 PM

Was This Post Helpful? 1
  • +
  • -

#5 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6504
  • View blog
  • Posts: 26,301
  • Joined: 12-December 12

Re: tkinter drawing rectangles

Posted 03 February 2015 - 03:35 AM

View PostThe Chief, on 02 February 2015 - 04:57 PM, said:

That's not really helping me I'm still struggling. Could you perhaps use my code and insert your code so that when I click the createList button it creates 20 rectangles all touching with each rectangle corresponding to it's height?

Nallo provided "an example". Do not ask for complete code as this is against the forum rules and will lead to your question being closed.
Was This Post Helpful? 0
  • +
  • -

#6 The Chief  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 12-November 14

Re: tkinter drawing rectangles

Posted 03 February 2015 - 07:24 AM

View Postandrewsw, on 03 February 2015 - 03:35 AM, said:

View PostThe Chief, on 02 February 2015 - 04:57 PM, said:

That's not really helping me I'm still struggling. Could you perhaps use my code and insert your code so that when I click the createList button it creates 20 rectangles all touching with each rectangle corresponding to it's height?

Nallo provided "an example". Do not ask for complete code as this is against the forum rules and will lead to your question being closed.


I understand the reason people should not give out full solutions, I too believe we learn more when we put in our own effort but I provided a large chunk of the program in my initial post, I even hinted that there was more code to come (sort) and (reset) methods to which I did not ask for the code.

The actual code CreamDelight provided me was basically 1 line. That makes 47 lines of the program my own code.

I'll try to avoid this in the future though.

Thanks CreamDelight that is perfect, just what I wanted.

This post has been edited by The Chief: 03 February 2015 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

#7 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 131
  • Joined: 04-July 11

Re: tkinter drawing rectangles

Posted 03 February 2015 - 06:12 PM

No problem mate. :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1