因此,所包含的代码根据用户提供的输入保存了一个文件,为了更好,我决定添加一个"添加"按钮,该按钮将创建另外3个文本输入。现在我学会了如何使用id访问它们,但它只将输入保存在前3个文本框中,我尝试使用循环,因为我认为当你第二次使用它时,它可能会从第二个3个文本框中获取第二个输入,但它不起作用。抱歉,如果代码在某些方面效率低下,这是我第一次制作应用程序这是py文件
from datetime import date
from kivy.app import App
from kivy.factory import Factory
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
t=1
lista=[]
class MyGrid(GridLayout) :
id_1 = ObjectProperty(None)
id_2 = ObjectProperty(None)
id_3 = ObjectProperty(None)
id_4 = ObjectProperty(None)
id_10 = ObjectProperty(None)
def add(self):
self.ids.parts_text_inputs.add_widget(Factory.PartsTextInputs())
global t
t += 1
def perfundo (self) :
file = open('D:\agron\testing\' + self.id_1.text + '-' + str(date.today()) + '.txt', 'x')
global lista
global t
for i in range(0, t) :
lista.append(self.ids.parts.ids.txtinp1.text)
lista.append(self.ids.parts.ids.txtinp2.text)
lista.append(self.ids.parts.ids.txtinp3.text)
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+ 'n' + 'idk :' + str(lista))
file.close()
class HAZApp(App) :
def build (self) :
return MyGrid()
app = HAZApp()
app.run()
这是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
id:txtinp1
BoxLayout:
size_hint_y: None
height: self.minimum_height
TextInput:
size_hint_y: None
height: 48
id:txtinp2
TextInput:
size_hint_y: None
height: 48
id:txtinp3
<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: 'foto.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, 0
outline_width: 2
text: 'Auto Servis "Haziri"'
BoxLayout:
spacing:10
Label:
text:"Name"
font_size: 20
color:0.4,0.5,0.8,1
Label:
text:"Type of car"
font_size: 20
color:0.4,0.5,0.8,1
Label:
text:"Phone number"
font_size: 20
color:0.4,0.5,0.8,1
Label:
text:"Problem"
font_size: 20
color:0.4,0.5,0.8,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,0.5,0.8,1
BoxLayout:
Label:
text:'price for parts'
font_size: 20
color:0.4,0.5,0.8,1
Label:
text:'price for work'
font_size: 20
color:0.4,0.5,0.8,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
id:parts
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:''
由于parts
id只分配给第一个PartsTextInputs
(最初位于kv
中的那个(,因此您得到了相同的输出。因此,在同一个对象中多次循环只会重复相同的数据。您可以通过使用parts_text_inputs
:的id
循环BoxLayout
的子级来访问所有PartsTextInputs
def perfundo (self) :
file = open('D:\agron\testing\' + self.id_1.text + '-' + str(date.today()) + '.txt', 'x')
global lista
global t
# get the list of all PartsTextInputs
parts_prices = self.ids.parts_text_inputs.children[:]
# reverse the list to get them in the samee order that you see on the screen
parts_prices.reverse()
# loop through all the PartsTextInputs
for ti in parts_prices:
lista.append(ti.ids.txtinp1.text)
lista.append(ti.ids.txtinp2.text)
lista.append(ti.ids.txtinp3.text)
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+ 'n' + 'idk :' + str(lista))
file.close()