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.
Issue changing ui button title
-
If I create a worklow that runs the sample code in the docs for the ui module found here: getting started it works as described and expected.
However, if I change it to the following:
import ui def button_tapped(sender): sender.title = 'Hello' view = ui.View() # [1] view.name = 'Demo' # [2] view.background_color = 'white' # [3] button = ui.Button() # [4] button.title='Tap me!' button.center = (view.width * 0.5, view.height * 0.5) # [5] button.flex = 'LRTB' # [6] button.action = button_tapped # [7] view.add_subview(button) # [8] view.present('sheet') # [9]
(i.e. Moving the title assignment to a separate line), it doesn't work. The button doesn't appear. Any idea why? I assume that button titles can be changed after the fact b/c the button_tapped function in the original code sample worked.
-
I'm not sure if this is an answer, but by initializing the ui.Button with a None title it works.
import ui def button_tapped(sender): sender.title = 'Hello' view = ui.View() # [1] view.name = 'Demo' # [2] view.background_color = 'white' # [3] button = ui.Button(title=None) # [4] button.title='Tap me!' button.center = (view.width * 0.5, view.height * 0.5) # [5] button.flex = 'LRTB' # [6] button.action = button_tapped # [7] view.add_subview(button) # [8] view.present('sheet') # [9]
I'm not certain as to what would cause this. Perhaps Button needs to have the title initialized with the object or it won't work.
Hope this helps :)
Ninja Edit: Looking at the documentation, it says that when you initialize with a title, the button resizes automatically to fit the text. What may be happening is that the title is being set, but the button is not resizing to accommodate the new text, so it just doesn't show up. I'll poke around the API a bit more and get back to you with what I find.
-
@derickfay, I'm posting again with the answer to your problem. Looking through the API I was correct in my assumption that the button is not resizing to accommodate the size of the text, so it is not showing the title. I fixed this problem by setting the width and height of the button after initialization (since ui.Button is a subclass of View, I can access those attributes). Here is the updated code:
import ui def button_tapped(sender): sender.title = 'Hello' view = ui.View() # [1] view.name = 'Demo' # [2] view.background_color = 'white' # [3] button = ui.Button() # [4] button.title='Tap me!' button.width = 100 button.height = 20 button.center = (view.width * 0.5, view.height * 0.5) # [5] button.flex = 'LRTB' # [6] button.action = button_tapped # [7] view.add_subview(button) # [8] view.present('sheet') # [9]
I hope this helps :)
-
You can also set the size of a button by calling
button.size_to_fit()
(after setting the title). This will automatically make it large enough to show the text. -
Excellent - thanks for the help!