Python:如何动态添加行

编程入门 行业动态 更新时间:2024-10-28 13:29:05
本文介绍了Python:如何动态添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是python/Kivy的新手. 我有两个文件test.py和test.ky. 现在我正在使用两个静态行,其序列号为1和2. 谁能告诉我? 单击"+添加更多"按钮时如何添加动态行.现在第2行显示带有编号递增的静态行. 我想在行号1到10之间添加动态行.

I am new to python/Kivy. I have two files test.py and test.ky. Now I am using two static row with serial number 1 and 2. Can anyone tell me? How to add row dynamic when click on '+add more' button.Now 2 row shows which are static row with serial number increment. I want add row dynamic 1 to 10 with serial number increment.

import kivy from kivy.uix.screenmanager import Screen from kivy.app import App from kivy.lang import Builder from kivy.core.window import Window Window.size = (450, 525) class display(Screen): def add_more(self): print('test') class test(App): def build(self): self.root = Builder.load_file('test.kv') return self.root if __name__ == '__main__': test().run()

test.kv

display: BoxLayout: orientation: "vertical" padding : 20, 20 BoxLayout: orientation: "horizontal" Button: size_hint_x: .2 text: "+Add More" valign: 'bottom' on_press: root.add_more() BoxLayout: orientation: "horizontal" Label: size_hint_x: .2 text: "SN" valign: 'bottom' Label: size_hint_x: .8 text: "Value" valign: 'bottom' BoxLayout: orientation: "horizontal" spacing: 0, 5 Button: text: '1' size_hint_x: .2 TextInput: size_hint_x: .8 BoxLayout: orientation: "horizontal" spacing: 0, 5 Button: text: '2' size_hint_x: .2 TextInput: size_hint_x: .8 BoxLayout: orientation: "horizontal" padding : 10, 0 spacing: 10, 10 size_hint: .5, .7 pos_hint: {'x': .25, 'y':.25} Button: text: 'Ok' on_release: root.dismiss() Button: text: 'Cancel' on_release: root.dismiss()

有人可以帮我吗?

Can someone help me?

推荐答案

您可以为Row和Rows创建自定义类,然后使用添加行的方法. 我对您的示例做了一些修改.试试这个:

You can make a custom class for Row and Rows, then have a method adding rows. I modified your example a bit. Try this:

from kivy.uix.screenmanager import Screen from kivy.app import App from kivy.lang import Builder from kivy.core.window import Window from kivy.uix.boxlayout import BoxLayout from kivy.properties import StringProperty Window.size = (450, 525) class display(Screen): def add_more(self): self.ids.rows.add_row() class Row(BoxLayout): button_text = StringProperty("") class Rows(BoxLayout): orientation = "vertical" row_count = 0 def __init__(self, **kwargs): super(Rows, self).__init__(**kwargs) self.add_row() def add_row(self): self.row_count += 1 self.add_widget(Row(button_text=str(self.row_count))) class test(App): def build(self): self.root = Builder.load_string(KV) return self.root KV = """ <Row>: orientation: "horizontal" spacing: 0, 5 Button: text: root.button_text size_hint_x: .2 TextInput: size_hint_x: .8 display: BoxLayout: orientation: "vertical" padding : 20, 20 BoxLayout: orientation: "horizontal" Button: size_hint_x: .2 text: "+Add More" valign: 'bottom' on_press: root.add_more() BoxLayout: orientation: "horizontal" Label: size_hint_x: .2 text: "SN" valign: 'bottom' Label: size_hint_x: .8 text: "Value" valign: 'bottom' Rows: id: rows BoxLayout: orientation: "horizontal" padding : 10, 0 spacing: 10, 10 size_hint: .5, .7 pos_hint: {'x': .25, 'y':.25} Button: text: 'Ok' on_release: root.dismiss() Button: text: 'Cancel' on_release: root.dismiss() """ test().run()

更多推荐

Python:如何动态添加行

本文发布于:2023-11-05 07:11:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!