Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for toga.Selection on android for value #1914

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1723.bugfix.rst
@@ -0,0 +1 @@
A Selection widget with no items now consistently returns a current value of ``None`` on all platforms.
4 changes: 3 additions & 1 deletion cocoa/src/toga_cocoa/widgets/selection.py
Expand Up @@ -43,7 +43,9 @@ def select_item(self, item):
self.native.selectItemWithTitle(item)

def get_selected_item(self):
return str(self.native.titleOfSelectedItem)
selected = self.native.titleOfSelectedItem
if selected:
return str(selected)

def set_on_select(self, handler):
pass
26 changes: 25 additions & 1 deletion examples/selection/selection/app.py
Expand Up @@ -19,6 +19,9 @@ def startup(self):

# Add the content on the main window
self.selection = toga.Selection(items=self.OPTIONS)
self.empty_selection = toga.Selection()

self.report_label = toga.Label("", style=label_style)

self.main_window.content = toga.Box(
children=[
Expand All @@ -29,6 +32,22 @@ def startup(self):
self.selection,
],
),
toga.Box(
style=box_style,
children=[
toga.Label("Empty selection", style=label_style),
self.empty_selection,
],
),
toga.Box(
style=box_style,
children=[
toga.Button(
"Report on selection", on_press=self.report_selection
),
self.report_label,
],
),
toga.Box(
style=box_style,
children=[
Expand Down Expand Up @@ -66,7 +85,7 @@ def startup(self):
toga.Box(
style=box_style,
children=[
toga.Label("use some style!", style=label_style),
toga.Label("Use some style!", style=label_style),
toga.Selection(
style=Pack(width=200, padding=24),
items=["Curium", "Titanium", "Copernicium"],
Expand Down Expand Up @@ -113,6 +132,11 @@ def my_on_select(self, selection):

print(f"The selection widget changed to {selection.value}")

def report_selection(self, widget):
self.report_label.text = (
f"Element: {self.selection.value!r}; Empty: {self.empty_selection.value!r}"
)


def main():
# App name and namespace
Expand Down
5 changes: 4 additions & 1 deletion iOS/src/toga_iOS/widgets/selection.py
Expand Up @@ -69,7 +69,10 @@ def select_item(self, item):
self.interface.factory.not_implemented("Selection.select_item()")

def get_selected_item(self):
return self.interface.items[self.picker.selectedRowInComponent(0)]
try:
return self.interface.items[self.picker.selectedRowInComponent(0)]
except IndexError:
return None

def set_on_select(self, handler):
# No special handling required
Expand Down