Skip to content

Commit

Permalink
Renamed Button.label to Button.text
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Rino <bruno.rino@gmail.com>
  • Loading branch information
bruno-rino committed Jul 29, 2022
1 parent 95ceebd commit 5f3eae4
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 84 deletions.
4 changes: 2 additions & 2 deletions examples/activityindicator/activityindicator/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def do_stuff(self, widget, **kwargs):

if self.spinner.is_running:
self.spinner.stop()
self.button.label = 'Start'
self.button.text = 'Start'
else:
self.spinner.start()
self.button.label = 'Stop'
self.button.text = 'Stop'

def startup(self):
# Set up main window
Expand Down
10 changes: 5 additions & 5 deletions examples/box/box/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ def startup(self):
title=self.name, size=(800, 500), resizeable=False, minimizable=False
)
self.yellow_button = toga.Button(
label="Set yellow color",
text="Set yellow color",
on_press=self.set_yellow_color,
style=Pack(background_color=YELLOW),
)
self.inner_box = toga.Box(
style=Pack(direction=ROW),
children=[
toga.Button(
label="Set red color",
text="Set red color",
on_press=self.set_red_color,
style=Pack(background_color=RED),
),
self.yellow_button,
toga.Button(
label="Set blue color",
text="Set blue color",
on_press=self.set_blue_color,
style=Pack(background_color=BLUE),
),
toga.Button(
label="Set green color",
text="Set green color",
on_press=self.set_green_color,
style=Pack(background_color=GREEN),
),
toga.Button(
label="Reset color",
text="Reset color",
on_press=self.reset_color,
style=Pack(background_color=WHITE),
),
Expand Down
42 changes: 21 additions & 21 deletions examples/button/button/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@ def startup(self):
style_inner_box = Pack(direction=ROW)

# Button class
# Simple button with label and callback function called when
# Simple button with text and callback function called when
# hit the button
button1 = toga.Button(
'Change Label',
on_press=self.callbackLabel,
'Change Text',
on_press=self.callback_text,
style=Pack(flex=1),
)

# Button with label and enable option
# Button with text and enable option
# Keep a reference to it so it can be enabled by another button.
self.button2 = toga.Button(
'Button is disabled!',
enabled=False,
style=Pack(flex=1),
on_press=self.callbackDisable,
on_press=self.callback_disable,
)

# Button with label and style option
# Button with text and style option
button3 = toga.Button('Bigger', style=Pack(width=200))

# Button with label and callback function called
button4a = toga.Button('Make window larger', on_press=self.callbackLarger)
button4b = toga.Button('Make window smaller', on_press=self.callbackSmaller)
# Button with text and callback function called
button4a = toga.Button('Make window larger', on_press=self.callback_larger)
button4b = toga.Button('Make window smaller', on_press=self.callback_smaller)

# Box class
# Container of components
Expand All @@ -58,16 +58,16 @@ def startup(self):
]
)

# Button with label and margin style
# Button with text and margin style
button5 = toga.Button('Far from home', style=Pack(padding=50, color=BLUE))

# Button with label and RGB color
# Button with text and RGB color
button6 = toga.Button('RGB : Fashion', style=Pack(background_color=RED))

# Button with label and string color
# Button with text and string color
button7 = toga.Button('String : Fashion', style=Pack(background_color=BLUE))

# Button with label and string color
# Button with text and string color
button8 = toga.Button('Big Font', style=Pack(font_family='serif', font_size=20, font_weight='bold'))

# Add components for the second row of the outer box
Expand All @@ -93,26 +93,26 @@ def startup(self):
# Show the main window
self.main_window.show()

def callbackLabel(self, button):
def callback_text(self, button):
# Some action when you hit the button
# In this case the label will change
button.label = 'Magic {val}!!'.format(val=random.randint(0, 100))
# In this case the text will change
button.text = 'Magic {val}!!'.format(val=random.randint(0, 100))

# If the disabled button isn't enabled, enable it.
if not self.button2.enabled:
self.button2.enabled = True
self.button2.label = 'Disable button'
self.button2.text = 'Disable button'

