Forum Archive

Why do I get NameError when using main()

Bjucha

Can anybody help me with a noob question:
I'm trying to learn using main function and class function. The code works fine until I add a function for a button. Now I get a NameError saying that the code inside is not defined

Here is a simple example for the code:

import ui



def Tapped(sender):
    main_view.add_subview(L1)



def main():
    main_view = ui.View(name = 'test')
    main_view.background_color = 'white'
    main_view.present()

    L1 = ui.Label(text = 'test')
    L1.frame = (50,50,50,50)


    B1 = ui.Button(title = 'test')
    B1.frame = (50,100,50,50)
    main_view.add_subview(B1)
    B1.action = Tapped




if __name__ == '__main__':

    main()

ccc

view_main and L1 are defined inside the function main() so they are not visible at a global scope and therefor they are not visible inside of Tapped().

# Solution 1 =====
import ui

main_view = ui.View(name='test', bg_color='white')


def Tapped(sender):
    main_view.add_subview(ui.Label(text='label', frame=(50, 50, 50, 50)))


def main():
    B1 = ui.Button(title='button', frame=(50, 100, 50, 50), action=Tapped)
    main_view.add_subview(B1)
    main_view.present()


if __name__ == '__main__':
    main()

# Solution 2 =====
import ui


def Tapped(sender):
    sender.superview.add_subview(ui.Label(text='label', frame=(50, 50, 50, 50)))


def main():
    main_view = ui.View(name='test', bg_color='white')
    B1 = ui.Button(title='button', frame=(50, 100, 50, 50), action=Tapped)
    main_view.add_subview(B1)
    main_view.present()


if __name__ == '__main__':
    main()
Bjucha

@ccc thank you as always you are very helpful. Understood the problem but not the solution. This is great!

Phuket2

As usual, I am always late to the party :) but below i just made changes to get your code working (not the best way). I was just going to say as @ccc pointed out, you have a scope error the way you are accessing your variables.
Eg. If you look at your Tapped function. You are trying to use the main_view variable. But in that function you only have access to the sender variable. The sender is the object that sent the event, in this case your ui.Button. All ui objects have a superview property. So in the case when you say sender.superview, you are getting the view that your ui.button resides in. So sender.superview is basically main_view.

Sorry, i dont mean to confuse you. But its worth to take a breath and understand why @ccc solutions work.

import ui

def Tapped(sender):
    L1 = ui.Label(text = 'test Label')
    L1.frame = (50,50,100,50)
    sender.superview.add_subview(L1)

def main():
    main_view = ui.View(name = 'test')
    main_view.background_color = 'white'
    main_view.present()

    #L1 = ui.Label(text = 'test')
    #L1.frame = (50,50,50,50)

    B1 = ui.Button(title = 'test')
    B1.frame = (50,100,50,50)
    main_view.add_subview(B1)
    B1.action = Tapped

if __name__ == '__main__':  
    main()
Bjucha

@Phuket2 hehe thanks anyway. Really glad fir all the help