如何在列表中保存多个组合框中的数据,python



我的这部分代码有问题,我正在制作一个保存家用气瓶数据的系统,首先它会询问要存储多少个气瓶,然后用组合框选择气瓶的特性,这里是错误所在,控制台没有显示数据,这是代码:

from tkinter import *
from tkinter import Toplevel, getint, ttk
ventana = Tk()
def ejecucion():
can = cantidad.get() +1
ventana2 = Toplevel()
def guardar():
print(lista) #SE GUARDARAN LOS DATOS DE LAS LISTAS EN LA BASE DE DATOS 
def imprimir():
empresa.get()
tamano.get()
pico.get()

for cilindros in range(1, can ,1):
titulo = LabelFrame(ventana2, text= f"Cilindro {cilindros}")
titulo.pack()

empresa = ttk.Combobox(titulo, values=["Radelco", "PacoGas", "Comunal"])
empresa.bind("<<ComboboxSelect>>", imprimir)
empresa.pack()
tamano = ttk.Combobox(titulo, values=["Pequena", "Mediana", "Regular", "Grande"])
tamano.bind("<<ComboboxSelect>>", imprimir)
tamano.pack()
pico = ttk.Combobox(titulo, values=["Pequeno", "Mediano", "Grande"])
pico.bind("<<ComboboxSelect>>", imprimir)
pico.pack()
lista.append(imprimir)
Button(ventana2, text="Guardar", command=guardar).pack()

lista = []
cantidad = IntVar()
Label(ventana, text="cantidad cilindros").pack()
Entry(ventana, textvariable=cantidad).pack()
Button(ventana, text="listo", command=ejecucion).pack()
ventana.mainloop()

最后,这些获得的数据将存储在数据库中,提前感谢。

您的代码甚至无法工作,因为您使用了错误的缩进。下次输入正确的代码。


您将imprimir保留在listo上,但您应该保留列表[empresa, tamano, pico]
(甚至[cilindros ,empresa, tamano, pico](

您不需要函数imprimir(),因为当您单击按钮时,您在guardar()中所做的一切。

它应该使用for-循环来运行listo中的每个列表[cilindros ,empresa, tamano, pico],并使用.get()从该列表上的Comboboxes获取值。

后来你可以用open()write()close()把它写成文本文件,但我用模块csv把它写成CSV,你可以在Excel(或Python模块pandas(中打开它来进行其他计算。


import tkinter as tk   # PEP8: `import *` is not preferred
from tkinter import ttk
import csv
# --- functions ---   # PEP8: all functions before main code
def ejecucion():
can = cantidad.get() +1
ventana2 = tk.Toplevel()
def guardar():
data = []
# SE GUARDARAN LOS DATOS DE LAS LISTAS EN LA BASE DE DATOS 
for cilindros, empresa, tamano, pico in lista:
print('cilindros:', cilindros)
print('empresa:', empresa.get())
print('tamano :', tamano.get())
print('pico   :', pico.get())
print('---------------------------')
data.append([cilindros, empresa.get(), tamano.get(), pico.get()])

# ---

with open('output.csv', 'w') as fh:
cvs_writer = csv.writer(fh)
# write header (single row)
cvs_writer.writerow(["cilindros", "empresa", "tamano", "pico"])  # writerow (without char `s` at the end) to write one list (with single row).
# write data   (many rows)
cvs_writer.writerows(data)                                       # writerows (with char `s` at the end) to write list of lists (with many rows). 
for cilindros in range(1, can):
titulo = tk.LabelFrame(ventana2, text= f"Cilindro {cilindros}")
titulo.pack()
empresa = ttk.Combobox(titulo, values=["Radelco", "PacoGas", "Comunal"])
empresa.pack()
tamano = ttk.Combobox(titulo, values=["Pequena", "Mediana", "Regular", "Grande"])
tamano.pack()
pico = ttk.Combobox(titulo, values=["Pequeno", "Mediano", "Grande"])
pico.pack()
lista.append([cilindros, empresa, tamano, pico])
tk.Button(ventana2, text="Guardar", command=guardar).pack()
# --- main ---
lista = []
ventana = tk.Tk()
cantidad = tk.IntVar(ventana)
tk.Label(ventana, text="cantidad cilindros").pack()
tk.Entry(ventana, textvariable=cantidad).pack()
tk.Button(ventana, text="listo", command=ejecucion).pack()
ventana.mainloop()

PEP 8——Python代码的样式指南

相关内容

  • 没有找到相关文章

最新更新