mein Problem ist, dass ich nicht weiß, wie man den wert, den der Gepresste Button hat (also der Text, der darauf steht) an das Eingabefeld (tk.Entry) übermittelt und in dieses eingefügt werden kann
hier mein code
Code: Alles auswählen
import tkinter as tk
from tkinter import ttk
class Hauptfenster(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
ttk.Button(self, text="Tastatur anzeigen", command=lambda: Tastatur(self)).pack()
self.text = tk.StringVar(value="")
self.text_in = ttk.Entry(self, textvariable=self.text)
self.text_in.pack()
class Tastatur(tk.Toplevel):
OPENED = False
NAME = "Tastatur"
NUM = ({"7":{"text":"7", "info":"7", "width":1},
"8":{"text":"8", "info":"1", "width":1},
"9":{"text":"9", "info":"2", "width":1},
"hoch":{"text":"^", "info":"hoch", "width":1},
"wurzel":{"text":"\u221A", "info":"wurzel", "width":1},
"4":{"text":"4", "info":"3", "width":1},
"5":{"text":"5", "info":"4", "width":1},
"6":{"text":"6", "info":"5", "width":1},
"mal":{"text":"\u22C5", "info":"mal", "width":1},
"durch":{"text":":", "info":"durch", "width":1},
"1":{"text":"1", "info":"6", "width":1},
"2":{"text":"2", "info":"7", "width":1},
"3":{"text":"3", "info":"8", "width":1},
"plus":{"text":"+", "info":"plus", "width":1},
"minus":{"text":"-", "info":"minus", "width":1},
"komma":{"text":",", "info":", ;", "width":1},
"0":{"text":"0", "info":"0", "width":1},
"gleich":{"text":"=", "info":"gleich", "width":1},
"EXE":{"text":"EXE", "info":"führt die Eingabe aus", "width":2},})
CHAR = ({"A":{"text":"A", "info":"a, A", "width":1},
"B":{"text":"B", "info":"b, B", "width":1},
"C":{"text":"C", "info":"c, C", "width":1},
"D":{"text":"D", "info":"d, D", "width":1},
"E":{"text":"E", "info":"e, E", "width":1},
"F":{"text":"F", "info":"f, F", "width":1},
"G":{"text":"G", "info":"g, G", "width":1},
"H":{"text":"H", "info":"h, H", "width":1},
"I":{"text":"I", "info":"i, I", "width":1},
"J":{"text":"J", "info":"j, J", "width":1},
"K":{"text":"K", "info":"k, K", "width":1},
"L":{"text":"L", "info":"l, L", "width":1},
"M":{"text":"M", "info":"m, M", "width":1},
"N":{"text":"N", "info":"n, N", "width":1},
"O":{"text":"O", "info":"o, O", "width":1},
"P":{"text":"P", "info":"p, P", "width":1},
"Q":{"text":"Q", "info":"q, Q", "width":1},
"R":{"text":"R", "info":"r, R", "width":1},
"S":{"text":"S", "info":"s, S", "width":1},
"T":{"text":"T", "info":"t, T", "width":1},
"U":{"text":"U", "info":"u, U", "width":1},
"V":{"text":"V", "info":"v, V", "width":1},
"W":{"text":"W", "info":"w, W", "width":1},
"X":{ "text":"X", "info":"x, X", "width":1},
"Y":{"text":"Y", "info":"y, Y", "width":1},
"Z":{"text":"Z", "info":"z, Z", "width":1}})
def __init__(self, master):
if not Tastatur.OPENED:
Tastatur.OPENED = True
print("Tastatur wurde geöffnet!")
tk.Toplevel.__init__(self, master)
self.title(self.NAME)
self.protocol("WM_DELETE_WINDOW", self.close)
self.tast_nb = ttk.Notebook(self)
self.tast_nb.pack()
self.num_tab = ttk.Frame(self.tast_nb)
self.createPad(self.num_tab, Tastatur.NUM, 5)
self.tast_nb.add(self.num_tab, text="123")
self.char_tab = ttk.Frame(self.tast_nb)
self.createPad(self.char_tab, Tastatur.CHAR, 7)
self.tast_nb.add(self.char_tab, text="ABC")
def createPad(self, master, pad:dict, max_col):
self.co_count = 0
self.ro = 1
for button in pad.keys():
self.b = ttk.Button(master, width=6*pad[button]["width"], text=pad[button]["text"], command=self.bclicked(button))
if self.co_count >= max_col:
self.ro = self.ro + 1
self.co_count = 0
self.b.grid(row=self.ro, columnspan=pad[button]["width"], column=self.co_count)
self.co_count = self.co_count+pad[button]["width"]
def bclicked(self, button:str):
### ist der ansatz einer Methode, die als command des jeweiligen Buttons gebunden wird richtig, oder gibt es da bessere Möglichkeiten?
### wie kann ich den Text des Buttons (also der inhalt des button parameters) in das Entry des Hauptfensters einfügen?
### ist es möglich,dass das Hauptfenster nicht jedesmal den Fokus verliert, wenn ich die Tastatur aktiviere (also entweder öffne oder wieder mit der mas reinklicke) und umgekehrtauch?
### ---> also dass beide fenster permanent fokussiert sind?
pass
def close(self):
Tastatur.OPENED = False
self.destroy()
print("Tastatur wurde geschlossen!")
root = Hauptfenster()
root.mainloop()
Vielen dank für eure Hilfe schonmal!!!