ich habe mir eine kleine GUI programmiert und habe folgendes Problem.
Wenn ich mein Programm starte, kann ich mir wie gewollt einen Ordner auswählen.
Nachdem sich dieses Fenster zur Auswahl geschlossen hat, ist mein Hauptfenster aktiv.
In diesem Hauptfenster kann ich auch auf die Buttons drücken, aber nicht in die Entrys.
In die kann ich erst klicken, wenn ich zu einem anderem Fenster und wieder zurück gewechselt bin.
Wenn ich aber den Ordner nicht auswählen lasse am Anfang, dann funktioniert es.
Code: Alles auswählen
import tkinter as tk
import tkinter.messagebox as tkmessagebox
import tkinter.filedialog as tkfiledialog
def ask_direction(path_list, mainwindow):
path = tkfiledialog.askdirectory(parent = mainwindow)
path_list.append(path)
return path_list
#method to close the mainwindow
def destroy_mainwindow(mainwindow):
mainwindow.destroy()
#method to delete the given conten of an entry
def delete_text(event):
try:
int(event.widget.get())
except ValueError:
event.widget.delete(0,tk.END)
event.widget.configure(foreground = "black")
event.widget.configure(background = "white")
def cancle(input_list, path_list, mainwindow):
del input_list[:]
del path_list[:]
destroy_mainwindow(mainwindow)
#method which checks that all entrys filled with integers
#in that case it store them in the inputlist
#otherwise show the user that there was a wrong or incomplete input
def check_content(entry_list, input_list, path_list, mainwindow):
error = False
i = 0
del input_list[:]
for entry in entry_list:
content = entry.get()
try:
int(content)
entry.configure(background = "white")
input_list.insert(i, content)
except ValueError:
if (error == False):
tkmessagebox.showerror("Falsche Eingabe","Es wurde kein Zahlenwert eingebenn")
entry.configure(background = "red")
error = True
i += 1
if (len(path_list) == 0 and error == False):
tkmessagebox.showerror("Kein Quellordner", "Es wurde kein Quellordner ausgewählt")
error = True
elif (len(input_list) == len(entry_list) and error == False):
tkmessagebox.showinfo("Vorgang Abgeschlossen", "Eingaben erfolgreich gespeichert")
destroy_mainwindow(mainwindow)
return input_list
#method to create the nessecary entrys with their functions
def create_entrys(on_frame_place, name_list, label_list, input_list, path_list, mainwindow):
entry_list = []
for name in name_list:
fra = tk.Frame(on_frame_place)
lab = tk.Label(fra, text = label_list[name_list.index(name)])
ent = tk.Entry(fra, width = 20, foreground = "grey", state = "normal")
ent.insert(tk.END, "Hier Wert eingeben")
ent.bind("<FocusIn>", delete_text)
ent.bind("<Return>", lambda event: check_content(entry_list, input_list, path_list, mainwindow))
fra.pack()
lab.pack(side = "top", padx = 3, pady = 3)
ent.pack(side = "bottom", padx = 3, pady = 3)
entry_list.append(ent)
return entry_list
def create_inputwindow(name_list, label_list, input_list, path_list):
#create and place mainwindow
root = tk.Tk()
root.title("Parameterabfrage")
root.geometry('450x225+600+300')
ask_direction(path_list, root) #Wenn die Zeile auskommentiert ist funktioniert es!!!!!!!!!
#frames to place all correctly
fbuttons = tk.Frame(root)
fentrys = tk.Frame(root)
#create entrys and labels
entry_for_parameter_list = create_entrys(fentrys, name_list, label_list, input_list, path_list, root)
#button to select startfolder
buttonchangdir = tk.Button(fbuttons, text = "Quellordner ändern", width = 20, command = lambda: ask_direction(path_list, root))
buttonchangdir.bind("<Return>", lambda event: ask_direction(path_list, root))
buttonchangdir.pack(side = "top", padx = 10, pady = 10)
#assume-button
buttonassume = tk.Button(fbuttons, text = "Übernehmen", width = 20, command = lambda:(check_content(entry_for_parameter_list, input_list, path_list, root)))
buttonassume.bind("<Return>", lambda event: check_content(entry_for_parameter_list, input_list, path_list, root))
buttonassume.pack(side = "top", padx = 10, pady = 10)
#cancle-button with function
buttoncancle = tk.Button(fbuttons, text ="Abbrechen", width= 20,command = lambda: cancle(input_list, path_list, root))
buttoncancle.bind("<Return>", lambda event: cancle(input_list, path_list, root))
buttoncancle.pack(side = "bottom", padx = 10, pady = 10)
#show buttonframe and entryframe
fentrys.pack(side = "left")
fbuttons.pack(side = "right")
#infinite loop
root.mainloop()
return input_list, path_list
input_parameter_list = []
startpath_list = []
#startpath = tkfiledialog.askdirectory()
#startpath_list.append(startpath)
#list of paramters whitch the user have to define
#names begin with an "e" becuase that are the names of the entrys
entry_name_list = ["efeeder", "espindelrpm", "etool", "edrillingdepth"]
#list of questions to interact with the user
question_list = ["Welchen Vorschub soll die Spindel haben?",
"Welche Drehzahl soll die Spidel haben?",
"Welche Werkzeugnummer soll verwendet werden?",
"Wie tief soll das Bohrloch werden? (Plattenstärke)"]
input_parameter_list, startpath_list = create_inputwindow(entry_name_list, question_list, input_parameter_list, startpath_list)