ich möchte eine Tastatur automatisch generieren lassen.
dazu sollen Buttons erzeugt und anhand eines Dictionaries konfiguriert werden.
das ganze soll durch eine forSchleife passieren.
hier mein code:
Code: Alles auswählen
from tkinter import Tk, Button
root = Tk()
BUTTONDICT = ({"0":{"command":print("0"), "text":"0"},
"1":{"command":print("1"), "text":"1"},
"2":{"command":print("2"), "text":"2"},
"3":{"command":print("3"), "text":"3"},
"4":{"command":print("4"), "text":"4"},
"5":{"command":print("5"), "text":"5"},
"6":{"command":print("6"), "text":"6"},
"7":{"command":print("7"), "text":"7"},
"8":{"command":print("8"), "text":"8"},
"9":{"command":print("9"), "text":"9"},
"komma":{"command":print(","), "text":","},
"gleich":{"command":print("="), "text":"="},
"plus":{"command":print("+"), "text":"+"},
"minus":{"command":print("-"), "text":"-"},
"mal":{"command":print("mal"), "text":"\u22C5"},
"durch":{"command":print("durch"), "text":":"},
"hoch":{"command":print("hoch"), "text":"^"},
"wurzel":{"command":print("sqrt"), "text":"\u221A"},
"sinus":{"command":print("sin"), "text":"sin( )"},
"cosinus":{"command":print("cos"), "text":"cos( )"},
"tangens":{"command":print("tan"), "text":"tan( )"},
"logarithmus":{"command":print("log"), "text":"log( )"},
"EXP":{"command":print("EXP"), "text":"\u22C5"+"10^( )"},
"EXE":{"command":print("EXE"), "text":"EXE"} })
buttonlist = ["7","4","1","komma","8","5","2","0","9","6","3","EXP","mal","durch","plus","gleich","hoch","wurzel","minus","EXE","logaritmus","sinus", "cosinus","tangens"]
co=ro=0
for button in buttonlist:
b = Button(root, text=str(BUTTONDICT[button["text"]]), command=BUTTONDICT[button["command"]])
b.grid(collumn=co, row=ro)
ro += 1
if ro >= 4:
ro=0
co += 1
root.mainloop()
Mein Problem: es erschein ein Error "TypeError: string indices must be integers". ich vermute, dass das irgendwo ein problem mit der Variable "button" gibt, die ja durch die Liste geht, alsoein element der liste nach dem anderen symbolisiert.
habt ihr eine idee, wie ich das anders schreiben kann oder den Fehler beheben?
danke schonmal im vorraus!