def callbackDisable(self, button):
def callback_disable(self, button):
button.enabled = False
button.label = 'Button is disabled!'
button.text = 'Button is disabled!'

def callbackLarger(self, button):
def callback_larger(self, button):
# Some action when you hit the button
# In this case the window size will change
self.main_window.size = (1000, 600)

def callbackSmaller(self, button):
def callback_smaller(self, button):
# Some action when you hit the button
# In this case the window size will change
self.main_window.size = (200, 200)
Expand Down
2 changes: 1 addition & 1 deletion examples/canvas/canvas/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def startup(self):
toga.Label("Y Scale:", style=label_style),
self.scale_y_slider,
toga.Button(
label="Reset transform",
text="Reset transform",
on_press=self.reset_transform
)
]
Expand Down
6 changes: 3 additions & 3 deletions examples/focus/focus/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def startup(self):
self.text_input.focus()

def on_button_press(self, widget: toga.Button):
self.info_label.text = "{widget_label} was pressed!".format(
widget_label=widget.label
self.info_label.text = "{widget_text} was pressed!".format(
widget_text=widget.text
)

def on_switch_toggle(self, widget: toga.Switch):
Expand All @@ -104,7 +104,7 @@ def on_textinput_lose_focus(self, widget: toga.TextInput):

def focus_with_label(self, widget: toga.Widget):
widget.focus()
self.info_label.text = "{name} is focused!".format(name=widget.label)
self.info_label.text = "{name} is focused!".format(name=widget.text)


def main():
Expand Down
2 changes: 1 addition & 1 deletion examples/font/font/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def do_clear(self, widget, **kwargs):
self.textpanel.value = ""

def do_monospace_button(self, widget):
self.textpanel.value += widget.label + "\n"
self.textpanel.value += widget.text + "\n"

def do_icon_button(self, widget):
self.textpanel.value += widget.id + "\n"
Expand Down
16 changes: 8 additions & 8 deletions examples/layout/layout/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ class ExampleLayoutApp(toga.App):
def startup(self):

self.button_hide = toga.Button(
label='Hide label',
text='Hide label',
style=Pack(padding=10, width=120),
on_press=self.hide_label,
)

self.button_add = toga.Button(
label='Add image',
text='Add image',
style=Pack(padding=10, width=120),
on_press=self.add_image,
)

self.button_remove = toga.Button(
label='Remove image',
text='Remove image',
style=Pack(padding=10, width=120),
on_press=self.remove_image,
enabled=False,
)

self.button_insert = toga.Button(
label='Insert image',
text='Insert image',
style=Pack(padding=10, width=120),
on_press=self.insert_image,
)

self.button_reparent = toga.Button(
label='Reparent image',
text='Reparent image',
style=Pack(padding=10, width=120),
on_press=self.reparent_image,
enabled=False,
)

self.button_add_to_scroll = toga.Button(
label='Add new label',
text='Add new label',
style=Pack(padding=10, width=120),
on_press=self.add_label,
)
Expand Down Expand Up @@ -84,10 +84,10 @@ def startup(self):
def hide_label(self, sender):
if self.labels[0].style.visibility == HIDDEN:
self.labels[0].style.visibility = VISIBLE
self.button_hide.label = "Hide label"
self.button_hide.text = "Hide label"
else:
self.labels[0].style.visibility = HIDDEN
self.button_hide.label = "Show label"
self.button_hide.text = "Show label"

def add_image(self, sender):
self.content_box.add(self.image_view)
Expand Down
6 changes: 3 additions & 3 deletions examples/selection/selection/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def startup(self):
"Selection value can be set by property setter",
style=label_style,
),
toga.Button(label="Set Carbon", on_press=self.set_carbon),
toga.Button(label="Set Ytterbium", on_press=self.set_ytterbium),
toga.Button(label="Set THULIUM", on_press=self.set_thulium),
toga.Button(text="Set Carbon", on_press=self.set_carbon),
toga.Button(text="Set Ytterbium", on_press=self.set_ytterbium),
toga.Button(text="Set THULIUM", on_press=self.set_thulium),
],
),
toga.Box(
Expand Down
4 changes: 2 additions & 2 deletions src/android/toga_android/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def __init__(
- window: Toga Window
- title: Title of dialog
- message: Message of dialog
- positive_text: Button label where clicking it returns True (or None to skip)
- negative_text: Button label where clicking it returns False (or None to skip)
- positive_text: Button text where clicking it returns True (or None to skip)
- negative_text: Button text where clicking it returns False (or None to skip)
- icon: Integer used as an Android resource ID number for dialog icon (or None to skip)
"""
super().__init__()
Expand Down
4 changes: 2 additions & 2 deletions src/android/toga_android/widgets/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def create(self):
self.native = A_Button(self._native_activity)
self.native.setOnClickListener(TogaOnClickListener(button_impl=self))

def set_label(self, label):
self.native.setText(self.interface.label)
def set_text(self, text):
self.native.setText(self.interface.text)

def set_enabled(self, value):
self.native.setEnabled(value)
Expand Down
4 changes: 2 additions & 2 deletions src/cocoa/toga_cocoa/widgets/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def set_font(self, font):
if font:
self.native.font = font.bind(self.interface.factory).native

def set_label(self, label):
self.native.title = self.interface.label
def set_text(self, text):
self.native.title = self.interface.text

def set_on_press(self, handler):
# No special handling required
Expand Down
62 changes: 50 additions & 12 deletions src/core/tests/widgets/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ def setUp(self):
super().setUp()

# Create a button with the dummy factory
self.initial_label = 'Test Button'
self.btn = toga.Button(self.initial_label, factory=toga_dummy.factory)
self.initial_text = 'Test Button'
self.btn = toga.Button(self.initial_text, factory=toga_dummy.factory)

def test_widget_created(self):
self.assertEqual(self.btn._impl.interface, self.btn)
self.assertActionPerformed(self.btn, 'create Button')

def test_button_label(self):
self.assertEqual(self.btn._label, self.initial_label)
self.btn.label = 'New Label'
self.assertEqual(self.btn.label, 'New Label')
def test_button_text(self):
self.assertEqual(self.btn._text, self.initial_text)
self.btn.text = 'New Text'
self.assertEqual(self.btn.text, 'New Text')

# test if backend gets called with the right label
self.assertValueSet(self.btn, 'label', 'New Label')
# test if backend gets called with the right text
self.assertValueSet(self.btn, 'text', 'New Text')

def test_button_label_with_None(self):
self.btn.label = None
self.assertEqual(self.btn.label, '')
self.assertValueSet(self.btn, 'label', '')
def test_button_text_with_None(self):
self.btn.text = None
self.assertEqual(self.btn.text, '')
self.assertValueSet(self.btn, 'text', '')

def test_button_on_press(self):
self.assertIsNone(self.btn._on_press)
Expand All @@ -46,3 +46,41 @@ def callback(widget, **extra):
def test_focus(self):
self.btn.focus()
self.assertActionPerformed(self.btn, "focus")

######################################################################
# 2022-07: Backwards compatibility
######################################################################

def test_label_deprecated(self):
new_text = 'New Label'
with self.assertWarns(DeprecationWarning):
self.btn.label = new_text
with self.assertWarns(DeprecationWarning):
self.assertEqual(self.btn.label, new_text)
self.assertValueSet(self.btn, 'text', new_text)

def test_init_with_deprecated(self):
# label is a deprecated argument
with self.assertWarns(DeprecationWarning):
toga.Button(
label='Test Button',
factory=toga_dummy.factory
)

# can't specify both label *and* text
with self.assertRaises(ValueError):
toga.Button(
label='Test Button',
text='Test Button',
factory=toga_dummy.factory
)

# label/text is mandatory
with self.assertRaises(TypeError):
toga.Button(
factory=toga_dummy.factory
)

######################################################################
# End backwards compatibility.
######################################################################
Loading

0 comments on commit 5f3eae4

Please sign in to comment.