Replies: 4 comments 4 replies
-
Welcome to programming. Firstly, my apologies - the below is using a class structure, and I notice you were using an imperative style. If you aren't yet using classes - my recommendation would be that you should start, especially when writing GUI code. They are much more manageable in the long run, and actually, much easier to work with. All of the cumbersome passing of global variables, difficulties with mega long scripts, or just unreadable code, and things that just don't feel right - it is what classes were made for. You mention that you have a json file - I would write a function which can return a Python list for a given json file, the json library will help you, and it will look like this from memory: import json
python_list = json.loads(json_array_string) I won't say that I was bored.. but I have actually left a few comments. Good luck! import flet as ft
class CharacterListView(ft.ListView):
"""
A custom Flet control that displays a list of strings in a scrollable view.
Inherits from ft.ListView and populates itself based on a provided list.
"""
def __init__(self, character_names: list[str]):
"""
Initializes the CharacterListView.
Args:
character_names: A list of strings, where each string is a character name.
"""
# Call the constructor of the parent class (ft.ListView)
# expand=True allows the ListView to fill available vertical space, enabling scrolling
# spacing adds vertical space between list items
# padding adds space around the entire list content
# auto_scroll=False ensures manual scrolling works as expected by default
# build_controls_on_demand if you have a long list - to keep it performant
super().__init__(expand=True, spacing=10, padding=20, auto_scroll=False, build_controls_on_demand = True)
# Populate the ListView's controls property
# Iterate through the provided list of names
for name in character_names:
# For each name, create a Text control and add it to the ListView's children
self.controls.append(ft.Text(name))
# --- Main Application Function ---
def main(page: ft.Page):
"""
The main entry point for the Flet application.
"""
page.title = "Long Star Wars Character List"
# --- Data ---
# Define the list.
base_star_wars_characters = [
"Luke Skywalker",
"Darth Vader",
"Leia Organa",
"Han Solo",
"Chewbacca",
"R2-D2",
"C-3PO",
"Yoda",
"Obi-Wan Kenobi",
"Emperor Palpatine"
]
# Multiply the base list by 100 for the demo
long_character_list = base_star_wars_characters * 100
# --- UI Construction ---
# Create an instance of our custom CharacterListView class
# Pass in our characters
character_list_view = CharacterListView(character_names=long_character_list)
# Add the custom ListView control to the page.
page.add(character_list_view)
# Update the page to display the added controls
page.update()
# --- Run the Application ---
if __name__ == "__main__":
ft.app(target=main) |
Beta Was this translation helpful? Give feedback.
-
Hi, thank you very much for the detailed explanations. I think the mix of
Also I have several questions on your code sample. What does Also, should I set another directory in the parameters on the, the json file method to display the musics from that directory would need, I think, a restart of the app to actually update the music displayed. Which method - I'd read of Baskets - would allow to have the Thanks ! |
Beta Was this translation helpful? Give feedback.
-
Thanks again for your replies. They are very detailed / valuable. I now understand why my code doesn't work and how I should fix it, but I'm also completely lost within my own code and I just don't know where to put things 😅. I think I'm going to try and re arrange it with classes and all rather than keep trying mixing new nice code written by you, and old wobbly code / inadequate code written following an introduction video on Flet. Also, I have one more question - kind of out of topic - : how long did it take you to understand code, how much longer to be able to write it without copy / paste / forums ? And would you have a channel or site or method or whatever that you'd recommend to learn ? I figured that by trying to make an app I'd be put in contact with many notions, which I am, but I feel like it'd be years before I'm even slightly comfortable with coding. |
Beta Was this translation helpful? Give feedback.
-
In the example I shared, we are directly adding that widget to the page - character_list_view. Because it is an instance of a class, and assigned a variable, we can manipulate it in the button clicks. page.add(
ft.Row(
[btn_star_wars, btn_shrek, btn_physicists],
alignment=ft.MainAxisAlignment.CENTER
),
# This line, we actually add that control to the page.
character_list_view Your function In your main() block, you should obtain your list using your function, and then pass the list into the MusicListView widget in the same way that we did this: character_list_view = CharacterListView()
character_list_view.update_characters(multiplied_list) |
Beta Was this translation helpful? Give feedback.
-
Question
Hi, first and foremost, I am aware of some topics related to this question. But me being new to coding, I couldn't use them to fix my issue.
So, I have a json file in which a folder path is stored as well as a global variable from another page that I intend to pass to the page of code - given below - with basket.
I'd like to have a list of every musical file present in the given folder displayed at the center of the app, in a scroll-able list.
I have tried, as you can see in the code sample, to create a container, with a row, with a controls section where ft.ListViews is. I don't think it really works, but I don't know how to make it work, and more importantly, I don't know how to make ft.ListViews actually list the files !
Hope someone is bored enough to help me, thanks in advance !
Code sample
Error message
No response
------------------------------------------------------
Beta Was this translation helpful? Give feedback.
All reactions