0
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?

nbro
  • 15,395
  • 32
  • 113
  • 196
Kara
  • 765
  • 5
  • 11
  • 29

2 Answers2

4

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.

ted
  • 4,791
  • 5
  • 38
  • 84
0

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

Community
  • 1
  • 1
Sami N
  • 1,170
  • 2
  • 9
  • 21