I am trying to create an app with a dropdown menu, on click then it will show an image – code attached:
“””
My first application
“””
import toga
from Tools.scripts import startuptime
from toga.style.pack import CENTER, COLUMN, ROW, Pack, BOTTOM, TOP, LEFT
import httpx
from toga import App, Box, MainWindow
from toga import ImageView, Image
import sys
def greeting(name):
if name:
if name == “Brutus”:
return “BeeWare the IDEs of Python!”
else:
return f”Hello, {name}”
else:
return “Hello, stranger”
class HelloWorld(toga.App):
def startup(self):
main_box = toga.Box(style=Pack(direction=COLUMN))
name_label = toga.Label(
"Your name: ",
style=Pack(padding=(0, 5))
)
self.name_input = toga.TextInput(style=Pack(flex=1))
name_box = toga.Box(style=Pack(direction=ROW, padding=5))
name_box.add(name_label)
name_box.add(self.name_input)
selection = toga.Selection(
items=[
{"name": "Select a muscle group"},
{"name": "Traps"},
{"name": "Delts"},
{"name": "Triceps"},
{"name": "Biceps"},
{"name": "Forearms"},
{"name": "Chest"},
{"name": "Abs"},
{"name": "Lats"},
{"name": "Legs"},
],
accessor="name",
on_change=self.clicked,
)
# Select Bob explicitly
selection.value = selection.items[0]
# selection.value = selection.items.find("Traps")
# Which item is currently selected? This will print "Charlie"
print(f"Currently selected: {selection.value}")
button = toga.Button(
"Say Hello!",
on_press=self.say_hello,
style=Pack(padding=5)
)
image_from_path = toga.Image("anat_one.png")
main_box.add(name_box)
main_box.add(selection)
main_box.add(button)
# self.triceps(main_box, image_from_path)
# print('no.1' + str(image_from_path))
self.main_window = toga.MainWindow(title=self.formal_name)
self.main_window.content = main_box
self.main_window.show()
def clicked(self, selection):
print("Clicked row", selection.value, "of table")
box = toga.Box(style=Pack(direction=COLUMN))
image_from_path = toga.Image("anat_one.png")
if 'Triceps' in str(selection.value):
# if 'Triceps' in str(selection.value):
box.add(toga.ImageView(image_from_path))
print('Triceps are proper ' + str(image_from_path))
print('box is: ' + str(box))
async def say_hello(self, widget):
async with httpx.AsyncClient() as client:
response = await client.get("https://jsonplaceholder.typicode.com/posts/42")
payload = response.json()
self.main_window.info_dialog(
greeting(self.name_input.value),
payload["body"],
)
def main():
return HelloWorld()
specifically, I added ‘on_change = self.clicked’ to the ‘selection’ dropdown menu which calls the function ‘clicked’ (also can be found in the code above):
def clicked(self, selection):
print("Clicked row", selection.value, "of table")
box = toga.Box(style=Pack(direction=COLUMN))
image_from_path = toga.Image("anat_one.png")
if 'Triceps' in str(selection.value):
# if 'Triceps' in str(selection.value):
box.add(toga.ImageView(image_from_path))
print('Triceps are proper ' + str(image_from_path))
print('box is: ' + str(box))
However, although the image exists and it shows that box.add exists, nothing appears when “Triceps” are selected from the dropdown menu
screenshot of selected dropdown option
Wrise is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.