0

verán soy nuevo en programación y estoy haciendo un pequeño código para registrar datos y generar un TXT con estos. Mi problema es cuando un usuario no contesta un cuadro, ya que en el txt se muestra vacío el campo que no llenó. Por Ejemplo:

Si un usuario contesta "Edad" y "Peso" y no contesta "Altura" espero que en el TXT se vea asi:

Edad: 25

Peso: 175

y no así:

Edad: 25

Peso: 175

Altura:

No puedo hacer obligatorio el llenado de los cuadros porque esa no es la idea, Aquí abajo adjunto mi código. Espero haberme explicado bien y gracias !

from tkinter import *
import tkinter as tk
from tkinter import ttk

root = Tk()
root.title('A. APP')
root.geometry("290x290")
root.config(bg="gold3")

def Datos():
  Entry1_info = Entry1.get()
  Entry2_info = Entry2.get()
  Entry3_info = Entry3.get()
  
  file = open("Datos1.txt", "a") 
  file.write("Edad: "+ Entry1_info)
  file.write("\n")
  file.write("Peso: "+Entry2_info)
  file.write("\n")
  file.write("Altura: "+Entry3_info)
  file.close()
  
Entry1 = StringVar()
Entry2 = StringVar()
Entry3 = StringVar()


Frame1 = Frame(root, bd=2, padx=10, pady=3)

Label(Frame1, text="Edad: ", font=('Times', 14)).grid(row=1, column=0, sticky=W, pady=10)
Label(Frame1, text="Peso en KG: ", font=('Times', 14)).grid(row=2, column=0,sticky=W, pady=10)
Label(Frame1, text="Altura en CM: ", font=('Times', 14)).grid(row=3, column=0, sticky=W, pady=10)

Entry11 = Entry(Frame1,textvariable = Entry1, width=5).grid(row=1, column=1, sticky=W, pady=10)
Entry22 = Entry(Frame1,textvariable = Entry2, width=5).grid(row=2, column=1, sticky=W, pady=10)
Entry33 = Entry(Frame1,textvariable = Entry3, width=5).grid(row=3, column=1, sticky=W, pady=10)

Boton1 = Button(Frame1, text="Generar TXT", width=10, command= Datos).grid(row=4, column=1, sticky=W, pady=10)

Frame1.place(x=20, y=40)

root.mainloop()

1 respuesta 1

0

Puedes hacerlo validando si un entry está vacío mediante su longitud de la siguiente manera:

def Datos():
  Entry1_info = Entry1.get()
  Entry2_info = Entry2.get()
  Entry3_info = Entry3.get()

 #validas si los entry están vacíos, si no están vacíos les añades el prefijo deseado
  if len(Entry1_info) > 0:
    Entry1_info = "Edad: "+ Entry1_info;

  if len(Entry2_info) > 0:
    Entry2_info = "Peso: "+ Entry2_info;

  if len(Entry3_info) > 0:
    Entry3_info = "Altura:: "+ Entry3_info;
    
  file = open("Datos1.txt", "a")

  #si los entry no estaban vacíos los escribes en el fichero
  if(len(Entry1_info)>0):
    file.write(Entry1_info)
    file.write("\n")
  if(len(Entry2_info)>0):
    file.write(Entry2_info)
    file.write("\n")
  if(len(Entry3_info)>0):
    file.write(Entry3_info)
    file.write("\n")

  file.close()
1
  • Vaya no creí que fuera tan sencillo! Muchas gracias hermano me has ayudado a pasar mi semestre!
    – Siro27
    el 24 jun. 2021 a las 17:04

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.