Kivy Removing elements from a Stack- / GridLayout

2024/10/14 20:27:29

I made a pop-up. It is basically some rows of options (max. 5 rows).

If I press the '+' button, there will be a new line of options.

If I press the '-' button the last row should diasappear. Unfortunatelly it doesn't.

I tried already the followings out in root.remove():

--> widget_path.pop(0) : no visual change, I see n rows instead of n-1 .

--> widget_path.pop() : It removes the first line instead of the last one.

--> Gridlayout (cols: 4) instead of StackLayout: similar results

Could you help me?

Here is my code:

.kv -file;

<FormPopup>:size_hint: None, Nonesize: '361pt', '220pt'BoxLayout:orientation: 'vertical'BoxLayout:size_hint: 1, Noneheight: '20pt'orientation: 'horizontal'Label:text: 'column1'Label:text: 'column2'Label:text: 'column3'Label:text: 'column4'# list of sensorsStackLayout:padding: 0spacing: 1orientation: 'lr-tb'pos_hint: {'center_x': .5, 'center_y': .5}height: self.minimum_heightid: measure_stackBoxLayout:orientation: 'horizontal'MyButton:text: '+'on_release: root.add()MyButton:text: '-'on_release: root.remove()

my .py class:

class FormPopup(Popup):"""Class: Popup for comments"""def __init__(self):super().__init__()self.error = 0self.linenumber = 0self.add()def add(self):"""add a new option-line"""widget_path = self.idsif self.linenumber < 5 :sensor_id = 'sensor_' + str(self.linenumber)widget_path['measure_stack'].add_widget(Spinner(id = sensor_id, size_hint=(None,None), height='20pt', width='85pt', text = '---', values= ('A','B','C') ))measurand_id = 'measurand_' + str(self.linenumber)widget_path['measure_stack'].add_widget(Spinner(id = measurand_id, size_hint=(None,None), height='20pt', width='85pt', text = '---', values= ('A','B','C') ) )branchwise_id = 'branchwise_' + str(self.linenumber)widget_path['measure_stack'].add_widget(Spinner(id = branchwise_id, size_hint=(None,None), height='20pt', width='85pt', text = '---', values= ('A','B','C')))procedure_id = 'procedure_' + str(self.linenumber)widget_path['measure_stack'].add_widget(Spinner(id = procedure_id, size_hint=(None,None), height='20pt', width='85pt', text = '---', values= ('A','B','C'))) self.linenumber += 1def remove(self):"""remove one option-line"""widget_path = self.ids.measure_stack.children# do not remove if there is only one lineif len(widget_path) > 4:self.linenumber -= 1for i in range(4):widget_path.pop(0)
Answer

Get the widget, and remove it.
An example of that:

from kivy.app import App
from kivy.lang import Builderroot = Builder.load_string('''BoxLayout:GridLayout:id: gridcols: 1rows: 5Label:text: "label 1"Label:text: "label 2"Label:text: "label 3"Label:text: "label 4"Button:text: "Remove last item"on_release: grid.remove_widget(grid.children[0])''')class MyApp(App):def build(self):return rootMyApp().run()
https://en.xdnf.cn/q/117914.html

Related Q&A

Bad timing when playing audio files with PyGame

When I play a sound every 0.5 second with PyGame:import pygame, timepygame.mixer.init() s = pygame.mixer.Sound("2.wav")for i in range(8):pygame.mixer.Channel(i).play(s)time.sleep(0.5)it doesn…

Using read_batch_record_features with an Estimator

(Im using tensorflow 1.0 and Python 2.7)Im having trouble getting an Estimator to work with queues. Indeed, if I use the deprecated SKCompat interface with custom data files and a given batch size, the…

How to insert integers into a list without indexing using python?

I am trying to insert values 0 - 9 into a list without indexing. For example if I have the list [4, 6, X, 9, 0, 1, 5, 7] I need to be able to insert the integers 0 - 9 into the placeholder X and test i…

Separate/reposition/translate shapes in image with pillow in python

I need to separate or translate or replace pixels in an image with python so as to all the shapes to share the same distance between each other and the limits of the canvas.background is white, shapes …

django.db.utils.OperationalError: (1045, Access denied for user user@localhost

I cant get my Django project to load my database correctly. It throws this error. Im running MariaDB with Django, and I uninstalled all MySQL I added the user by running:create database foo_db; create …

Paho Python Client with HiveMQ

i am developing a module in python that will allow me to connect my raspberry pi to a version of hivemq hosted on my pc.it connects normally but when i add hivemqs file auth plugin it doesnt seem to wo…

Comparison of value items in a dictionary and counting matches

Im using Python 2.7. Im trying to compare the value items in a dictionary.I have two problems. First is the iteration of values in a dictionary with a length of 1. I always get an error, because python…

how to send cookies inside post request

trying to send Post request with the cookies on my pc from get request #! /usr/bin/python import re #regex import urllib import urllib2 #get request x = urllib2.urlopen("http://www.example.com) #…

Flask-Uploads gives AttributeError?

from flask import Flask from flask.ext.uploads import UploadSet, configure_uploads, IMAGESapp = Flask(__name__)app.config[UPLOADED_PHOTOS_DEST] = /home/kevin photos = UploadSet(photos, IMAGES)configure…

Python: Alternate way to covert from base64 string to opencv

Im trying to convert this string in base64 (http://pastebin.com/uz4ta0RL) to something usable by OpenCV. Currently I am using this code (img_temp is the string) to use the OpenCV functions by convertin…