Kara
Kara

Reputation: 785

How to draw more than one square in tkinter's Canvas?

from Tkinter import Tk, Canvas


master = Tk()
w = Canvas(master, width=250, height=200)
w.pack()
w.create_rectangle(0, 0, 100, 100, fill="blue", outline = 'blue')
master.mainloop() 

This creates one square/rectangle. How do I create a function so that it will create more than one square?

Upvotes: 1

Views: 18844

Answers (2)

Sami N
Sami N

Reputation: 1180

Read up on how to define functions in Python. I recommend the official tutorial.

Implementing the rectangle as a class (NOTE: For your own sake, read about functions and variables first): Help Creating Python Class with Tkinter

Upvotes: 0

ted
ted

Reputation: 4975

How about calling create_rectangle repeatedly?

from Tkinter import *
master = Tk()

w = Canvas(master, width=250, height=200)
w.create_rectangle(0, 0, 100, 100, fill="blue", outline = 'blue')
w.create_rectangle(50, 50, 100, 100, fill="red", outline = 'blue') 
w.pack()
master.mainloop()

Maybe you should put a little more effort into it, it is not that hard to go from making one to makeing n.

Upvotes: 5

Related Questions