Skip to content

Instantly share code, notes, and snippets.

@tenkabuto
Last active March 27, 2019 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenkabuto/77b8abc98db4651ac48c3c057193ce13 to your computer and use it in GitHub Desktop.
Save tenkabuto/77b8abc98db4651ac48c3c057193ce13 to your computer and use it in GitHub Desktop.
Tree example for Toga, with print out (to terminal) of what's currently selected in the tree when you click on either a row with a Title or click the button Insert Row
from random import choice
import toga
from toga.style import Pack
from toga.constants import ROW, COLUMN
bee_movies = [
{'year': 2008, 'title': 'The Secret Life of Bees', 'rating': '7.3', 'genre': 'Drama'},
{'year': 2007, 'title': 'Bee Movie', 'rating': '6.1', 'genre': 'Animation, Adventure, Comedy'},
{'year': 1998, 'title': 'Bees', 'rating': '6.3', 'genre': 'Horror'},
{'year': 2007, 'title': 'The Girl Who Swallowed Bees', 'rating': '7.5', 'genre': 'Short'},
{'year': 1974, 'title': 'Birds Do It, Bees Do It', 'rating': '7.3', 'genre': 'Documentary'},
{'year': 1998, 'title': 'Bees: A Life for the Queen', 'rating': '8.0', 'genre': 'TV Movie'},
{'year': 1994, 'title': 'Bees in Paradise', 'rating': '5.4', 'genre': 'Comedy, Musical'},
{'year': 1947, 'title': 'Keeper of the Bees', 'rating': '6.3', 'genre': 'Drama'}
]
class ExampleTreeApp(toga.App):
# Table callback functions
def on_select_handler(self, widget, node):
if node is not None and node.title:
self.label.text = 'You selected node: {}'.format(node.title)
print(dir(node))
setattr(self, 'selection', node)
print(self.selection.title)
#node.title = "Whoops!"
else:
self.label.text = 'No node selected'
# Button callback functions
def insert_handler(self, widget, **kwargs):
item = choice(bee_movies)
if item['year'] >= 2000:
root = self.decade_2000s
elif item['year'] >= 1990:
root = self.decade_1990s
elif item['year'] >= 1980:
root = self.decade_1980s
elif item['year'] >= 1970:
root = self.decade_1970s
elif item['year'] >= 1960:
root = self.decade_1960s
elif item['year'] >= 1950:
root = self.decade_1950s
else:
root = self.decade_1940s
self.tree.data.append(root, **item)
print(vars(self))
if hasattr(self, 'selection'):
print(self.selection.title)
def startup(self):
# Set up main window
self.main_window = toga.MainWindow(title=self.name)
# Label to show responses.
self.label = toga.Label('Ready.')
self.tree = toga.Tree(
headings=['Year', 'Title', 'Rating', 'Genre'],
on_select=self.on_select_handler,
style=Pack(flex=1)
)
self.decade_1940s = self.tree.data.append(None, year='1940s', title='', rating='', genre='')
self.decade_1950s = self.tree.data.append(None, year='1950s', title='', rating='', genre='')
self.decade_1960s = self.tree.data.append(None, year='1960s', title='Test', rating='', genre='')
self.decade_1970s = self.tree.data.append(None, year='1970s', title='', rating='', genre='')
self.decade_1980s = self.tree.data.append(None, year='1980s', title='', rating='', genre='')
self.decade_1990s = self.tree.data.append(None, year='1990s', title='Why', rating='', genre='')
self.decade_2000s = self.tree.data.append(None, year='2000s', title='', rating='', genre='')
# Buttons
btn_style = Pack(flex=1)
btn_insert = toga.Button('Insert Row', on_press=self.insert_handler, style=btn_style)
btn_box = toga.Box(children=[btn_insert], style=Pack(direction=ROW))
# Outermost box
outer_box = toga.Box(
children=[btn_box, self.tree, self.label],
style=Pack(
flex=1,
direction=COLUMN,
padding=10,
)
)
# Add the content on the main window
self.main_window.content = outer_box
# Show the main window
self.main_window.show()
def main():
return ExampleTreeApp('Tree', 'org.pybee.widgets.tree')
if __name__ == '__main__':
app = main()
app.main_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment