Hey there,
I am new to python. At the moment I am trying to program a GUI, that show me some user information from a table of a SQL Database with a loop. He also should add a entry field, where i can type in the amount of pieces for the user. After that i want some form entries where i can add a description, a date and a upload button. The upload button should open a function where i can send the amount of the entry fields and the belonging user_id to a new function with a loop that add the information in another sql table.
the problem is, that i dont know how i can catch all the entries from the loop, so that i can send the information to another function. I experimented with the append method - but i doesn't get it. Can anybody help? thank you
PROBLEM SOLVED - FOUND A CODE THAT WORKED - BUT NOT WHY (AT THE MOMENT) :-D
CODE WHICH WORKS:
I am new to python. At the moment I am trying to program a GUI, that show me some user information from a table of a SQL Database with a loop. He also should add a entry field, where i can type in the amount of pieces for the user. After that i want some form entries where i can add a description, a date and a upload button. The upload button should open a function where i can send the amount of the entry fields and the belonging user_id to a new function with a loop that add the information in another sql table.
the problem is, that i dont know how i can catch all the entries from the loop, so that i can send the information to another function. I experimented with the append method - but i doesn't get it. Can anybody help? thank you
PROBLEM SOLVED - FOUND A CODE THAT WORKED - BUT NOT WHY (AT THE MOMENT) :-D
CODE WHICH WORKS:
import tkinter as tk
import sqlite3
def insert_data(zahler_id, grund, betrag, preis, insert_date):
endresult=float(betrag)*float(preis)
conn = sqlite3.connect('data.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS SOLL (zahler_id TEXT, grund TEXT, betrag TEXT, datum INT)")
cursor.execute("INSERT INTO SOLL (zahler_id, grund, betrag, datum) VALUES (?, ?, ?, ?)", (zahler_id, grund, endresult, insert_date))
conn.commit()
conn.close()
def fetch_data():
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute("SELECT zahler_id, firstname, lastname FROM customer_data")
return cursor.fetchall()
def submit_values():
preis=preis_entry.get()
grund=grund_entry.get()
insert_date=datum_entry.get()
for entry in entry_fields:
zahler_id = entry[0]
betrag = entry[1].get()
insert_data(zahler_id, grund, betrag, preis, insert_date)
app = tk.Toplevel()
app.title("Einkauf")
data = fetch_data()
entry_fields = []
for zahler_id, vorname, nachname in data:
frame = tk.Frame(app)
frame.pack()
tk.Label(frame, text=f"Zahler-ID: {zahler_id}").pack(side=tk.LEFT)
tk.Label(frame, text=f"Name: {vorname}, Nachname: {nachname}", bg="white", font="Helvetica 13 bold").pack(side=tk.LEFT)
entry = tk.Entry(frame, width=5)
entry.pack(side=tk.LEFT)
entry_fields.append((zahler_id, entry))
preislabel=tk.Label(app, text="Kosten pro Ware")
preislabel.pack()
preis_entry=tk.Entry(app)
preis_entry.pack()
grundlabel=tk.Label(app, text="Beschreibung")
grundlabel.pack()
grund_entry=tk.Entry(app)
grund_entry.pack()
date_label = tk.Label(app, text="Buchungsdatum")
date_label.pack()
#Eine StringVar erstellen- Für Zahlen IntVar, für Kommazahlen DoubleVar, für Wahr oder falsch Booleanvar
datum_text = tk.StringVar()
#ein Text wird gesetzt
datum_text.set("DD.MM.YYYY")
datum_entry=tk.Entry(app, textvariable=datum_text)
datum_entry.pack()
upload_button = tk.Button(app, text="Ware eintragen", command=submit_values)
upload_button.pack()
app.mainloop()
