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.
Simple UI tutorial?
-
Hi @Bjucha try to look at the first script in this post: https://forum.omz-software.com/topic/3964/unipage-as-a-bridge-between-kivy-and-pythonista. It defines simple ways to put basic ui elements at any position on the screen programmatically.
-
Thx for all the info, will look at it all
Im not very good with programming but it's fun to learn and imporve my basic skills -
@ramvee hello I have a question for you
This part of your code:w,h = ui.get_screen_size() h = h - 64 view = ui.View(name = 'Flex', bg_color = 'lightyellow', frame = (0,0,w,h)) #view.flex = 'WH' # label height and button width bh = bw = 80 # margin mg = 10
Is about the size of the ui. W,h is width and height ?
But why is h = -64?
is bh and bw borderheight and borderwidth?
And finally mg what is that? -
@Bjucha Sorry, you can remove that line.
i used to think that screensize measures the total screen including the title bar.
So i used to subtract 64 from height to compensate for title bar height.bw and bh are just width and height of button, or label in this case.
mg is margin between the top, bottom and side edges from screen, from where the labels are..
You can fool around with values to see what happens. 😊
-
@ramvee Hello Just wanna say thanks for your template.
I have now been able to create labels and buttons thanks to you -
-
I would like to print to a view rather than the console so that I can control fonts size, etc. Could someone give me a snippet of code does this? I just want a full screen view that displays the text and a way to close the view.
-
-
@ihf, I wrote the below in a few mins (I am still a novice). My point is sometimes its worth doing yourself. Even if you have to ask some questions along the way. Of course its good for learning, and because you write yourself its a lot easier for you to get your exact solution. I still copy code though, but I often have a go at it myself also.
Anyway, from what you describe, I think the below does what you want or at least is a good starting point.
import ui class MyConsole(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.tv = None self.make_view() def make_view(self): tv = ui.TextView(frame=self.bounds, flex='wh', font=('Arial Rounded MT Bold', 24), editable=False, ) self.tv = tv self.add_subview(self.tv) def write_line(self, txt): self.tv.text += "{}\n".format(txt) if __name__ == '__main__': f = (0, 0, ui.get_screen_size()[0], ui.get_screen_size()[1]) v = MyConsole(frame=f, name='My Full Sceen Console') v.present(style='', animated=False) for i in range(100): v.write_line('line-{}'.format(i))
-
@ccc very nice example.
What and why flex=name ? Can't find the meaning of it -
@Phuket2 First of all, thank you very much for writing this script...I think it is precisely what I need. My only problem is I am using PrettyTable which is apparently formatting for a fixed width font so I need to play with the ui view options to get things to line up. Second, while I agree completely that doing this myself is the best way to learn python I am struck by the fact that it will take me awhile to even understand your code much less write it from scratch on my own. So, thanks again for giving me working code and something to study.
-
@PeterG http://omz-software.com/pythonista/docs/ios/ui.html#view Under View Attributes see View.flex... Hint, 'wh' is not a name.
-
@ihf, for the font you could just use Menlo or another mono spaced font (Menlo, is what is used in the console by default I think) . Anyway, I was not critising you at all. I just has similar problems when I started. There is a smalll bump to get over using the ui module, but it does fall into place pretty quickly. I did not try writing from prettytable into the TextField , However I can't see why it should not work. The only think that comes to mind could be you getting extra line feed chars in the text. If that were to happen you could do a replace on those chars either in the write_line method, or before you pass the string to write_line. Anyway, let me know if you have a problem, If will help if I can
tv = ui.TextView(frame=self.bounds, flex='wh', font=('Menlo', 24), editable=False, ) ```python
-
@ihf, I just updated the example to have a copy to clipboard and clear methods and menu items. Not sure they are useful, but good to see how to add menu Items anyway, if you haven't played with them yet.
My example is not as compact looking as it could be, but I try and following the PEP8 style now. Eg, only one import per line, 2 lines after the imports etc.import ui import clipboard import console class MyConsole(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.tv = None self.make_view() def make_view(self): tv = ui.TextView(frame=self.bounds, flex='wh', font=('Menlo', 24), editable=False, ) self.tv = tv self.add_subview(self.tv) ''' Create 2 ui.ButtonItem to insert into the menubar. ButtonItem's are one of those pesky ui Items that are not subclassed from ui.View Its not a big deal, just good to know they differ from ui.Button. You can see in the docs. Below, I am adding the 2 menu buttons to the right side as it seems to make sense. There is a method left_button_items also. ''' mbtn_clear = ui.ButtonItem(title='Clear', action=self.clear_console, tint_color='red') mbtn_copy = ui.ButtonItem(title='Copy', action=self.copy_console) self.right_button_items = (mbtn_clear, mbtn_copy) def write_line(self, txt): self.tv.text += "{}\n".format(txt) def clear_console(self, sender=None): ''' sender is set to None so sender is not required to call this method. The action from the menu ButtonItem needs to see it there though. But it means you can call this method on the object withouut having to pass a sender. i.e if youwanted to clear the console from your code rather than the menu button action. ie. obj.clear_console() will work. Same goes for the copy_console method below. ''' self.tv.text = "" def copy_console(self, sender=None): clipboard.set(self.tv.text) console.hud_alert('{} characters copied.'.format(len(self.tv.text))) if __name__ == '__main__': f = (0, 0, ui.get_screen_size()[0], ui.get_screen_size()[1]) v = MyConsole(frame=f, name='My Full Sceen Console') v.present(style='', animated=False) for i in range(100): v.write_line('line-{}'.format(i))