@JonB
Ah yes, so you mean the codes of @TPO, posted 2 years ago. Sorry, I did't get it and was misunderstanding your suggestion. Well, I think I need to try it. Will let you know how it works.
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.
Posts made by satsuki.kojima
-
RE: Problem when running a ui program from shortcut.
-
RE: Problem when running a ui program from shortcut.
@JonB
Excuse my slow response. I knew, advised by you, that the same discussion was held in this forum 2 years ago. There were many ideas in that discussion as well although I haven't been all through this yet.
I am very new to singleton approach and my understanding is that singleton pattern can be implemented in one running program. Am I right? If I run my program twice and create a thread in each program, then those threads have to be different threads(different instances,) I think.
Given many ideas, I'll keep posting for any update. Thank you so much.@cvp
Your solution seems working. Mysterious behavior (which I described) may be caused by my impatient testing. (run a program, close Pythonista, run a program again with no break but only clicking the shortcut icon etc.) -
RE: Problem when running a ui program from shortcut.
@cvp
I closed all the tab of my script, and now it works all fine so far. -
RE: Problem when running a ui program from shortcut.
Yeah, this seems to work somehow, I mean I’m not quite sure why it works.
Anyway, adding your codes, I run my program by shortcut, leave it without closing it, once again I click the shortcut and now the previous view is closed. But this behavior is not stable, sometimes the view is still running behind the new view. I’m trying to find what makes this difference of behavior.So far, by adding some print statement as follows:
class my_thread(threading.Thread):
global main_viewdef __init__(self): self.timestamp = time.time() threading.Thread.__init__(self) def run(self): while True: time.sleep(1) print(self.timestamp, console.is_in_background()) if console.is_in_background(): print('closing view') main_view.close() break
Seems like my_thraead closes the view only when the Pythonista itself is re-opened.
For example when I run this program on Pythonista (not from shortcut,) close the view and then again run the program, the output in the console shows like this:1618751761.064212 False
1618751765.282424 False
1618751761.064212 False
1618751765.282424 False
1618751761.064212 False
1618751765.282424 False
;
;
(meaning two threads are still running)and when I go back to the home screen and reopen Pythonista, the output shows like this:
1618751761.064212 True
closing view
1618751765.282424 True
closing viewI’m not sure but it may make sense. What do you think?
Still, I have some mysterious behavior so keep working on it. Thank you for your help. I didn’t think about using “console.is_in_background()”. -
RE: Custom Image for a Imageview?
@RocketBlaster05:
instead of,
img = ui.Image.named('companylogo.PNG')
try :
img = ui.Image('companylogo.PNG')I think that would work.
-
Problem when running a ui program from shortcut.
I put a home screen icon with shortcut to run a program I made. The program is with a ui view and the problem is that when I leave the view after using it without closing it (which we usually do when using a tablet,) and next time I run a program from the shortcut, a new view is invoked over the same view I opened last time. That seems natural because shortcut is only to open a program and run it, but I wonder if there is any way to avoid opening as many views as I click the icon. It seems difficult to manage a program running in a different thread, but as I believe we should mostly face the same problem when making a shortcut, does anyone have good idea on it?
-
RE: How can I manage controls over two views
not @csv,
@cvp
Excuse my type mistake. -
RE: How can I manage controls over two views
@csv
Excuse my previous posting, I think I now solve this problem. Looking back to your advise, I no more need “wait_modal().” in your first sample, right? By deleting “wait_modal()”, it looks to work Ok now, thanks. -
RE: How can I manage controls over two views
@cvp
What I’m doing now is calling a view which is already made using view class (it’s like my original font-picker), so I would rather choose your first sample. I pass a label object when calling a popup view as an argument and it seems almost perfect except one thing.In case of your first sample (which is without @ui.in_background,) when getting back from popup, the button looks stay grey even though I can still press the button. (so it just looks grey.) I thought putting “@ui.in_background” would solve it, but not really.
When putting “@ui.in_background” before def onBtn, after returning from popup the button now looks ok (I mean the color,) but the button never responds after it. Only after closing the main view, the button’s action occurs. -
RE: How can I manage controls over two views
Yeah! your codes now put the texts back to the main view.
You can't put a wait_modal inside a button action, because the ui thread blocks until the button exits
Ok, so this was the problem.
and this is why the button still looks grey after closing the pop-up?“@ui.in_background” didn’t work.
“using a "shield" view” > means using add_subview, right? Yes, I believe this would work for handing data.
dialog modules> I didn’t think about. I will check this.Thanks for your many hints.
-
RE: How can I manage controls over two views
@satsuki.kojima said:
Excuse my codes, but it always happens when embedding them from pythonista’s editor.
Tabs and under scores(__) of init are gone when I posted. Please fix them before running my codes. -
How can I manage controls over two views
This maybe an easy question but I cannot figure out myself.
I want to show a pop-up view over my main view, selecting something on pop-up and send the selected thing back to the main view.
I made up a simple codes to show what I want to do.
By pressing a button, another small view is shown and putting some texts in a textfield and close it, I expect the texts will be put on the main view’s label. However it will not happen and only after closing the main view, I find in the console the texts are surely printed by print statement. Seems like control is not actually back to the program after closing the pop-up. How can I fix it?<test codes>
import ui class Test(ui.View): def __init__(self, frame): self.frame = frame self.bg_color = 'white' btn = ui.Button(frame=(100,100,100,30), title='Open pop', action = self.onBtn) self.label = ui.Label(frame=(100,200,400,30), border_width=1) self.add_subview(btn) self.add_subview(self.label) def onBtn(self, sender): self.label.text = 'calling pop' pop = Popup() pop.present('sheet') self.wait_modal() text = pop.get_text() self.label.text = text pop.close() print('after get_text', text ) class Popup(ui.View): def __init__(self): self.frame = (100,100,400,600) self.bg_color = '#ededed' self.textbox = ui.TextField(frame=(100,100,200,30)) self.add_subview(self.textbox) self.right_button_items = [ui.ButtonItem(title='close', action=lambda x:self.close())] def get_text(self): return self.textbox.text if __name__ == '__main__': w, h = ui.get_screen_size() v = Test((0,0,w,h)) v.present('sheet')
-
RE: Can I save ui.Image as a jpeg file ?
Now, I can make it a PIL image this way. Thank you so much.
-
RE: Can I save ui.Image as a jpeg file ?
@cvp
Thank you. I could make a png external file.
Now, I have another problem. I thought png file would do, but I wanted to combine the image with a photo pictures which are usually in jpeg. When I try it with paste method of PIL, such asphoto_img.paste(img_labels)
where photo_img is in jpeg format and img_labels is in png format, this will result an error “images do not match.”
If you think of any good solution, let me know. Thank you. -
Can I save ui.Image as a jpeg file ?
Can I save ui.Image as an external file with the format of jpeg (or png). If I can transform ui.Image into PIL Image, that would do.
What I want to do is snapshot an image on ui.View and save that image as a jpeg file.
Here is my code and I can only make a ui.Image, but don’t know how to make a jpeg file from it.with ui.ImageContext(100, 100) as ctx:
...
... v = ui.ImageView(width=100,height=100)
... label = ui.Label(text='testlabel')
... v.add_subview(label)
... v.draw_snapshot()
... label_img= ctx.get_image()
...
type(label_img)
<class '_ui.Image'> -
RE: Can I add custom fonts in font picker.
@satsuki.kojima said:
I have picked the part of my codes to show the picker. And I also have installed some custom fonts in iOS and they are seen in setting.
==============================================================
import ui
from objc_util import *def UIFontPickerViewController():
v = ui.View()
vc = ObjCInstance(v)
fontpicker = ObjCClass('UIFontPickerViewController').new().autorelease()
fontview = fontpicker.view()
v.frame = (0,0,512,960)
vc.addSubview_(fontview)
done_button = ui.ButtonItem(title='OK')
def tapped(sender):
sel = fontpicker.selectedFontDescriptor()
if sel != None:
v.font= str(sel.objectForKey_('NSFontFamilyAttribute'))
v.close()
done_button.action = tapped
v.right_button_items = [done_button]
v.font = None
v.present('sheet')
v.wait_modal()
return v.fontdef show_font_picker(sender):
font = UIFontPickerViewController()
print(font)
if font != None:
label.text = font
label.font = (font, 18)root = ui.View(bg_color='white')
button = ui.Button(
frame=(100,100,150,60),
title='Pick font',
border_width=1,
action=show_font_picker
)
label = ui.Label(
frame=(200,100,300,60),
tint_color='black',
border_width=1)root.add_subview(button)
root.add_subview(label)
root.present('fullscreen')Well, tab spaces were cut from the above codes when embedded to here. If you could try the above codes in your device, need to fix the tabs.
-
RE: Can I add custom fonts in font picker.
I have picked the part of my codes to show the picker. And I also have installed some custom fonts in iOS and they are seen in setting.
============================================================== import ui from objc_util import * def UIFontPickerViewController(): v = ui.View() vc = ObjCInstance(v) fontpicker = ObjCClass('UIFontPickerViewController').new().autorelease() fontview = fontpicker.view() v.frame = (0,0,512,960) vc.addSubview_(fontview) done_button = ui.ButtonItem(title='OK') def tapped(sender): sel = fontpicker.selectedFontDescriptor() if sel != None: v.font= str(sel.objectForKey_('NSFontFamilyAttribute')) v.close() done_button.action = tapped v.right_button_items = [done_button] v.font = None v.present('sheet') v.wait_modal() return v.font def show_font_picker(sender): font = UIFontPickerViewController() print(font) if font != None: label.text = font label.font = (font, 18) root = ui.View(bg_color='white') button = ui.Button( frame=(100,100,150,60), title='Pick font', border_width=1, action=show_font_picker ) label = ui.Label( frame=(200,100,300,60), tint_color='black', border_width=1) root.add_subview(button) root.add_subview(label) root.present('fullscreen') ============================================================
-
Can I add custom fonts in font picker.
I want to show some custom fonts in a font picker. I’m currently using ‘UIFontPickerViewController’ for listing fonts and I have installed some custom fonts in iOS, but I don’t know how or if I can add those fonts in the font picker. Can I and how?
-
RE: How can I add an easer in “sketch.py” in Example?
to:@cvp
Yeah.... I finally decided my eraser to set the line_width to 20 while the default pen to 2.
I’m building a game “4 numbers”, which is to guess 4 randomly given numbers by hints of hits and blows. I wanted to add a sketch board so the players can scribble their process of guessing.By the way, I actually took for a while to fix my problem even though your advise was very clear. My sketch board showed some odd behavior. I finally found it was because I was writing my program adding subview of path_view before image_view like as below.
iv = ui.ImageView(frame=(0, 0, width, height)) pv = PathView(frame=self.bounds) pv.action = self.path_action pv.main = self self.add_subview(pv). [ <- shoud be after self.add_subview(iv)] self.add_subview(iv)