我正在尝试使添加按钮在kivy中添加另外3个textinput框

如何解决我正在尝试使添加按钮在kivy中添加另外3个textinput框

对不起,如果某些部分希望使用我的语言,那没关系 当我按下“添加”按钮添加另外3个文本输入(如“添加”按钮上方的那些)时,我想保存输入到这些文本输入框上的输入,但是我想我自己也可以。 我搜索了,但是找不到有用的东西。这也是我第一次在此级别创建某些东西,如果它在某些方面效率低下,请对不起。 这是py文件

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty


class MyGrid(GridLayout) :
    id_1 = ObjectProperty(None)
    id_2 = ObjectProperty(None)
    id_3 = ObjectProperty(None)
    id_4 = ObjectProperty(None)
    id_10 =ObjectProperty(None)

    def perfundo (self) :
        file = open('F:\\F\\F\\' + self.id_1.text + '-' + str(date.today()) + '.txt','x')
        file.write('Emri dhe mbiemri :' + self.id_1.text + '\n' + 'Lloji i vetures :' + self.id_2.text + '\n' + 'Nr. i telefonit :' + self.id_3.text + '\n' + 'Problemi :' + self.id_4.text)
        file.close()

    def add(self):
       self.id_10.text

class HAZApp(App) :
    def build (self) :
        return MyGrid()


app = HAZApp()
app.run()

这是kv文件

<MyGrid>
id: HAZ
id_1: emridhembiemri
id_2:llojiivetures
id_3:numriitelefonit
id_4:problemi
id_10:add
rows: 10
padding: 10
spacing: 10
canvas.before:
    Rectangle:
        pos: self.pos
        size: self.size
        source: 'photo.jpg'

BoxLayout:
    canvas.before:
        Color:
            rgba: 0.4,0.5,0.8,1
        Rectangle:
            size: self.size
            pos: self.pos
    Label:
        font_size: '40sp'
        outline_color: 0,0
        outline_width: 2
        text: 'Auto Servis "Haziri"'

BoxLayout:
    spacing:10
    Label:
        text:"Name"
        font_size: 20
        color:0.4,1
    Label:
        text:"Type of car"
        font_size: 20
        color:0.4,1
    Label:
        text:"Phone number"
        font_size: 20
        color:0.4,1
    Label:
        text:"Problem"
        font_size: 20
        color:0.4,1

BoxLayout:
    spacing:20
    TextInput:
        font_size: 20
        id:emridhembiemri
    TextInput:
        font_size: 20
        id:llojiivetures
    TextInput:
        font_size: 20
        id:numriitelefonit
    TextInput:
        font_size: 20
        id:problemi

BoxLayout:
    spacing: 20
    column:2
    Label:
        text:'Parts'
        font_size: 20
        color:0.4,1
    BoxLayout:
        Label:
            text:'price for parts'
            font_size: 20
            color:0.4,1
        Label:
            text:'price for work'
            font_size: 20
            color:0.4,1


BoxLayout:
    spacing: 20
    column:2
    TextInput:
    BoxLayout:
        TextInput:
        TextInput:

BoxLayout:
    Button:
        id:add
        text:'Add'
        on_press:root.add()

BoxLayout:
    column:4
    Label:
        text:''
    Label:
        text:''
    Label:
        text:''
    Label:
        text:'Total'


BoxLayout:
    column:4
    Label:
        text:''
    Label:
        text:''
    Label:
        text:''
    Button:
        text:'total'

BoxLayout:
    Label:
        text:''
    Button:
        text:'Finish'
        font_size: 20
        on_press:root.perfundo()
        on_press: app.stop()
    Label:
        text:''

解决方法

我建议使用ScrollView来包含TextInputs。您的add()方法可以是:

def add(self):
    # Add a new PartsTextInputs inside the ScrollView
    self.ids.parts_text_inputs.add_widget(Factory.PartsTextInputs())

然后在PartsTextInputs中为kv添加一个规则,并为其中包含一个ScrollView。修改后的kv如下所示:

<PartsTextInputs@BoxLayout>:  # this is a rule for building the parts TextInputs
    size_hint_y: None
    height: self.minimum_height
    spacing: 20
    TextInput:
        size_hint_y: None
        height: 48
    BoxLayout:
        size_hint_y: None
        height: self.minimum_height
        TextInput:
            size_hint_y: None
            height: 48
        TextInput:
            size_hint_y: None
            height: 48
<MyGrid>
    id: HAZ
    id_1: emridhembiemri
    id_2:llojiivetures
    id_3:numriitelefonit
    id_4:problemi
    id_10:add
    # rows: 10
    cols: 1
    padding: 10
    spacing: 10
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'photo.jpg'
    
    BoxLayout:
        canvas.before:
            Color:
                rgba: 0.4,0.5,0.8,1
            Rectangle:
                size: self.size
                pos: self.pos
        Label:
            font_size: '40sp'
            outline_color: 0,0
            outline_width: 2
            text: 'Auto Servis "Haziri"'
    
    BoxLayout:
        spacing:10
        Label:
            text:"Name"
            font_size: 20
            color:0.4,1
        Label:
            text:"Type of car"
            font_size: 20
            color:0.4,1
        Label:
            text:"Phone number"
            font_size: 20
            color:0.4,1
        Label:
            text:"Problem"
            font_size: 20
            color:0.4,1
    
    BoxLayout:
        spacing:20
        TextInput:
            font_size: 20
            id:emridhembiemri
        TextInput:
            font_size: 20
            id:llojiivetures
        TextInput:
            font_size: 20
            id:numriitelefonit
        TextInput:
            font_size: 20
            id:problemi
    
    BoxLayout:
        spacing: 20
        column:2
        Label:
            text:'Parts'
            font_size: 20
            color:0.4,1
        BoxLayout:
            Label:
                text:'price for parts'
                font_size: 20
                color:0.4,1
            Label:
                text:'price for work'
                font_size: 20
                color:0.4,1
                
    ScrollView:
        size_hint_y: 3  # Since this is part of a GridLayout space is assigned by size_hint ratios
         
        BoxLayout:
            id: parts_text_inputs  # this will contain all the parts TextInputs
            orientation: 'vertical'
            size_hint_y: None
            height: self.minimum_height
            
            PartsTextInputs:   # this is the first of the parts TextInputs
    
    BoxLayout:
        Button:
            id:add
            text:'Add'
            on_press:root.add()
    
    BoxLayout:
        column:4
        Label:
            text:''
        Label:
            text:''
        Label:
            text:''
        Label:
            text:'Total'
    
    
    BoxLayout:
        column:4
        Label:
            text:''
        Label:
            text:''
        Label:
            text:''
        Button:
            text:'total'
    
    BoxLayout:
        Label:
            text:''
        Button:
            text:'Finish'
            font_size: 20
            on_press:root.perfundo()
            on_press: app.stop()
        Label:
            text:''

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?