Forum Archive

How to use a custom class for a whole UI

GenericUser

Hi,
My goal is to make a subclass of Ui.View and use it as a class for a whole UI like in a pyui file made in UI builder. How does that work? I don't get buttons wired up to actions in the class; it only works for methods outside a class. Any help?

I came to that issue since I defined a global SQLite instance and cursor and tried to use it in defs wired up to buttons, all without any class, but then it fails to execute sqllite since different threads being used.

Greetings!

ccc

In your .pyui file, you must make sure to set the Custom View Class to the name of your class and then use code like http://omz-forums.appspot.com/pythonista/post/5588266841538560#post-5278865450598400

GenericUser

Thanks, strange syntax.

I somehow expected something like setting the custom class of the pyui and then all methods set as action in the Ui builder would have referred to corresponding methods of the set custom subclass of View used for it. Instead, one needs to wire up all actions by code. Maybe that is something for an upcoming update?

But my initial problem still exists: how can one deal with sqllite3 in an object based manner? As soon as executions are done by a method of a class whereas pointer and connection are instance variables, I get an error saying that they are running in different threads?

ccc

You might try putting the @ui.in_background decorator on the line just before your functions or methods that use sqllite3:

@ui.in_background
def my_sqllite3_function():
    [ ... ]

Can you post a github repo with some of your code in it so we can debug as a team? IETDPCTEP.

omz

You don't have to wire up all the actions in code if you follow this pattern:

class MyViewController (object):
  def load_view(self):
    self.view = ui.load_view()

  def some_action(self, sender):
    print 'action 1'

  def some_other_action(self, sender):
    print 'action 2'

controller = MyViewController()
controller.view.present()

You'd then set an action in the pyui file as self.some_action (not just some_action), and it'll automagically be connected to the object that's loading the view (an instance of MyViewController in this case).

Setting a custom view class in the pyui file isn't really intended for connecting actions, it's a way to do things like custom drawing and touch handling.

ccc

omz, In the controller scenario above, what would the best way to catch ui.View.did_load(self) signal which should come after ui.load_view() completes?

omz

@ccc You'd have to use a custom view class for this, though if that's the only thing you need a custom view for, you could consider just putting the same logic into your own load_view method. There's no reason you couldn't do both btw, i.e. use a controller to load the view, as shown above, and still have a custom class for the view itself.