在 Kivy MDDataTable 中插入一行不起作用
我在 Python3 应用程序中使用 Kivy MDDataTables 。在运行时我想动态添加和删除表中的行。该文档给出了以下示例
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.datatables import MDDataTable
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.button import MDRaisedButton
class Example(MDApp):
data_tables = None
def build(self):
layout = MDFloatLayout() # root layout
# Creating control buttons.
button_box = MDBoxLayout(
pos_hint={"center_x": 0.5},
adaptive_size=True,
padding="24dp",
spacing="24dp",
)
for button_text in ["Add row", "Remove row"]:
button_box.add_widget(
MDRaisedButton(
text=button_text, on_release=self.on_button_press
)
)
# Create a table.
self.data_tables = MDDataTable(
pos_hint={"center_y": 0.5, "center_x": 0.5},
size_hint=(0.9, 0.6),
use_pagination=False,
column_data=[
("No.", dp(30)),
("Column 1", dp(40)),
("Column 2", dp(40)),
("Column 3", dp(40)),
],
row_data=[("1", "1", "2", "3")],
)
# Adding a table and buttons to the toot layout.
layout.add_widget(self.data_tables)
layout.add_widget(button_box)
return layout
def on_button_press(self, instance_button: MDRaisedButton) -> None:
'''Called when a control button is clicked.'''
try:
{
"Add row": self.add_row,
"Remove row": self.remove_row,
}[instance_button.text]()
except KeyError:
pass
def add_row(self) -> None:
last_num_row = int(self.data_tables.row_data[-1][0])
self.data_tables.add_row((str(last_num_row + 1), "1", "2", "3"))
def remove_row(self) -> None:
if len(self.data_tables.row_data) > 1:
self.data_tables.remove_row(self.data_tables.row_data[-1])
Example().run()
在调用 self.data_tables.add_row()
时,程序崩溃并出现以下错误:
AttributeError: 'MDDataTable' object has no attribute 'add_row'
我在两个不同的系统上执行了代码,并出现相同的错误:
笔记本:
- Windows 10
- Python 3.9 .7
- Kivy v2.0.0
- KivyMD 0.104.2, git-bc7d1f5,2021-06-06
- 通过 Anaconda 安装的虚拟环境 通过
- 安装的软件包Anaconda
Raspberry Pi 400:
- Raspbian GNU/Linux 11
- Python 3.9.2
- Kivy v2.1.0
- KivyMD 0.104.2, git-bc7d1f5, 2021-06-06
- 通过 venv 安装的虚拟环境
- 通过 pip3 安装的软件包
作为修复,我直接将数据添加到self.data_tables.row_data
。但由于内部索引错误,在几次插入和删除后崩溃了。
为什么 MDDataTables 对象没有文档中编写的 add_row() 方法?我是否安装了错误版本的kivyMD?
I am using Kivy MDDataTables in a Python3 App. During runtime i want to dynamically add and remove rows in the table. The documentation gives the following example
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.datatables import MDDataTable
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.button import MDRaisedButton
class Example(MDApp):
data_tables = None
def build(self):
layout = MDFloatLayout() # root layout
# Creating control buttons.
button_box = MDBoxLayout(
pos_hint={"center_x": 0.5},
adaptive_size=True,
padding="24dp",
spacing="24dp",
)
for button_text in ["Add row", "Remove row"]:
button_box.add_widget(
MDRaisedButton(
text=button_text, on_release=self.on_button_press
)
)
# Create a table.
self.data_tables = MDDataTable(
pos_hint={"center_y": 0.5, "center_x": 0.5},
size_hint=(0.9, 0.6),
use_pagination=False,
column_data=[
("No.", dp(30)),
("Column 1", dp(40)),
("Column 2", dp(40)),
("Column 3", dp(40)),
],
row_data=[("1", "1", "2", "3")],
)
# Adding a table and buttons to the toot layout.
layout.add_widget(self.data_tables)
layout.add_widget(button_box)
return layout
def on_button_press(self, instance_button: MDRaisedButton) -> None:
'''Called when a control button is clicked.'''
try:
{
"Add row": self.add_row,
"Remove row": self.remove_row,
}[instance_button.text]()
except KeyError:
pass
def add_row(self) -> None:
last_num_row = int(self.data_tables.row_data[-1][0])
self.data_tables.add_row((str(last_num_row + 1), "1", "2", "3"))
def remove_row(self) -> None:
if len(self.data_tables.row_data) > 1:
self.data_tables.remove_row(self.data_tables.row_data[-1])
Example().run()
On call of self.data_tables.add_row()
the program crashes with the following error:
AttributeError: 'MDDataTable' object has no attribute 'add_row'
I executed the code on two different systems with the same error:
Notebook:
- Windows 10
- Python 3.9.7
- Kivy v2.0.0
- KivyMD 0.104.2, git-bc7d1f5,2021-06-06
- Virtual Environment installed via Anaconda
- Packages installed via Anaconda
Raspberry Pi 400:
- Raspbian GNU/Linux 11
- Python 3.9.2
- Kivy v2.1.0
- KivyMD 0.104.2, git-bc7d1f5, 2021-06-06
- Virtual Environment installed via venv
- Packages installed via pip3
As fix I directly add the data to the self.data_tables.row_data
. But this crashed after a few inserts and deletes due to internal index errors.
Why does the MDDataTables object does not have the methode add_row() as written in the documentation? Do I have installed the wrong version of kivyMD?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论