gpt4 book ai didi

python - 如何从行列表中删除最后一行

转载 作者:行者123 更新时间:2023-12-01 08:38:02 25 4
gpt4 key购买 nike

我正在尝试制作一个具有一堆行的应用程序,您可以通过按按钮添加或删除这些行。

我的“添加行”按钮运行良好,现在我需要“删除行”功能。

我在列表“(self.rows.content.children)”中有行,我只需要知道如何从列表中弹出最后一行,然后继续随意添加/减去。

感谢您的浏览。

测试.py

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty, StringProperty, ListProperty
from kivy.clock import Clock

from sql_update_data import update_db

kivy.require('1.10.1')


class GUILayout(BoxLayout, GridLayout):
rows = ObjectProperty(None)

def add_more(self):
self.ids.rows.add_row()

def remove_row(self):
print("Remove last row")

def insert_value(self):
values = [row.values for row in reversed(self.rows.content.children)]

for category, id, strap in values:
update_db(category, id, strap)


class Row(BoxLayout):
button_text = StringProperty("")
id = ObjectProperty(None)
values = ListProperty()


class Rows(ScrollView):
row_count = 0
content = ObjectProperty(None)

def __init__(self, **kwargs):
super(Rows, self).__init__(**kwargs)
Clock.schedule_once(self.add_row)

def add_row(self, *args):
self.row_count += 1
self.content.add_widget(Row(button_text=str(self.row_count),
id=str(self.row_count)))


class TestApp(App):

def build(self):
return GUILayout()


GUIApp = TestApp()
GUIApp.run()

测试.kv

#: import main test
<Row>:
values: row_id.text, col1.text, col2.text
orientation: "horizontal"
spacing: 0, 5
size_hint_y: None
height: "60dp"
spacing: 2
pos_hint: {'center_x': .50, 'y': .80}

Button:
id: row_id
text: root.button_text
size_hint_x: .1
Spinner:
id: col1
text: 'Select Category'
values: ['One', 'Two', 'Three']
size_hint_x: .3
TextInput:
id: col2
size_hint_x: .8

<Rows>:
content: content
BoxLayout:
id: content
orientation: "vertical"
size_hint_y: None
height: self.minimum_height


GUILayout:


<GUILayout>:
rows: rows
orientation: "vertical"
padding: 10
spacing: 10

BoxLayout:
orientation: "horizontal"
height: 60

BoxLayout:
orientation: "horizontal"
size_hint_x: .25

TabbedPanel:
do_default_tab: False


# ----------- TAB 1 ------------

TabbedPanelItem:
text: "tab1"
BoxLayout:
orientation: 'vertical'

Rows:
id: rows

GridLayout:
rows: 1
cols: 6
padding: 1
spacing: 5
size_hint_y: None
height: 50

# --------- MINUS ---------
Button:
text: " - "
font_size: 70
size_hint_x: .1
on_press: root.remove_row()

# -------- SUBTRACT -------
Button:
text: " + "
font_size: 50
size_hint_x: .1
on_press: root.add_more()


# ----- UPDATE MAESTRO -----
Button:
text: "Update Maestro"
size_hint_x: .4
on_press: root.insert_value()


# -------- SETTINGS --------
Button:
text: "Settings"
font_size: 30
size_hint_x: .2

最佳答案

您应该做的是使用remove_widget方法从内容中删除最后一个子小部件,另一方面不要使用id作为变量的名称,因为它是保留字:

另一方面,GUILayout 不能继承 2 个小部件,它只需要来自 BoxLayout。

# ..

class GUILayout(BoxLayout):
rows = ObjectProperty(None)

def add_more(self):
self.rows.add_row()

def remove_row(self):
self.rows.remove_row()

def insert_value(self):
values = [row.values for row in reversed(self.rows.content.children)]
for category, _id, strap in values:
update_db(category, _id, strap)

#...

class Rows(ScrollView):
content = ObjectProperty(None)

def __init__(self, **kwargs):
super(Rows, self).__init__(**kwargs)
self.row_count = 0
Clock.schedule_once(lambda _: self.add_row())

def add_row(self):
self.row_count += 1
self.content.add_widget(Row(button_text=str(self.row_count),
id=str(self.row_count)))

def remove_row(self):
if self.content.children:
self.content.remove_widget(self.content.children[0])
self.row_count -= 1
# ...

关于python - 如何从行列表中删除最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53633167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com