Numpy-Array aus TkInter-Entrys belegen
Hallo zusammen, langsam glaube ich, ich gebe Google einfach die falschen Stichwörter... und mein Problem kommt mir so dermaßen banal vor, dass eigentlich schon haufenweise Leute drüber gestolpert sein müssten: Ich würde gern eine Benutzeroberfläche bauen, aus der die eingegebenen Werte in ein numpy-Array übernommen und dann weiterverarbeitet werden. In ganz simpler Form also sowas: ---schnipp # Zwei Eingabewerte addieren und Summe ausgeben from numpy import * from Tkinter import * root = Tk() def add( ): sum = entry_val[0].get() + entry_val[1].get() print sum # Test, ob eine veraenderte Variable wieder in den entry uebernommen wird (das brauch ich aber eigentlich nicht) entry_val[1].set(sum) Label(root, text="Summand1").grid(row=0, sticky=E) Label(root, text="Summand2").grid(row=1, sticky=E) entry_val[0] = DoubleVar() entry_val[1] = DoubleVar() Entry(root, textvariable=entry_val[0]).grid(row=0, column=1) Entry(root, textvariable=entry_val[1]).grid(row=1, column=1) Button(root, text="Addieren", command=add).grid(row=2, sticky=W) root.mainloop() ---schnapp Wenn ich statt der Array-Elemente einfach Variablennamen einsetze, funktioniert das wunderbar, aber so leider nicht :-( Wenn das direkte Belegen von Array-Elementen prinzipiell nicht geht, könnte ich zumindest aus den Entrys Variablen belegen und die mit einer Schleife in das Array schreiben. Das fände ich aber... extrem unelegant. Kann mir jemand die Augen öffnen? Vielen Dank im Voraus Jan
Jan Harnisch wrote:
langsam glaube ich, ich gebe Google einfach die falschen Stichwörter... und mein Problem kommt mir so dermaßen banal vor, dass eigentlich schon haufenweise Leute drüber gestolpert sein müssten: Ich würde gern eine Benutzeroberfläche bauen, aus der die eingegebenen Werte in ein numpy-Array übernommen und dann weiterverarbeitet werden. In ganz simpler Form also sowas:
---schnipp # Zwei Eingabewerte addieren und Summe ausgeben from numpy import * from Tkinter import *
root = Tk()
def add( ): sum = entry_val[0].get() + entry_val[1].get() print sum # Test, ob eine veraenderte Variable wieder in den entry uebernommen wird (das brauch ich aber eigentlich nicht) entry_val[1].set(sum)
Label(root, text="Summand1").grid(row=0, sticky=E) Label(root, text="Summand2").grid(row=1, sticky=E)
entry_val = [DoubleVar(), DoubleVar()]
Entry(root, textvariable=entry_val[0]).grid(row=0, column=1) Entry(root, textvariable=entry_val[1]).grid(row=1, column=1)
Button(root, text="Addieren", command=add).grid(row=2, sticky=W)
root.mainloop() ---schnapp
Wenn ich statt der Array-Elemente einfach Variablennamen einsetze, funktioniert das wunderbar, aber so leider nicht :-( Wenn das direkte Belegen von Array-Elementen prinzipiell nicht geht, könnte ich zumindest aus den Entrys Variablen belegen und die mit einer Schleife in das Array schreiben. Das fände ich aber... extrem unelegant. Kann mir jemand die Augen öffnen?
Mir ist kein Trick bekannt, wie sich numpy-Arrayeinträge automatisch mit Tkinter-Entries verknüpfen lassen. Dazu müsste numpy.array das Observer- Pattern implementeren, was die Effizienz einer numbercrunching-Bibliothek wohl ruinieren würde. Was an einer Schleife so unelegant sein soll, kann ich aber nicht nachvollziehen. Unter Verwendung von numpy sähe add() so aus: def add(): a = array([v.get() for v in entry_val]) print a.sum() Um die Werte in das GUI zurückzuschreiben: for v, x in zip(entry_val, a): v.set(a) Beantwortet das deine Frage? Falls nicht, erläutere bitte nochmal genauer, wo numpy ins Spiel kommt.
participants (2)
-
Jan Harnisch
-
Peter Otten