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.


    Pyui button image options

    Pythonista
    6
    14
    10146
    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.
    • Sabarblatoe
      Sabarblatoe last edited by

      I’m not exactly sure what you mean, however, I will list a few of the many things I have tried in the attempt to get this to work.
      Adding {image_name : image.png} to the buttons Custom Attributes with & without “”.

      Changing the file format of the .pyui file to .json & editing the image_name : to the custom image.png.

      Editing the py file & adding button.background_image = ui.Image.named('filePath/image.PNG')\

      With just these three things I have tried every variation I can think of along with MAAAAAAAANY others. I found that I was able to get the image to show up as a button by editing a .py file manually but my goal is to do this using the .pyui file & the UI Design tool. I would like to be able to create the layout of my ui with the design to because positioning every thing manually is just excruciating.

      Anyway...Thanks in advance for any help that can be given.

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @Sabarblatoe last edited by cvp

        @Sabarblatoe example of custom attributes in UI designer:

        {'background_image':ui.Image.named('IMG_6066.PNG')}
        

        Or

        {'image':ui.Image.named('IMG_6066.PNG'). with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)}
        

        But you only will see the effect when you run your script, not at design time

        1 Reply Last reply Reply Quote 0
        • Sabarblatoe
          Sabarblatoe last edited by

          Hey thanks for taking the time to reply. Unfortunately both examples were included in the plethora of things I have tried and I figured there was a good chance any custom changes may not show up in the designer . I literally spent an entire day googling & reading and tried everything I could find along with slight variations and combinations.

          1 Reply Last reply Reply Quote 0
          • Sabarblatoe
            Sabarblatoe last edited by

            Ok...so I decided to delete & reinstall the app & boom. It works now so something must have just been missing. You do have to add the last part included in the second example but it works perfectly. Thanks again for your help.

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @Sabarblatoe last edited by cvp

              @Sabarblatoe Even so, I don't see the image in the UI designer, only when I run the script.
              I did not reinstall the app.

              1 Reply Last reply Reply Quote 0
              • Sabarblatoe
                Sabarblatoe last edited by

                Yes, you are correct. It does not show up in the designer & only when you run the program. Mine, however, was not working as expected. This is likely due to something not being installed correctly during my initial download of the app. Perhaps a crappy internet connection or my phone just being an ass, we will never know. After reinstalling the app the problem was fixed & the app was working as expected. You’re app, unlike mine, was working properly in the first place & thus would not require a reinstall. It was your initial post that prompted me to think the problem may be on my end seeing as how, during my research of this topic, I came across the same method you discribed multiple times. So again, thanks.

                Phuket2 1 Reply Last reply Reply Quote 0
                • Phuket2
                  Phuket2 @Sabarblatoe last edited by

                  @Sabarblatoe, not sure you are interested or not. Below is some code that reads a pyui/UIFile into a class and class behaves as if it is a Custom ui View. I think its handy for many reasons and it can make life a little easier (Maybe not in all cases). But in the demo below you can see you can code directly against the view rather than the pyui file being a subview of another view. This has been posted on the forum many times before.
                  Anyway, maybe its interesting to you.

                  @omz do you have any plans to add this functionality directly into API? I know I have asked before. But I still believe this would be a great addition to the API. It would also then be documented, I think a lot of ppl would use this approach to loading UIFiles if it was documented and built in.

                  import ui
                  
                  
                  class PYUILoader(ui.View):
                      # this acts as a normal Custom ui.View class
                      # the root view of the class is the pyui file read in
                      # from @JonB
                      def WrapInstance(obj):
                          class Wrapper(obj.__class__):
                              def __new__(cls):
                                  return obj
                          return Wrapper
                  
                      def __init__(self, pyui_fn, *args, **kwargs):
                          bindings = globals().copy()
                          bindings[self.__class__.__name__] = self.WrapInstance()
                  
                          ui.load_view(pyui_fn, bindings)
                  
                          # call after so our kwargs modify attrs
                          super().__init__(*args, **kwargs)
                  
                  
                  class MyClass(PYUILoader):
                      '''
                      This class acts the same as a ui Custom View would even though its
                      been loaded in from a pyui file.
                  
                      *** NOTE ***
                      You have to do 2 things for this to work correctly.
                  
                      1. pass the file name of your .pyui file when creating the class
                      2. in the ui Designer, you need to set the 'Custom View Class' field in the
                         Inspector to the name of this class's name. In this case 'MyClass'
                  
                         - Finding the 'Custom View Class' in the ui Designer
                         Make sure you are at the root level, i.e not in a subview
                         In the ui Designer, tap anywhere to make sure nothing is selected,
                         Then you will see the 'Size Presets' in the Inspector. Directly
                         below you will see the 'Custom View Class' field.
                      '''
                      def __init__(self, pyui_fn, *args, **kwargs):
                          super().__init__(pyui_fn, *args, **kwargs)
                          self.update_interval = 1
                  
                      def did_load(self):
                          print('view loaded')
                  
                      def layout(self):
                          print('in layout')
                  
                      def update(self):
                          print('update fired')
                  
                  if __name__ == '__main__':
                      f = (0, 0, 480, 600)
                      v = MyClass('MyUIFile.pyui', frame=f)
                      v.present(style='sheet')
                  
                  1 Reply Last reply Reply Quote 1
                  • Phuket2
                    Phuket2 last edited by

                    In the example above, if you say want to set the action attr of a ui.Button in the ui Designer, you cant refer to self to access a method in your class. In the case of the example above, the class is named MyClass, so if you had a method in MyClass called hit, in the ui Designer in the action property of the btn you would enter MyClass.hit, not self.hit.
                    If your action is a func in the same source file you would do as normal, just enter the name of the func.

                    I cant remember if it was always like this. It's possible that @omz made some changes to prevent injection attacks. I thought before that self.method worked. But maybe I am dreaming.

                    Regardless many things can be done with pyui files. It's worth searching the forum.

                    Sabarblatoe 1 Reply Last reply Reply Quote 0
                    • Sabarblatoe
                      Sabarblatoe @Phuket2 last edited by

                      @Phuket2
                      Hey thanks. I will definitely check that out. I really appreciate it. If it works the way I think it does then yes, it could be VERY helpfull. Thanks again.

                      Phuket2 1 Reply Last reply Reply Quote 0
                      • Phuket2
                        Phuket2 @Sabarblatoe last edited by

                        @Sabarblatoe , no problems. There seems to be a few little quirks with this way that I dont remember from before. But I think its still really nice. But I think it will work as you expect. Once you have returned from the super() call, its the same as if you have created a Custom ui class by hand.

                        1 Reply Last reply Reply Quote 0
                        • scarlett
                          scarlett last edited by

                          I am using Pythonista on my iPhone 11
                          And I am writing this
                          super().init( *args, **kwargs)
                          However it still tells me super is not defined

                          1 Reply Last reply Reply Quote 0
                          • ccc
                            ccc last edited by ccc

                            To write code in a post to this forum, please follow https://guides.github.com/features/mastering-markdown

                            So,
                            ```python
                            My code goes here...
                            ```

                            If not then __init__ becomes init.

                            If you are not inside a class definition then super() is not defined.

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