# Map Tk to WxRuby

**URL:** <https://www.ruby-forum.com/t/map-tk-to-wxruby/172692>\
**Category:** wxRuby\
**Created:** [July 25, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/map-tk-to-wxruby/172692 "2009-07-25T18:54:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![quann2007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/4af34b/32.png) [@quann2007](https://www.ruby-forum.com/u/quann2007)\
**Post date:** [July 25, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/map-tk-to-wxruby/172692/1 "2009-07-25T18:54:44Z")

</div>

Hello,

I try to map a Tk to Wx for the simple animation example below. The code  
for Wx is way too long. Can anyone help me to come up with the shorter  
code for wx? Either in WxRuby or WxPython would be fine. Wonder is there  
a script out there (or an easier or convenient way) to map Tk script to  
Wx script for both WxPython and WxRuby?

-Quan Nguyen

############################################################################  
Except from “Scipting Languages by Examples”  
" In the example of “sun\_moving” in section 4.3.1, we have shown that we  
can create a simple moving object in Tk with only few lines of code.  
Yes, with a mere few lines of code we can create and emulate the moving  
circles to animate the sun coming toward us. The complete Ruby and  
Python code in Tk are re-displayed below. How can we repeat the same  
function in Wx? The example may not be fair for comparision purpose but  
it will highlight the different characteristics of Tk and Wx. One thing  
for sure is that it will take a lot more lines of code in Wx script. "  
##############################################################################  
#!/usr/bin/python #Python: sun coming with Tk  
4.3.1

from Tkinter import \*

mw=Tk()  
c=Canvas(mw,width=300,height=300)  
c.pack()

for i in range(1000):  
x0,x1=i/2,i+i/2  
c.create\_oval(x0, x0, x1, x1, fill=‘red’)  
c.update()

mainloop()  
##############################################################################  
#!/usr/bin/ruby #Ruby: sun coming with Tk 4.3.1

require ‘tk’  
c,k = TkCanvas.new.pack,0

TkAfter.new(1,1000,proc{TkcOval.new(c,k_0.5,k_0.5,k_1.5,k_1.5,‘fill’=\>‘red’);  
k+=1}).start

Tk.mainloop

* * *

#!/usr/bin/ruby #wxRuby: sun coming with Wx  
require ‘wx’ #

class MyApp \< Wx::App #  
def on\_init #  
frame = Wx::Frame.new(nil, :size =\> [300, 300]) #  
canvas = Wx::Panel.new(frame, :size =\> [300, 300]) #  
frame.show

```
buffer = Wx::Bitmap.new(300, 300) #storage buffer

canvas.evt_paint do |event| #evt_paint
  canvas.paint do |dc| #
    buffer.draw do |buf_dc| #dump buffer out
      dc.blit(0, 0, 300, 300, buf_dc, 0, 0) #start bitmap writing
    end #
  end #
end #

100.times do |i| #timng loop
  buffer.draw do |dcx| #clear bg trails
    dcx.draw_rectangle(0, 0, 300, 300) #
  end #

  buffer.draw do |dcy| #drawing into buffer
    dcy.brush = Wx::RED_BRUSH #
    dcy.draw_circle(2*i,2*i, i) #
  end #

  canvas.refresh #refresh
  canvas.update #
  sleep 0.1 #

end #

```

end #  
end #

app = MyApp.new #  
app.main\_loop #

#############################################################################
