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 action parameters
-
-
@resserone13 said:
I also have Trouble importing text from another file. I tried to set it up like the brick breaker levels and colors example but it’s not working.
Where is your code for this?
-
-
@resserone13 I understand but it is not in your GitHub, for testing your code 😀
-
@resserone13 did you try my code to open a web view?
-
@resserone13 said:
line = ui.Path.rounded_rect(x, y, screen_size.w * 0.8, 0, 50)
Your rectangle has a width of 0, thus not displayed...
-
@resserone13 and path can be drawn in the draw method of the ui.View, but, it seems, only if you sub-class it.
class View_with_Path(ui.View): def draw(self): for x, y in (screen_size.w/2, screen_size.h * 0.8), (screen_size.w/2, screen_size.h * 0.4), (screen_size.w/2, screen_size.h * 0.1): path = ui.Path.rounded_rect(x, y, screen_size.w * 0.8, 10, 50) path.line_width=3 ui.set_color('yellow') # only to see where the path is drawn path.fill() def page_info(text, image, dwell_time, desc): v = View_with_Path() #v = ui.View() v.frame = (0,0,screen_size.w,screen_size.h) v.background_color=bg_color title= make_title(text) #for x, y in (screen_size.w/2, screen_size.h * 0.8), (screen_size.w/2, screen_size.h * 0.4), (screen_size.w/2, screen_size.h * 0.1): #path = ui.Path.rounded_rect(x, y, screen_size.w * 0.8, 0, 50) #line.line_width=3 #ui.set_color('yellow') #path.fill() #display.add_subview(line) . . .
-
@cvp said:
@resserone13 did you try my code to open a web view?
Yes I got the web view working. Thanks. -
@resserone13 said:
Yes I got the web view working
If you prefer to open really Safari:
if tv_txt.startswith('http'): tv = ui.Button() tv.title = tv_txt def open_url(sender): import webbrowser webbrowser.open('safari-'+sender.title)
-
@resserone13, things are moving so fast here that I am probably hopelessly late, but I can think of two options if you want to have clickable links in the middle of a body of text.
First is to use a webview, which works but has the downside that it wants to open the link in place. You can get around this by using a delegate to capture the attempt and open the link in Safari instead.
Second is to use objc and attributed text with a textview or label. This is not difficult but can be made even easier with an addition to the RichLabel that is part of the ui3 package, let me know if that seems relevant to you.
-
@mikael I just have my iPhone 11 with Pythonista. I don’t know if I have ui3. I’ve had trouble using stash to import and pip install modules. The web view is working OK for me. I just need to add some navigational buttons at the top. But it does look nice. I will probably have questions on objc In the near future. Once I figure out UI I will dive into objective c.
-
@cvp. Thanks for all your input and advice. I am reviewing everything and re-writing the script. Hopefully everything will be functions. I am going to try classes as well but I might have mainly functions. I’m going to work on it and I will keep you updated. Thanks for all your help it’s really allowing me to get a lot done and learn a lot. Hopefully when I keep showing you these updates you’ll see that I’m learning from what you’re showing me.
-
@resserone13 said:
I just need to add some navigational buttons at the top.
import ui class MyWebView(ui.View): def __init__(self, url): self.width, self.height = ui.get_window_size() self.wv = ui.WebView(frame=self.bounds) self.wv.load_url(url) self.add_subview(self.wv) bi_back = ui.ButtonItem(image=ui.Image.named('iob:ios7_arrow_back_32')) bi_forward = ui.ButtonItem(image=ui.Image.named('iob:ios7_arrow_forward_32')) bi_back.action = self.go_back bi_forward.action = self.go_forward self.right_button_items = [bi_forward, bi_back] self.present('fullscreen') def go_back(self, sender): self.wv.go_back() def go_forward(self, sender): self.wv.go_forward() wv = MyWebView('https://omz-software.com/pythonista/docs/')
-
I have a for loop that loops over btn names and creates a button for each name. How do I make it loop over a list of btns and a list of actions so that the each button gets a name and an action?
I’m using enumerate to help space the btns out. I tired switching to zip but then I loose the i variable for the enumerate function?def add_btn(self, btn_names, actions_list): for i, name, a in enumerate((btn_names)): btn= ui.Button() btn.background_color=btn_clr btn.title=name btn.font=(btn_fnt, btn_txt_sz) btn.tint_color=txt_clr btn.border_width=2 btn.border_color=txt_clr btn.frame=(width * 0.15, height * (0.12 + i * 0.1), width * 0.7, height * 0.05) btn.corner_radius= btn_corner_radius btn.action='?????' self.add_subview(btn)
-
@resserone13 something like (not tested):
def add_btn(self, btn_names, actions_list): for i in range(len(btn_names)): btn = ui.Button() btn.name = btn_names[i] btn.action = actions_list[i]
-
range(len())
is almost always a sign to useenumerate()
instead.import ui def show_sender(sender): print(sender.title) class MyView(ui.View): def __init__(self): self.add_buttons("abc", (show_sender, show_sender, show_sender)) def add_buttons(self, titles, actions): width = self.width height = self.height for i, (title, action) in enumerate(zip(titles, actions)): button = ui.Button( title=title, action=action, background_color="pink", border_color="dark green", border_width=2, corner_radius=5, font=("system", 12), tint_color="grey", ) button.frame = (width * 0.1, height * (0.1 + i), width * 0.8, height * 0.8) self.add_subview(button) MyView().present()
-
@resserone13, maybe clearer and less error-prone is to pair the titles and actions when calling the method:
class MyView(ui.View): def __init__(self): self.add_buttons( ("A", action_a), ("B", action_b), ) def add_buttons(self, *args): width = self.width height = self.height for i, (title, action) in enumerate(args): button = ui.Button( title=title, action=action, ...
-
Thanks @cvp @ccc and @mikael for the help. I was able to get past the hang up and move forward on the app development. I ended up using @ccc suggestion because i was using code very similar to what I already had. I’m finishing up a few more things then I will post on GitHub.
def add_btn(self, btn_names, actions_list): for i, (n, a) in enumerate(zip(btn_names, actions_list)): btn= ui.Button() btn.background_color=btn_clr btn.title=n btn.font=(btn_fnt, btn_txt_sz) btn.tint_color=txt_clr btn.border_width=2 btn.border_color=txt_clr btn.frame=(width * 0.15, height * (0.12 + i * 0.1), width * 0.7, height * 0.05) btn.corner_radius= btn_corner_radius btn.action=a self.add_subview(btn)
-