Skip to content

Instantly share code, notes, and snippets.

@MaxClerkwell
Created April 13, 2023 12:07
Show Gist options
  • Save MaxClerkwell/b6fe62a1475f989dbdf2164cf83a561f to your computer and use it in GitHub Desktop.
Save MaxClerkwell/b6fe62a1475f989dbdf2164cf83a561f to your computer and use it in GitHub Desktop.
import tkinter as tk
from tkinter import messagebox
import requests
# Keycloak-Konfiguration
keycloak_url = "https://your-keycloak-server/auth/realms/your-realm/protocol/openid-connect/token"
client_id = "your-client-id"
client_secret = "your-client-secret"
def get_keycloak_token(username, password):
data = {
"grant_type": "password",
"client_id": client_id,
"client_secret": client_secret,
"username": username,
"password": password
}
response = requests.post(keycloak_url, data=data)
if response.status_code == 200:
return response.json()["access_token"]
else:
return None
def get_status(username, token):
headers = {
"Authorization": f"Bearer {token}"
}
url = f"https://api.gruppe.ai/timetracking/status/?name={username}"
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()["status"]
else:
return None
def login():
username = username_entry.get()
password = password_entry.get()
token = get_keycloak_token(username, password)
if token:
root.destroy()
show_main_window(username, token)
else:
messagebox.showerror("Fehler", "Ungültige Anmeldeinformationen")
def show_main_window(username, token):
main_window = tk.Tk()
main_window.title("Hauptfenster")
welcome_label = tk.Label(main_window, text=f"Willkommen, {username}!")
welcome_label.pack()
status_label = tk.Label(main_window, text=f"Status: {get_status(username, token)}")
status_label.pack()
status_options = [
# Radio-Button Optionen
]
selected_status = tk.StringVar()
for option in status_options:
radio_button = tk.Radiobutton(main_window, text=option, variable=selected_status, value=option)
radio_button.pack()
send_button = tk.Button(main_window, text="Senden", command=open_confirmation_dialog)
send_button.pack()
main_window.mainloop()
def open_confirmation_dialog():
confirmation_dialog = tk.Toplevel()
confirmation_dialog.title("Bestätigung")
message_label = tk.Label(confirmation_dialog, text="Soll die Auswahl gesendet werden?")
message_label.pack()
project_related_var = tk.BooleanVar()
project_related_check = tk.Checkbutton(confirmation_dialog, text="Projektbezogen", variable=project_related_var)
project_related_check.pack()
comment_label = tk.Label(confirmation_dialog, text="Kommentar:")
comment_label.pack()
comment_entry = tk.Entry(confirmation_dialog)
comment_entry.pack()
confirm_button = tk.Button(confirmation_dialog, text="Bestätigen", command=validate_and_send_data)
confirm_button.pack()
def validate_and_send_data():
status = selected_status.get()
project_related = project_related_var.get()
comment = comment_entry.get()
mqtt_client = mqtt.Client()
try:
mqtt_client.connect("public.mqtt.gruppe.ai")
except Exception as e:
messagebox.showerror("Fehler", f"Verbindung zum MQTT-Broker fehlgeschlagen: {e}")
if project_related and not comment.strip():
messagebox.showerror("Fehler", "Bitte geben Sie einen Kommentar ein, wenn die Statusänderung projektbezogen ist.")
else:
data = {
"status": status,
"projektbezogen": project_related,
"kommentar": comment
}
mqtt_client.publish(f"internal/timetracker/{username}", json.dumps(data))
confirmation_dialog.destroy()
# Tkinter-Anwendung
root = tk.Tk()
root.title("Keycloak-Anmeldung")
username_label = tk.Label(root, text="Benutzername:")
username_label.grid(row=0, column=0)
username_entry = tk.Entry(root)
username_entry.grid(row=0, column=1)
password_label = tk.Label(root, text="Passwort:")
password_label.grid(row=1, column=0)
password_entry = tk.Entry(root, show="*")
password_entry.grid(row=1, column=1)
login_button = tk.Button(root, text="Anmelden", command=login)
login_button.grid(row=2, column=0, columnspan=2)
token = None
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment