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
-
If I have a function that is a button action and the button action takes another function that requires parameters. How do I feed the parameters to the function inside of the button action?
def info_page(sender): page_info(text, image, dwell_time, desc)
So when I do ....
Btn.action = info_page
How do I feed the parameters to the page_info inside of info_page?
-
@resserone13 if I correctly understand, what happens rarely these last times:
import ui def page_info(text, image, dwell_time, desc): print(text, image, dwell_time, desc) def info_page(sender): page_info(sender.prm_text, sender.prm_image, sender.prm_dwell_time, sender.prm_desc) Btn = ui.Button() Btn.background_color = 'white' Btn.prm_text = 'xxx' Btn.prm_image = ui.Image.named('emj:Airplane') Btn.prm_dwell_time = 23 Btn.prm_desc = 'abcde' Btn.title = 'tap me' Btn.action = info_page Btn.present('sheet')
-
@resserone13, or using standard library
functools.partial
, which is a really good thing to have in your toolbox for any Python coding.from functools import partial import ui def page_info(text, image, sender): print(text, image, sender.title) btn = ui.Button( background_color='white', title="Tap me", ) btn.action = partial(page_info, 'some text', 'some image') btn.present('sheet')
-
@mikael I knew partial but I didn't know we could use it as action. Marvelous language
-
@mikael and @cvp Thanks for the help as always. I ended up using the @cvp suggestion. I’m redoing the personal protective equipment app using the ui module instead of scene. Coming along well.
I hve an issue with displaying hyperlinks in a text view and getting the ui.path.rounde_rect to draw to the screen I a for loop.
import console import ui from ppeasy_text import text app_title = 'PPEasy' app_title_font = 'Didot' btn_font = 'Didot' text_font = '<System>' text_color = '1D3557' bg_color = 'CAF0F8' btn_border_color = '0077B6' btn_color = 'CAF0F8' btn_shadow=('gray', 0, 7, 2) btn_corner_radius = 10 screen_size = ui.get_window_size() links ='\n\nwww.cdv.gov \n\nwww.fda.gov \n\nwww.coronavirus.gov \n\nwww.covid19.healthdata.org' v = ui.View() v.background_color=bg_color def make_title(text): title = ui.Label() title.font= app_title_font, 30 title.text = text title.alignment= ui.ALIGN_CENTER title.frame=(screen_size.w * 0.10, screen_size.h * 0.05, screen_size.w * 0.75, screen_size.h * 0.05) return title def make_button(title, locale, text_param, image_param,dwell_time_param, desc_param, action=None): btn= ui.Button() btn.background_color='white' btn.title=title btn.font=(btn_font, 18) btn.background_color=bg_color btn.tint_color='black' btn.border_width=2 btn.border_color='black' btn.frame=(screen_size.w * 0.15, screen_size.h * locale, screen_size.w * 0.7, screen_size.h * 0.07) btn.corner_radius= btn_corner_radius btn.text_param = text_param btn.image_param = image_param btn.dwell_time_param = dwell_time_param btn.desc_param = desc_param btn.action = action # v.add_subview(btn_1) return btn def page_info(text, image, dwell_time, desc): v = ui.View() 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): line = ui.Path() line = ui.Path.rounded_rect(x, y, screen_size.w * 0.8, 0, 50) line.line_width=3 #display.add_subview(line) img = ui.ImageView() img.image = ui.Image.named(image) img.frame=(screen_size.w * 0.15, screen_size.h * 0.15, 300, 300) img.content_mode = ui.CONTENT_SCALE_ASPECT_FIT dwell = ui.Label() dwell.font= app_title_font, 25 dwell.text = dwell_time dwell.alignment= ui.ALIGN_CENTER dwell.frame=screen_size.w * 0.10, screen_size.h * 0.45, screen_size.w * 0.75, screen_size.h * 0.05 description = ui.TextView() description.editable=False description.scroll_enabled=True description.auto_content_inset=True description.background_color=bg_color description.alignment= ui.ALIGN_LEFT description.number_of_lines=0 description.line_break_mode=ui.LB_CHAR_WRAP description.font=(text_font, 17) description.frame=(screen_size.w * 0.12, screen_size.h * 0.60, screen_size.w * 0.75, screen_size.h * 0.60) description.text=desc v.add_subview(title) v.add_subview(img) v.add_subview(dwell) v.add_subview(description) v.present('sheet', hide_title_bar=True) def mask_page(sender): v = ui.View() v.background_color=bg_color title=make_title('mask') level_1_btn = make_button('Level 1', 0.12, 'Level 1', 'Level1Mask.PNG', '', level_1_desc, info_page) level_3_btn = make_button('Level 3', 0.22, 'Level 3', 'Level3Mask.PNG', '', level_3_desc, info_page) k_n95_btn = make_button('K-N95', 0.32, 'K-N95', 'Kn95Mask.PNG', '', k_n95_desc, info_page) n95_btn = make_button('N95', 0.42, 'N95', 'N95Mask.PNG', '', n95_desc, info_page) papr_btn = make_button('Papr', 0.52, 'Papr', 'PaprMask.PNG', '', papr_desc, info_page) v.add_subview(title) v.add_subview(level_1_btn) v.add_subview(level_3_btn) v.add_subview(k_n95_btn) v.add_subview(n95_btn) v.add_subview(papr_btn) v.present('sheet', hide_title_bar=True) def info_page(sender): page_info(sender.text_param, sender.image_param, sender.dwell_time_param, sender.desc_param) def gowns_page(sender): v = ui.View() v.background_color=bg_color title= make_title('Gowns') cloth_btn = make_button('Cloth', 0.12, 'Cloth', 'ClothGown.PNG', '', cloth_desc, info_page) plastic_btn = make_button('Plastic', 0.22, 'Plastic', 'PlasticGown.PNG', '', plastic_desc, info_page) v.add_subview(title) v.add_subview(cloth_btn) v.add_subview(plastic_btn) v.present('sheet', hide_title_bar=True) def wipes_page(sender): v = ui.View() v.background_color=bg_color v.present('sheet', hide_title_bar=True) def faq_page(sender): v = ui.View() v.background_color=bg_color title = ui.Label() title.font= text_font, 25 title.text = "FAQ" title.alignment= ui.ALIGN_CENTER title.frame=screen_size.w * 0.1, screen_size.h * 0.02, screen_size.w * 0.75, screen_size.h * 0.05 tv = ui.TextView() tv.editable=False tv.scroll_enabled=True tv.auto_content_inset=True tv.background_color=bg_color tv.alignment= ui.ALIGN_LEFT tv.number_of_lines= 0 tv.line_break_mode = ui.LB_CHAR_WRAP tv.font= text_font, 17 tv.frame= screen_size.w * 0.07, screen_size.h * 0.08, screen_size.w * 0.87, screen_size.h * 0.85 tv.text = "Q. What is the difference between a K-N96 and a N95? \n\nA. Both products are said to filter 95 percent of aerosol particulates. K-N95 respirators differ from N95 respirators because they meet the Chinese standard but are not regulated by U.S. agencies. \n\nQ. Do mask really work? \n\nA. Yes. It stops the transmission of saliva and mucus from person to person " v.add_subview(title) v.add_subview(tv) v.present('sheet', hide_title_bar=True) def dd_page(sender): v = ui.View() v.background_color=bg_color title = ui.Label() title.font= text_font, 25 title.text = "Dos and Don'ts" title.alignment= ui.ALIGN_CENTER title.frame=screen_size.w * 0.1, screen_size.h * 0.02, screen_size.w * 0.75, screen_size.h * 0.05 tv = ui.TextView() tv.editable=False tv.scroll_enabled=True tv.auto_content_inset=True tv.background_color=bg_color tv.alignment= ui.ALIGN_LEFT tv.number_of_lines= 0 tv.line_break_mode = ui.LB_CHAR_WRAP tv.font= text_font, 17 tv.frame= screen_size.w * 0.07, screen_size.h* 0.08, screen_size.w * 0.85, screen_size.h * 0.85 tv.text = "DO wear your cask correctly. Cover you mouth and nose. \nDON'T improperly wear your mask. Make sure you mouth and nose is covered. \n\nDO keep you mask clean. \nDON'T touch mask with your hands. \n\nDO change your mask regulary. \nDON'T wear a dirty mask' \n\nDO wear your mask while talking. \nDON'T remove your mask to talk. \n\nDO wear masks that fit well and trap your breath. \nDON'T ware masks with vents. They allow your breath to escape and spread to others. \n\nDo keep 6 feet apart when out in public even while wearing a mask. \nDON'T beark social distancing even if your wearing a mask. \n\nDO" v.add_subview(title) v.add_subview(tv) v.present(hide_title_bar=True) def covid_page(sender): v= ui.View() v.background_color=bg_color title = ui.Label() title.font= app_title_font, 25 title.text = 'Covid Information' title.alignment= ui.ALIGN_CENTER title.frame=screen_size.w * 0.1, screen_size.h * 0.02, screen_size.w * 0.75, screen_size.h * 0.05 tv = ui.TextView() tv.editable=False tv.scroll_enabled=True tv.background_color=bg_color tv.alignment= ui.ALIGN_LEFT tv.number_of_lines= 0 tv.line_break_mode = ui.LB_CHAR_WRAP tv.font= app_title_font, 17 tv.frame= screen_size.w * 0.07, screen_size.h * 0.08, screen_size.w * 0.87, screen_size.h * 0.85 tv.text = 'COVID-19 is caused by a coronavirus called SARS-CoV-2. Older adults and people who have severe underlying medical conditions like heart or lung disease or diabetes seem to be at higher risk for developing more serious complications from COVID-19 illness. \n\nSymptoms may appear 2-14 days after exposure to the virus. People with these symptoms may have COVID-19:\n\n•Fever or chills\n•Cough\n•Shortness of breath or difficulty breathing\n•Fatigue\n•Muscle or body aches\n•Headache\n•New loss of taste or smell\n•Sore throat\n•Congestion or runny nose\n•Nausea or vomiting\n•Diarrhea \n\nEmergency warning signs. If someone is showing any of these signs, seek emergency medical care immediately:\n\n•Trouble breathing\n•Persistent pain or pressure in the chest\n•New confusion\n•Inability to wake or stay awake\n•Bluish lips or face \n\nCOVID-19 most commonly spreads during close contact. People who are physically near (within 6 feet) a person with COVID-19 or have direct contact with that person are at greatest risk of infection. \n\nWhen people with COVID-19 cough, sneeze, sing, talk, or breathe they produce respiratory droplets. These droplets can range in size from larger droplets (some of which are visible) to smaller droplets. Small droplets can also form particles when they dry very quickly in the airstream. \n\nInfections occur mainly through exposure to respiratory droplets when a person is in close contact with someone who has COVID-19. Respiratory droplets cause infection when they are inhaled or deposited on mucous membranes, such as those that line the inside of the nose and mouth. \n\nAs the respiratory droplets travel further from the person with COVID-19, the concentration of these droplets decreases. Larger droplets fall out of the air due to gravity. Smaller droplets and particles spread apart in the air.With passing time, the amount of infectious virus in respiratory droplets also decreases.' v.add_subview(title) v.add_subview(tv) v.present(hide_title_bar=True) def links_page(sender): v = ui.View() v.background_color=bg_color v= ui.View() v.background_color=bg_color title = ui.Label() title.font= text_font, 25 title.text = "Links" title.alignment= ui.ALIGN_CENTER title.frame=screen_size.w * 0.1, screen_size.h * 0.02, screen_size.w * 0.75, screen_size.h * 0.05 tv = ui.TextView() tv.editable=False tv.scroll_enabled=True tv.auto_content_inset=True tv.background_color=bg_color tv.alignment= ui.ALIGN_CENTER tv.number_of_lines= 0 tv.line_break_mode = ui.LB_CHAR_WRAP tv.font= text_font, 22 tv.frame= screen_size.w * 0.07, screen_size.h * 0.08, screen_size.w * 0.87, screen_size.h * 0.85 tv.text ='https://www.who.int/' v.add_subview(title) v.add_subview(tv) v.present('sheet', hide_title_bar=True) title = make_title(app_title) mask_btn = make_button('Mask', 0.12, '', '', '', '', mask_page) shield_btn = make_button('Shield', 0.22, 'Face Shield', 'FaceShield.JPG', '',fs_desc, info_page) gowns_btn = make_button('Gowns', 0.32,'', '', '', '', gowns_page) wipes_btn = make_button('Wipes', 0.42, '', '', '', '', wipes_page) faq_btn = make_button('FAQ', 0.52, '','','','',faq_page) dd_btn = make_button('Dos/Donts', 0.62, '', '', '', '', dd_page) covid_btn = make_button('Covid Info', 0.72, '', '', '', '', covid_page) links_btn = make_button('Links', 0.82, '', '', '', '', links_page) v.add_subview(title) v.add_subview(mask_btn) v.add_subview(shield_btn) v.add_subview(gowns_btn) v.add_subview(wipes_btn) v.add_subview(faq_btn) v.add_subview(dd_btn) v.add_subview(covid_btn) v.add_subview(links_btn) v.present('full_screen', orientations='portrait', hide_title_bar=True)
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.
-
@resserone13 first of all, to avoid repetitive code, you could create your own class with, as parameters, varying infos, here l_txt and tv_txt
class my_View(ui.View): def __init__(self,l_txt,tv_txt): self.background_color=bg_color title = ui.Label() title.font= text_font, 25 title.text = l_txt title.alignment= ui.ALIGN_CENTER title.frame=screen_size.w * 0.1, screen_size.h * 0.02, screen_size.w * 0.75, screen_size.h * 0.05 tv = ui.TextView() tv.editable=False tv.scroll_enabled=True tv.auto_content_inset=True tv.background_color=bg_color tv.alignment= ui.ALIGN_CENTER tv.number_of_lines= 0 tv.line_break_mode = ui.LB_CHAR_WRAP tv.font= text_font, 22 tv.frame= screen_size.w * 0.07, screen_size.h * 0.08, screen_size.w * 0.87, screen_size.h * 0.85 tv.text = tv_txt self.add_subview(title) self.add_subview(tv) . . . def links_page(sender): v= my_View('Links', 'https://www.who.int/') v.present('sheet', hide_title_bar=True)
-
@cvp ok be been trying to do more of that. That why I have the page_info(). I need to make a few more classes to cut down on the code.
How can I make the text on the links page be able to receive touches and take the user to the websites.
-
@resserone13 I was busy to understand your loop/path in page_info. What is its goal?
-
@resserone13 said:
take the user to the websites
How do you want to "take the user to a website"?
Open Safari or have a web view in your ui?Easiest way is to have, instead of a textview, a button and it's action to open the url
-
@cvp page_info() is like any_scene() class I used when I made the app with scene. I modeled page_info() after any_scene(). Page_info() is a generic function to display the page. Kinda like your myview().
I’ve also created make_btn() and make_title() for making buttons and creating titles.
-
@resserone13 I would like them to be directed to the website in Safari.
-
@resserone13 written just before your answer about safari, this code opens a web view in your app.
If you open safari, you will leave your app. Sure of this?class my_View(ui.View): def __init__(self,l_txt,tv_txt): self.background_color=bg_color self.frame = (0,0,700,700) title = ui.Label() title.font= text_font, 25 title.text = l_txt title.alignment= ui.ALIGN_CENTER title.frame=screen_size.w * 0.1, screen_size.h * 0.02, screen_size.w * 0.75, screen_size.h * 0.05 if tv_txt.startswith('http'): tv = ui.Button() tv.title = tv_txt def open_url(sender): wv = ui.WebView() wv.load_url(sender.title) wv.frame = sender.superview.frame wv.present('sheet') wv.wait_modal() tv.action = open_url else: tv = ui.TextView() tv.editable=False tv.scroll_enabled=True tv.auto_content_inset=True tv.number_of_lines= 0 tv.line_break_mode = ui.LB_CHAR_WRAP tv.text = tv_txt tv.background_color=bg_color tv.alignment= ui.ALIGN_CENTER tv.font= text_font, 22 tv.frame= screen_size.w * 0.07, screen_size.h * 0.08, screen_size.w * 0.87, screen_size.h * 0.85 self.add_subview(title) self.add_subview(tv)
-
@resserone13 i'm no more available during one hour...sorry
-
-
@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) . . .