omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Why do I get NameError when using main()

    Pythonista
    class error
    3
    5
    3399
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Bjucha
      Bjucha last edited by

      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()
      	
      
      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by 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 1 Reply Last reply Reply Quote 0
        • Bjucha
          Bjucha @ccc last edited by

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

          1 Reply Last reply Reply Quote 1
          • Phuket2
            Phuket2 last edited by

            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 1 Reply Last reply Reply Quote 0
            • Bjucha
              Bjucha @Phuket2 last edited by

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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors