Skip to content

Instantly share code, notes, and snippets.

@freakboy3742
Created March 28, 2015 01:01
Show Gist options
  • Save freakboy3742/db48444048322277720b to your computer and use it in GitHub Desktop.
Save freakboy3742/db48444048322277720b to your computer and use it in GitHub Desktop.
Sample tree-using Toga project.
#!/usr/bin/env python
from __future__ import print_function, unicode_literals, absolute_import
import toga
if __name__ == '__main__':
def build(app):
main_container = toga.Container()
option_container = toga.OptionContainer()
test_tree = toga.Tree(['Test'])
root1 = test_tree.insert(None, None, 'root1')
root2 = test_tree.insert(None, None, 'root2')
root2_1 = test_tree.insert(root2, None, 'root2.1')
root2_2 = test_tree.insert(root2, None, 'root2.2')
root2_2_1 = test_tree.insert(root2_2, None, 'root2.2.1')
root2_2_2 = test_tree.insert(root2_2, None, 'root2.2.2')
root2_2_3 = test_tree.insert(root2_2, None, 'root2.2.3')
root2_3 = test_tree.insert(root2, None, 'root2.3')
root3 = test_tree.insert(None, None, 'root3')
root2_4 = test_tree.insert(root2, 1, 'root2.4')
problem_tree = toga.Tree(['Test'])
root1 = problem_tree.insert(None, None, 'root1')
root2 = problem_tree.insert(None, None, 'root2')
root2_1 = problem_tree.insert(root2, None, 'root2.1')
root2_2 = problem_tree.insert(root2, None, 'root2.2')
root2_2_1 = problem_tree.insert(root2_2, None, 'root2.2.1')
root2_2_2 = problem_tree.insert(root2_2, None, 'root2.2.2')
root2_2_3 = problem_tree.insert(root2_2, None, 'root2.2.3')
option_container.add('Tests', test_tree)
option_container.add('Problems', problem_tree)
details_panel = toga.Container()
name_label = toga.Label('Name:', alignment=toga.RIGHT_ALIGNED)
duration_label = toga.Label('Duration:', alignment=toga.RIGHT_ALIGNED)
description_label = toga.Label('Description:', alignment=toga.RIGHT_ALIGNED)
output_label = toga.Label('Output:', alignment=toga.RIGHT_ALIGNED)
error_label = toga.Label('Error:', alignment=toga.RIGHT_ALIGNED)
details_panel.add(name_label)
details_panel.add(duration_label)
details_panel.add(description_label)
details_panel.add(output_label)
details_panel.add(error_label)
name = toga.TextInput('test.path.here')
duration = toga.TextInput('duration')
description = toga.TextInput('this is the description')
output = toga.MultilineTextInput('line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\n')
error = toga.MultilineTextInput('line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nbecause this is very long text that must not be allowed to scroll. because this is very long text that must not be allowed to scroll. because this is very long text that must not be allowed to scroll. because this is very long text that must not be allowed to scroll. because this is very long text that must not be allowed to scroll. because this is very long text that must not be allowed to scroll. because this is very long text that must not be allowed to scroll. because this is very long text that must not be allowed to scroll. ')
details_panel.add(name)
details_panel.add(duration)
details_panel.add(description)
details_panel.add(output)
details_panel.add(error)
details_panel.constrain(name_label.RIGHT + 10 == name.LEFT)
details_panel.constrain(duration_label.RIGHT + 10 == duration.LEFT)
details_panel.constrain(description_label.RIGHT + 10 == description.LEFT)
details_panel.constrain(output_label.RIGHT + 10 == output.LEFT)
details_panel.constrain(error_label.RIGHT + 10 == error.LEFT)
details_panel.constrain(name_label.TOP == name.TOP)
details_panel.constrain(duration_label.TOP == duration.TOP)
details_panel.constrain(description_label.TOP == description.TOP)
details_panel.constrain(output_label.TOP == output.TOP)
details_panel.constrain(error_label.TOP == error.TOP)
details_panel.constrain(name.TOP == details_panel.TOP + 10)
details_panel.constrain(duration.TOP == name.BOTTOM + 10)
details_panel.constrain(description.TOP == duration.BOTTOM + 10)
details_panel.constrain(output.TOP == description.BOTTOM + 10)
details_panel.constrain(error.TOP == output.BOTTOM + 10)
details_panel.constrain(details_panel.BOTTOM == error.BOTTOM + 10)
details_panel.constrain(name_label.WIDTH == duration_label.WIDTH)
details_panel.constrain(name_label.WIDTH == description_label.WIDTH)
details_panel.constrain(name_label.WIDTH == output_label.WIDTH)
details_panel.constrain(name_label.WIDTH == error_label.WIDTH)
details_panel.constrain(name.WIDTH == duration.WIDTH)
details_panel.constrain(name.WIDTH == description.WIDTH)
details_panel.constrain(name.WIDTH == output.WIDTH)
details_panel.constrain(name.WIDTH == error.WIDTH)
details_panel.constrain(details_panel.LEFT + 10 == name_label.LEFT)
details_panel.constrain(details_panel.LEFT + 10 == duration_label.LEFT)
details_panel.constrain(details_panel.LEFT + 10 == description_label.LEFT)
details_panel.constrain(details_panel.LEFT + 10 == output_label.LEFT)
details_panel.constrain(details_panel.LEFT + 10 == error_label.LEFT)
details_panel.constrain(name.RIGHT + 10 == details_panel.RIGHT)
details_panel.constrain(duration.RIGHT + 10 == details_panel.RIGHT)
details_panel.constrain(description.RIGHT + 10 == details_panel.RIGHT)
details_panel.constrain(output.RIGHT + 10 == details_panel.RIGHT)
details_panel.constrain(error.RIGHT + 10 == details_panel.RIGHT)
details_panel.constrain(2 * output.HEIGHT == error.HEIGHT)
# Constraints that should be enforced by the widget itself.
# details_panel.constrain(name_label.WIDTH == name.WIDTH/2)
# details_panel.constrain(error.HEIGHT > 100)
# details_panel.constrain(output.HEIGHT > 10)
split = toga.SplitContainer()
split.content = [option_container, details_panel]
main_container.add(split)
main_container.constrain(split.LEFT == main_container.LEFT)
main_container.constrain(split.TOP == main_container.TOP)
main_container.constrain(split.RIGHT == main_container.RIGHT)
main_container.constrain(2 * option_container.WIDTH > details_panel.WIDTH)
footer = toga.Container()
status_label = toga.Label('Running...')
progress = toga.ProgressBar(max=10)
footer.add(status_label)
footer.add(progress)
footer.constrain(status_label.LEFT == footer.LEFT + 5)
footer.constrain(status_label.TOP == footer.TOP + 5)
footer.constrain(status_label.BOTTOM + 5 == footer.BOTTOM)
footer.constrain(status_label.RIGHT + 5 == progress.LEFT)
footer.constrain(progress.WIDTH == 150)
footer.constrain(progress.RIGHT + 5 == footer.RIGHT)
footer.constrain(progress.TOP == footer.TOP + 5)
main_container.add(footer)
main_container.constrain(main_container.LEFT == footer.LEFT)
main_container.constrain(main_container.BOTTOM == footer.BOTTOM)
main_container.constrain(main_container.RIGHT == footer.RIGHT)
main_container.constrain(footer.TOP == split.BOTTOM)
# app.main_window.content = main_container
def action1(widget):
toga.Dialog.info('info panel', 'Who put the bop in the bop-she-bop-she-bop?')
def action2(widget):
if toga.Dialog.question('question panel', 'Do you wanna?'):
progress.start()
for i in range(0, 10):
progress.value = i
yield 1
progress.value = 10
progress.stop()
else:
print ("NO")
def action3(widget):
if toga.Dialog.confirm('error panel', 'Do we want to go ahead?'):
print ("Yes")
else:
print ("no")
def action4(widget):
toga.Dialog.error('error panel', 'Who put the bop in the bop-she-bop-she-bop?')
def action5(widget):
toga.Dialog.stack_trace('stack trace panel', 'Who put the bop in the bop-she-bop-she-bop?', 'long content 1\nlong content 2\nlong content 3\nlong content 4\nlong content 5\nlong content 6\nlong content 7\nlong content 1\nlong content 2\nlong content 3\nlong content 4\nlong content 5\nlong content 6\nlong content 7\n')
stop_cmd = toga.Command(action1, 'Stop', tooltip='Stop test suite', icon='icons/brutus.icns')
run_all_cmd = toga.Command(action2, 'Run all', tooltip='Run all', icon=toga.TIBERIUS_ICON)
run_selected_cmd = toga.Command(action3, 'Run selected', tooltip='Run selected', icon='icons/cricket-72.png')
error_cmd = toga.Command(action4, 'Error', tooltip='Generate Error', icon='icons/cricket-72.png')
stack_cmd = toga.Command(action5, 'Stack', tooltip='Generate Stack trace', icon='icons/cricket-72.png')
app.main_window.toolbar = [stop_cmd, run_all_cmd, run_selected_cmd, error_cmd, stack_cmd]
return main_container
app = toga.App('Cricket', 'org.pybee.cricket', startup=build)
app.main_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment