full circle magazine #65
7
HOWTO - PROGRAMMING IN PYTHON 37
The first two lines are required.
They basically say what version of
Kivy to expect. Next we create a
new type of label called
‘BoundedLabel’. The color is set
with RGB values (between 0 and 1,
which can be considered as 100
percent), and as you can see the
blue value is set at 100 percent.
We will also create a rectangle
which is the actual label. Save this
as “transpose.kv”. You must use
the name of the class that will be
using it.
Now that you have that
completed, add the following lines
just before the transpose class to
the source file from last time:
class BoundedLabel(Label):
pass
To make it work, all we need is a
definition. Before we go any
further, add the following line to
the import section:
from kivy.uix.popup import
Popup
This allows us to create the
popup later on. Now, in the
Transpose class, just inside the def
build routine, place the code
shown above right.
The LoadLabels routine will give
us the colored labels
(BoundedLabel) and the swap
ability. You saw most of this last
time. We pass a value to the “w”
parameter to determine which text
is being displayed. The
l=BoundedLabel line is pretty much
the same line from last time, with
the exception that, this time, we
are using a BoundedLabel widget
instead of a Button widget. The
LoadLabels will mainly be called
from the next routine, Swap. Place
this code (shown right) below
LoadLabels.
def LoadLabels(w):
if w == 0:
tex0 = self.text1
tex1 = self.text2
else:
tex0 = self.text3
tex1 = self.text4
for i in range(0,22):
if i <= 12:
if i < 10:
t1 = " " + str(i) + "| "
else:
t1 = str(i) + "| "
t = tex1
else:
t1 = ''
t = ''
l = BoundedLabel(text=t1+t[(i*6): ( i * 6 ) + 7 8 ] , size = ( 7 8 0 , 35),
size_hint=(None,None),h a l i g n = ' l e f t ' ,
font_name='data/fonts/D r o i d S a n s M o n o . t t f ' )
s.add_widget(l)
def Swap(instance):
if self.whichway == 0:
self.whichway = 1
btnWhich.text = "Guitar --> Piano"
btn1.text = " " + self.text3
s.clear_widgets()
LoadLabels(1)
else:
self.whichway = 0
btnWhich.text = "Piano --> Guitar"
btn1.text = " " + self.text1
s.clear_widgets()
LoadLabels(0)