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 all 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
1 change: 1 addition & 0 deletions changes/1821.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NumberInput now supports changing alignment on GTK.
19 changes: 14 additions & 5 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,14 @@ def do_extract_values(self, widget, **kwargs):
self.password_input.value,
)

number = self.number_input.value
if number:
self.number_label.text = f"Double the number is: {number * 2}"
else:
self.number_label.text = "You didn't enter a number"
try:
number = self.number_input.value + self.right_aligned_number_input.value
self.number_label.text = (
f"The sum of {self.number_input.value} and "
f"{self.right_aligned_number_input.value} number is: {number}"
)
except TypeError:
self.number_label.text = "Please enter a number in each number input."

# Wait 5 seconds
for i in range(5, 0, -1):
Expand All @@ -47,6 +51,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 +80,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 +118,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