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.
search for phrase within script
-
Is there a search function in the pythonista editor? How can I find a specific word or phrase in a script on iphone without reading through the entire script looking for it?
-
-
Thank you.
-
I have yet another question...over the past few months I have written a number of scripts in which I use a number of TextFields and Buttons in multiple views within the script. I have been creating duplicate versions of the TextFields and Buttons to use in each of the various views. I have been having some small success lately creating classes so I thought I could create a class to contain a set of fields and buttons that I could instantiate for each view. I tried writing a simple class and I can instantiate a button and field in a view but I have so far been unable to populate the field as a result of the button push. If I change the value of the TextField text in the button press function, it prints correctly to the console but does not update the contents of the field in the view. What am I doing wrong? Thanks.
import ui # class Common_View (object): def __init__(self): pass def select_author(sender): print(self.author_result.text) self.author_result.text = 'selected author' print(self.author_result.text) return self.author_button = ui.Button(title='Author', frame=(10,5,350,100), border_color='black', border_width=2, background_color='#EAECEE', action=select_author, font=('Arial Rounded MT Bold',18)) self.author_button.width=70 self.author_button.height=40 self.author_result = ui.TextField(name='author',frame=(100,5,250,40), border_color='black', border_width=3, text= 'empty') # w, h = ui.get_screen_size() view = ui.View(name= 'Test View', bg_color='lightblue', frame=(0, 0, w, h)) main_author = Common_View().author_result view.add_subview(main_author) main_author_button = Common_View().author_button view.add_subview(main_author_button) nav_view = ui.NavigationView(view) nav_view.present('sheet')
-
The issue is that you create two different CommonViews, but the button refers to one, and the Test field is from the other. So self.author_response doesn't do what you want.
Create one instance:
C=CommonView() #then refer to view.add_subview(C.author_response) view.add_subview(C.author_button)
Instead of creating new objects each time.
Also, you can subclass ui.View, so that the add_subviews would be inside init, and would be self.add_subview, etc.
class Common_View(ui.View): def select_author(self, sender): print(self.author_result.text) self.author_result.text = "selected author" print(self.author_result.text) return def __init__(self, *args, **kwargs): self.author_button = ui.Button( title="Author", frame=(10, 5, 350, 100), border_color="black", border_width=2, background_color="#EAECEE", action=self.select_author, font=("Arial Rounded MT Bold", 18), ) self.author_button.width = 70 self.author_button.height = 40 self.author_result = ui.TextField( name="author", frame=(100, 5, 250, 40), border_color="black", border_width=3, text="empty", ) w, h = ui.get_screen_size() ui.View.__init__( self, name="Test View", bg_color="lightblue", frame=(0, 0, w, h), **kwargs ) self.add_subview(main_author) self.add_subview(main_author_button) view = CommonView() nav_view = ui.NavigationView(view) nav_view.present("sheet")
-
Sorry white space maybe got messed up -- both defs should be at same level, inside the class. And the spacing in second method got screwed up too. But hopefully this helps
-
Thank you. The first suggestion works and I like the idea of subclassing ui.View, but after I moved the white space around, I'm still getting an error message:
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/99B62839-B39E-4ADF-9308-D5D986E28E91/Pythonista3/Documents/Frank/CommonView2.py", line 19, in <module>
view = Common_View()
File "/private/var/mobile/Containers/Shared/AppGroup/99B62839-B39E-4ADF-9308-D5D986E28E91/Pythonista3/Documents/Frank/CommonView2.py", line 15, in init
self.add_subview(main_author)
NameError: name 'main_author' is not definedimport ui class Common_View(ui.View): def select_author(self, sender): print(self.author_result.text) self.author_result.text = "selected author" print(self.author_result.text) return def __init__(self, *args, **kwargs): self.author_button = ui.Button(title="Author", frame=(10, 5, 350, 100), border_color="black", border_width=2, background_color="#EAECEE", action=self.select_author, font=("Arial Rounded MT Bold", 18)) self.author_button.width = 70 self.author_button.height = 40 self.author_result = ui.TextField(name="author", frame=(100, 5, 250, 40), border_color="black", border_width=3, text="empty") w, h = ui.get_screen_size() ui.View.__init__(self, name="Test View", bg_color="lightblue", frame=(0, 0, w, h), **kwargs) self.add_subview(main_author) self.add_subview(main_author_button) view = Common_View() nav_view = ui.NavigationView(view) nav_view.present("sheet")```
-
No need to respond, I figured out the problem.