Assigning state['volume'] = mute



  • I want to assign state['volume'] to a variable that I have defined earlier as integer; can this work? below erro throws erros.

    Cheers, Gernot

    @touch(xy=(25,220), size=(50,40), align= 'center')
    def on_touch(xy, action):
    if action == 'down':
    if state['volume'] == 0:
    state['volume'] = mute
    screen.text('Volume mute %i' % mute,xy=(200,100), font_size= 16 )
    #time.sleep(5)
    state['volume'] = 0
    set_volume(state['volume'])


  • Core team

    Hi Gernot, could you post the whole program? I don't think there's enough code here to tell!



  • @joerick here is the complete code
    I do something wrong in the function # mute or umute speaker; this can change the state of the speaker from loud to mute and vice versa.
    the idea is to assigne the current speaker volume to the variable mute when the touch event occurs; this is followed by setting the speaker volume to 0; when pressing the mute/umute button again the value of mute should be re-assigned to state['number'] = mute
    but it
    will thorugh the following error
    local variable 'mute' referenced before assignment

    by the way - is it possible to change the font size in the Tide editor?

    Button presses can be received by using the @button.press

    #decorator. There are four buttons on Tingbot:
    #
    #@left_button.press
    #@midleft_button.press
    #@midright_button.press
    #@right_button.press
    
    import tingbot
    from tingbot import *
    
    state = {'number': 0,
             'volume': 0
             }
    #state = {'volume': 0}
    global mute
    
    @left_button.press
    def press():
        state['number'] -= 1
    
    @right_button.press
    def press():
        state['number'] += 1
    
    
    @touch(xy=(20,30), size=(50,50), align= 'center')
    def on_touch(xy, action):
        if action == 'down':
            state['number'] += 5
            if state['number'] >= 100:
                state['number'] = 100
    
    @touch(xy=(20,80), size=(50,50), align= 'center')
    def on_touch(xy, action):
        if action == 'down':
            state['number'] -= 5
            if state['number'] >= 100:
                state['number'] = 100
    
    
    #volume
    
    @touch(xy=(25,135), size=(50,50), align= 'center')
    def on_touch(xy, action):
        if action == 'down':
            screen.brightness = 100
            state['number'] += 5
            if state['number'] >= 100:
                state['number'] = 100
            
    
    @touch(xy=(25,175), size=(50,60), align= 'center')
    def on_touch(xy, action):
        if action == 'down':
            state['number'] -= 5
            screen.brightness = 1
            if state['number'] >= 100:
                state['number'] = 100
            
    # mute or umute speaker
    @touch(xy=(25,220), size=(50,40), align= 'center')
    def on_touch(xy, action):
        if action == 'down':
            if state['number'] == 0: # speaker has been previously muted level before muting has been assigned to variable muute
                state['number'] = mute
            if state['number'] > 0: # speaker level is above 0
                mute = state['number'] #level is assigned to mute
                state['number'] = 0 #soundlevel is set to 0 = mute
    
    
    
    @every(seconds=1.0/30)
    def loop():
        screen.fill(color='black')
        screen.text(state['number'], color='green')
        #Channel settings
        screen.rectangle(xy=(20,20), size=(50,250), color=(255,255,255), align='right')
        screen.image('Channel.png', xy=(25,60), max_width=(60), max_height=(80))
        #Volume settings
        screen.rectangle(xy=(0,190), size=(30,110), color=(255,255,255), align='center')
        screen.text('Volume %i' % state['number'],xy=(200,200), color= 'white', font_size= 12 )
        screen.text(state['number'],xy=(15,172), font_size= 12, align= 'center' )
        screen.image('Vol_plus.png', xy=(25,135), max_width=(50), max_height=(50))
        screen.image('Vol_minus.png', xy=(25,175), max_width=(50), max_height=(50))
        screen.image('Vol_mute.png', xy=(25,220), max_width=(50), max_height=(50))
        
    tingbot.run()
    


  • @geab @joerick has probably something to do how i assign the global variable link text

    will try later

    can you still comment ont he font size? and how can touch function read that there is a constant touch


  • Core team

    Yep, if you're using the mute global variable you need to put global mute inside the function where it's set, so in your case it should be in the on_touch function.

    This annoying error is why we recommend that the state dict is used instead for app memory. So I'd suggest setting state['mute'] instead of mute.


  • Core team

    Re: font size, if you're comfortable with the developer console in Tide (Window>Toggle Developer Tools), you can increase it by pasting this command into the console there:

    document.getElementById('editor').style.fontSize = '16px'

    but yes I definitely need to add an option for that!


Log in to reply

Looks like your connection to Assigning state['volume'] = mute was lost, please wait while we try to reconnect.