pyFltk scripts
(January 2005)
The scripts in this page have been tested with Python 2.3.3, FLTK 1.1.5, and pyFltk 1.1b3, on a Linux operating system.
This example is a clone of the arc example included in the FLTK distribution.
And here is the python script:
#!/usr/bin/python
from fltk import *
class Drawing(Fl_Widget) :
def draw(self) :
global args
fl_push_clip(self.x(), self.y(), self.w(), self.h())
fl_color(FL_DARK3)
fl_rectf(self.x(), self.y(), self.w(), self.h())
fl_push_matrix()
if args[5] :
fl_translate(self.x()+self.w()/2.0, self.y()+self.h()/2.0)
fl_rotate(args[5])
fl_translate(-(self.x()+self.w()/2.0), -(self.y()+self.h()/2.0))
fl_color(FL_WHITE)
fl_translate(self.x(), self.y())
fl_begin_complex_polygon()
fl_arc(args[0], args[1], args[2], args[3], args[4])
fl_gap()
fl_arc(140, 140, 20, 0, -360)
fl_end_complex_polygon()
fl_color(FL_RED)
fl_begin_line()
fl_arc(args[0], args[1], args[2], args[3], args[4])
fl_end_line()
fl_pop_matrix()
fl_pop_clip()
def slider_cb(widget, v) :
global args, drawing
args[v] = widget.value()
drawing.redraw()
# "storage_area" is a storage array for widgets, so that
# they are not deleted if their variable name is deaffected
storage_area = []
args=[140, 140, 50, 0, 360, 0]
name=["X", "Y", "R", "start", "end", "rotate"]
window = Fl_Window(100,100,300,500)
drawing = Drawing(10, 10, 280, 280)
y = 300
for n in range(6) :
s = Fl_Hor_Value_Slider(50, y, 240,25, name[n])
y = y+25
if n < 3 :
s.minimum(0)
s.maximum(300)
elif n==5 :
pass
s.minimum(0)
s.maximum(360)
else :
pass
s.minimum(-360)
s.maximum(360)
s.step(1)
s.value(args[n])
s.align(FL_ALIGN_LEFT)
s.callback(slider_cb, n)
# store s, so that the s widget is not deleted on next loop
storage_area.append(s)
window.end()
window.show()
Fl.run()
Note: the object contained in the "s" variable (the value slider) must be stored somewhere in order not to be deleted as soon as the variable is deaffected (a segmentation fault may occur otherwise).
In this example, we focus on 2 tricky points:
- A text used in a widget label must be hold in a global variable
- The value of a button widget is a character and can be read using the ord function
#!/usr/bin/python
from fltk import *
# caveat:
# the labeltext trick is necessary so that the memory space for widget.label()
# is not freed when the box_cb function terminates
labeltext = ""
def button_cb(widget) :
global labeltext
# caveat : the value method return a character \0 or \1 !!
# that is why we use ord(widget.value())
labeltext = "value="+str(ord(widget.value()))
widget.label(labeltext)
window = Fl_Window(120, 75)
button = Fl_Light_Button(10, 10, 100, 25, "click")
button.callback(button_cb)
window.end()
window.show()
Fl.run()
This part is just here to show how to position a scroll bar left, right, up or down.
#!/usr/bin/python
from fltk import *
class background(Fl_Widget) :
def draw(self) :
fl_color(FL_DARK3)
fl_rectf(self.x(), self.y(), self.w(), self.h())
window = Fl_Window(100, 100)
window.box(FL_NO_BOX)
scroll = Fl_Scroll(0, 0, 100, 100)
scroll.getScrollbar().align(FL_ALIGN_LEFT+FL_ALIGN_TOP)
inside = background(0, 0, 200, 200)
scroll.end()
window.resizable(scroll)
window.end()
window.show()
Fl.run()