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.


    Ui.button / inheritance issue

    Pythonista
    2
    4
    2990
    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.
    • m_doyle04
      m_doyle04 last edited by

      Hi Guys,

      I'm new to Python/Pythonista and reading through different material but I'm struggling to find examples of how to get a ui.button to change attributes of a different class object.

      I have a basic example of a widget that has an Image View that should present/change a different image when the ui.button is pressed.

      The error I get is "ui.button object has no attribute "view"". Obviously it's "ImView" that has the attribute "view" but I just can't seem to work out how to access the "ImView" object via the button action.

      I'm sure there may be numerous issues with my beginner code but any general help would be greatly appreciated.

      import appex, ui
      import random
      
      Image_One = ui.Image.named('Image_One.PNG')
      Image_Two = ui.Image.named('Image_Two.JPG')
      PIC_LIST = [Image_One,Image_Two]
      
      class MyView(ui.View):
      	def __init__(self):
      		self.hue = 0
      		self.background_color = 'white'
      		self.add_subview(ImView().view)
      		self.add_subview(MyHandler().sender)
      		
      class ImView(object):
      	def __init__(self):
      		self.view = ui.ImageView()
      		self.view.frame = (150,0,170,110)
      		self.view.background_color = 'white'
      		self.view.border_color = 'black'
      		self.view.border_width = 1
      		self.view.image = None
      	
      	def add_pic(self):
      		self.view.image =  PIC_LIST[random.randint(0,1)]
      		
      class MyHandler(object):
      	def __init__(self):
      		self.sender = ui.Button(frame=(0,0,150,110), title='Hit Me', action=ImView.add_pic)
      		
      appex.set_widget_view(MyView())```
      Phuket2 1 Reply Last reply Reply Quote 0
      • Phuket2
        Phuket2 @m_doyle04 last edited by

        @m_doyle04 , hi. Look I started to modify your code, but i thought It better I just write my way, well mostly.

        I rarely use appex, but I think it's not that different from what I remember. But my personal feeling is it's better to write without appex in mind until you get a little more used to 'Pythonista. Just easier to debug etc.

        Not saying my example below is the most correct, I tried to stay close to your code.

        Anyway, I hope it helps.

        import ui
        #import random 
        from random import choice
        
        Image_One = ui.Image.named('iob:alert_256')
        Image_Two = ui.Image.named('iob:alert_circled_256')
        PIC_LIST = [Image_One,Image_Two]
        
        class MyClass(ui.View):
        	def __init__(self, *args, **kwargs):
        		super().__init__(*args, **kwargs)
        		self.img_view = None
        		self.btn = None
        		
        		self.make_view()
        		
        	def make_view(self):
        		# create the ui.Image
        		self.img_view = ui.ImageView(frame = (150,0,170,110),
        									background_color = 'white',
        									border_color = 'black',
        									border_width = 1)
        		# create the ui.Button
        		self.btn = ui.Button(frame=(0,0,150,110),
        							title='Hit Me',
        							action=self.btn_hit)
        		
        		# add objects to the view
        		self.add_subview(self.img_view)
        		self.add_subview(self.btn)
        		
        	def btn_hit(self, sender):
        		#self.img_view.image =  PIC_LIST[random.randint(0,1)]
        		self.img_view.image = choice(PIC_LIST)	
        		
        if __name__ == '__main__':
        	f = (0, 0, 400, 600)
        	v = MyClass(frame = f, background_color = 'white')
        	v.present(style='sheet')
        
        1 Reply Last reply Reply Quote 0
        • m_doyle04
          m_doyle04 last edited by

          @Phuket2 You're a legend. Thank you very much! I've still got a long way to go with my learning but it's great to get help when you hit a stumbling block.

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

            @m_doyle04 , no problems. I am sure you will be teaching me before too long.

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