#๐ deleting long label
65 messages ยท Page 1 of 1 (latest)
@boreal wharf
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
i think we need more context
show us your code for example
this will be in kivy, can u send me the quote like, i cant find it in my keyboard
ill put comment to these ideas
!code
you got them up there
from kivymd.app import MDApp
from kivy.core.window import Window
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
from kivy.clock import Clock
Window.size = (360, 600)
class MyApp(MDApp):
def build(self):
self.data=[]
return
def GoToSecondScreen(self):
self.root.current = 'second'
def GoToFirstScreen(self):
self.root.current = 'first'
# for My DropDown
def open_menu(self, caller):
menu_items = [
{"text": "About", "on_release": lambda: self.menu_callback("About Us")}
]
self.menu = MDDropdownMenu(
caller=caller,
items=menu_items,
width_mult=4,
)
self.menu.open()
def menu_callback(self, text_item):
print(f"You selected: {text_item}")
self.menu.dismiss()
# for my function e.g add, update, delete
def add_card(self):
text = self.root.ids.TextField.text.strip()
if text == '':
return
self.data.append(text)
card = MDCard(
size_hint=(0.45, None), # width relative, height fixed
height=150,
radius=15,
md_bg_color=(0, 1, 0, 1)
)
card.add_widget(MDLabel(text=text, halign="center"))
#my idea is if i click this label i want to have an ability to delete it.
#it is storing 50letters more or so.
self.root.ids.listview.add_widget(card)
self.root.ids.TextField.text = ""
MyApp().run()
this is a todo list app
@grand marten
got an idea?
So you want to delete a particular index of array when a certain event is triggered?
yes
that's it
Can you offer an easier way?
If you're using list, just use
my_list.pop(index)
You can also use
del my_list[index]
wouldn't that be ineffective?
i mean. a user will just normally select a UI and deletes it
a little bit if you have very long lists
Yes, It can take O(N) time
but that is the nature of lists
wdym?
but for short lists in an app like this it shouldn't be any problems
can u give me a sample?
You can you Linked list if you want, but then accessing any particular block will take O(N) time
how can a user delete specific data with index
you must know the index of the element that you want to delete from the list
i still not very familiar with linked list even tho i can create node i still can't imagine any use of it
i wouldn't worry too much about performance of a list in the context of a to-do gui app
See, I have a better idea... Use a dictionary, for every entry you take, store it with certain key and value.... When delete event is triggered, find out which key is to be deleted and remove it from dict
can u suggest another way of deleting?
that's what im thinking, storing it in another variable when clicked and then delete the match but i dont know how
as @slate jasper mentioned, dictionaries are better if you need fast access and ability to remove any entry in an efficient way
but as i said, just forget about the performance aspect of it all
can u give me a sample? i just need to have the grasp of it and theoritically apply it later
its a project i need the full performance
your coding a gui application in python, if performance was a top priority you probably wouldn't be using python in the first place
if you do, you'll notice, first implement something that works
performance over aesthetics were the instruction
instructions from?
instructor
# Dictionary to store values
data = {}
key_counter = 1 # To assign incremental keys
def add_value(value):
global key_counter
data[key_counter] = value
key_counter += 1
def delete_value(key):
if key in data:
data.pop(key)
else:
print(f"Key {key} not found!")
Example
add_value("apple")
add_value("banana")
delete_value(1)
print(data) # {2: 'banana'}
This will have better performance
how can i assign value 1 when in interfac?
it picks the id for new items from the counter
the counter always counts up for each item that is added
When a user add a todo, assign a id to that task.... Store the task as value and id as key in dictionary, when a user presses delete, find out the id in dictionary and delete it
when deleting items there will be a gap in the ids, but that shouldn't matter
can u show me a sample?
they did, above
i thoght dictionary is this
key : values inside array
wait, wouldn't this just deletes the last item added?
it is (the {"key": "value"} thing, kind of), but not inside an array
and what you are calling "array" is named "list" in python
import random
import string
tasks = {}
def generate_id():
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=4))
def add_task(task):
task_id = generate_id()
tasks[task_id] = task
print(f"Task added: {task_id} -> {task}")
return task_id
def delete_task(task_id):
if task_id in tasks:
removed = tasks.pop(task_id)
print(f"Task deleted: {task_id} -> {removed}")
else:
print(f"No task found with ID {task_id}")
# Example usage
task = input("Add your task: ")
task_id = add_task(task)
print("\nAll tasks:", tasks)
# Example delete call
# delete_task(task_id)
No it won't, you need to give key to it, if you passed 1, it will delete 1st item added
i get it now so how can i pass id in generate id every add of element of list and how can i select that specific id just by clicking something
See... Whats happening here is
When a user is giving some input, it is generating a random id using generate_id() function and assigns that id to that particular input from user and adds it in dictionary... What you need to do is just pass that particular generated id in delete_task() function and it will remove that task as well
You can make it trigger through frontend using flask or fastapi
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.