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

Number input supports alignment #1821

Merged
merged 14 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changes/1817.feature.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
TextInput now supports focus handlers and changing alignment on GTK.
kiljoy001 marked this conversation as resolved.
Show resolved Hide resolved
NumberInput now supports changing alignment on GTK.
Empty file added changes/1821.feature.rst
kiljoy001 marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
12 changes: 9 additions & 3 deletions examples/textinput/textinput/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def do_extract_values(self, widget, **kwargs):
self.right_aligned_input.enabled = False
self.password_input.enabled = False
self.number_input.enabled = False
self.right_aligned_number_input.enabled = False

# Update the labels with the extracted values
self.text_label.text = "Text content: {}; {}".format(
Expand All @@ -29,11 +30,11 @@ def do_extract_values(self, widget, **kwargs):
self.password_input.value,
)

number = self.number_input.value
if number:
number = self.number_input.value + self.right_aligned_number_input.value
if number > 0:
self.number_label.text = f"Double the number is: {number * 2}"
else:
self.number_label.text = "You didn't enter a number"
self.number_label.text = "Pick some numbers that add up to more than zero"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old logic exited because number can be blank, which means addition and doubling won't work.

This new example will crash if either value doesn't exist. It would also make sense to report the sum of the two values, rather than double one of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the logic completely to address this:

  • label shows values for each box and sum
  • added basic error handling for null values by displaying a warning to add a number to input box


# Wait 5 seconds
for i in range(5, 0, -1):
Expand All @@ -47,6 +48,7 @@ def do_extract_values(self, widget, **kwargs):
self.right_aligned_input.enabled = True
self.password_input.enabled = True
self.number_input.enabled = True
self.right_aligned_number_input.enabled = True

def startup(self):
# Set up main window
Expand Down Expand Up @@ -75,6 +77,9 @@ def startup(self):
self.right_aligned_input = toga.TextInput(
placeholder="Right aligned text", style=Pack(padding=10, text_align=RIGHT)
)
self.right_aligned_number_input = toga.NumberInput(
style=Pack(padding=10, text_align=RIGHT)
)
self.password_input = toga.PasswordInput(
placeholder="Password...",
style=Pack(padding=10),
Expand Down Expand Up @@ -110,6 +115,7 @@ def startup(self):
self.password_content_label,
self.email_input,
self.number_input,
self.right_aligned_number_input,
self.text_label,
self.password_label,
self.number_label,
Expand Down
5 changes: 3 additions & 2 deletions gtk/src/toga_gtk/widgets/numberinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from travertino.size import at_least

from ..libs import Gtk
from ..libs import Gtk, gtk_alignment
from .base import Widget


Expand Down Expand Up @@ -53,7 +53,8 @@ def set_value(self, value):
self.native.set_value(self.interface.value)

def set_alignment(self, value):
self.interface.factory.not_implemented("NumberInput.set_alignment()")
xalign, justify = gtk_alignment(value)
self.native.set_alignment(xalign)

def set_font(self, font):
self.interface.factory.not_implemented("NumberInput.set_font()")
Expand Down