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.
Graphing/UI question
-
I want to write a simple user interface script. Part of the ui will consist of 4 buttons. When a button is clicked, depending on the user's choice, the script will draw one of the following shapes: oval, arc, rectangle, or polygon. In the ui Add View" it seems like the only view that can be drawn on is the Image View.
1. Is this the correct or best way to go about this? a. If so, can the ImageDraw module be used together with the ui module? 2. If it is not the correct or best way, which way would you recommend.
-
An ImageView is one option.
Another option is a custom View (read the doc section about custom views) where you implement a draw() method, and use the ui drawing methods (ui.Path, etc).
-
SceneView is another option.
-
Here is a script which illustrates all these three views , image view, custom view, and scene view. In addition it also includes a button as another option (images are stored as background images of the buttons). Buttons are used for selecting a shape. In addition you can adjust fill color, stroke color, position, size etc. using sliders.
https://gist.github.com/balachandrana/255a25f19fffe8ec4f384c1bcdad9792The pyui information is encoded in the script. If you want to get the pyui file,
use the following decoding script. Please note that it should be stored as editor action script.https://gist.github.com/balachandrana/d7dcaf3aadef5dbe633768d477898eb9
-
@dat2357 , I did a example below. It's not 100%. I could not remember all the details. I am using a ui.TableView here. Also just drawing the objects off the draw method, they could have been easily rendered into a ui.Image if required. Also, using layout. But to be orientation friendly you need to this unless you use the flex built into Pythonista.
Anyway, just another way. iOS views tend to be like this.
import ui _list = ['Rect', 'Oval', 'Rounded_Rect', 'Polygon'] class MyClass(ui.View): def __init__(self, *args, **kwargs): ui.View.__init__(self, *args, **kwargs) self.tv = None #self.tv_r = ui.Rect(*self.bounds) #self.tv_r.width *= .25 self.make_view() self.tv.selected_row = 0 def make_view(self): tv = ui.TableView() tv.data_source = ui.ListDataSource(_list) self.add_subview(tv) self.tv = tv tv.delegate = self def layout(self): r = ui.Rect(*self.frame) r.width *= .33 self.tv.frame = r def draw(self): # the shape drawing area r = ui.Rect(*self.frame) ui.set_color('teal') r.width -= self.tv.width r.x = self.tv.bounds.max_x r.y = self.tv.bounds.y s = ui.Path.rect(*r) s.fill() ui.set_color('blue') s.stroke() # square the rectangle to draw the shape into r1 = ui.Rect(r.x, r.y, r.width, r.width).inset(5, 5) r1.center(r.center()) ui.set_color('white') # draw the shape based on the list selection txt_type = self.get_selected_row_data() if txt_type is 'Rect': s1 = ui.Path.rect(*r1) elif txt_type is 'Oval': s1 = ui.Path.oval(*r1) elif txt_type is 'Rounded_Rect': s1 = ui.Path.rounded_rect(r1[0], r1[1], r1[2], r1[3], 3) #s1 = ui.Path.rounded_rect(*r1, 3) elif txt_type is 'Polygon': s1 = ui.Path.rect(*r1) print('ok, thats for you to implement...') s1.fill() def get_selected_row_data(self): return self.tv.data_source.items[self.tv.selected_row[1]] def tableview_did_select(self, tableview, section, row): self.set_needs_display() if __name__ == '__main__': w = 600 h = 800 f = (0, 0, w, h) mc = MyClass(frame = f, bg_color = 'white', name = 'Shapes Demo') mc.present('sheet')
Edited: I commented out 2 lines in the init
-
@abcabc I am fairly new to this. I copied the script and I think I even had success extracting pyui, but I carry on getting an error with the script and I am uncertain how to fix it. On line 230, 'Myscene' object has no attribute 'sprite'. Any ideas how to fix it?
-
Please try the updated code. I have tested with app store version and it should work now. Please let me know if there are problems.
The earlier version worked with Pythonista (beta) and did not work with app store version. The "anchor_point" parameter in sprite initialization (line 226 in new code) does not seem to work in app store version. I am now setting it separately.
-
@abcabc thanks, I can see it does work. I'm on an iPhone 5. So I am only able to see top left corner of the screen. So without tweaking where everything is and resizing it I can't change options. But I am up for the challenge. Thanks for the assistance.