import tkinter as tk
import os
import pyperclip
class WindowManager:
def __init__(self):
self.count_label = None
self.window_counter = 0
self.windows = []
def set_count_label(self, label):
self.count_label = label
def open_new_window(self):
self.window_counter += 1
new_window = tk.Toplevel()
new_window.title(f"Fenster {self.window_counter}")
new_window.geometry("300x200")
new_window.protocol("WM_DELETE_WINDOW", lambda: self.on_window_close(new_window))
self.set_window_position(new_window)
self.windows.append(new_window)
self.update_window_count_label()
def on_window_close(self, window):
self.windows.remove(window)
window.destroy()
self.update_window_count_label()
def update_window_count_label(self):
if self.count_label:
self.count_label.config(text=f"Anzahl der offenen Fenster: {len(self.windows)}")
def update_window_coordinates(self):
os.system('cls' if os.name == 'nt' else 'clear')
for index, window in enumerate(self.windows, start=1):
window.title(f"Fenster {index}")
print(f"{window.title()} - x: {window.winfo_x()}, y: {window.winfo_y()}, width: {window.winfo_width()}, height: {window.winfo_height()}")
print("----")
def set_window_position(self, window):
root.update()
root_x = root.winfo_rootx()
root_y = root.winfo_rooty()
root_width = root.winfo_width()
root_height = root.winfo_height()
window_width = window.winfo_width()
window_height = window.winfo_height()
x = root_x + (root_width // 2) - (window_width // 2)
y = root_y + (root_height // 2) - (window_height // 2)
window.geometry(f'+{x}+{y}')
def center_window(window, width, height):
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = (screen_width // 2) - (width // 2)
y = (screen_height // 2) - (height // 2)
window.geometry(f'{width}x{height}+{x}+{y}')
def copy_to_clipboard(window_manager):
console_output = ""
for index, window in enumerate(window_manager.windows, start=1):
console_output += f"Fenster {index} - x: {window.winfo_x()}, y: {window.winfo_y()}, width: {window.winfo_width()}, height: {window.winfo_height()}\n"
console_output += "----\n"
pyperclip.copy(console_output)
def load_coordinates(window_manager, coordinates_text):
coordinates = coordinates_text.get("1.0", "end-1c")
window_list = coordinates.split("\n")
for window in window_manager.windows:
window.destroy()
window_manager.windows.clear()
window_manager.window_counter = 0
for window_info in window_list:
if " - " in window_info:
window_data = window_info.split(" - ")[1]
window_data = window_data.replace(", ", "\n")
window_data = window_data.replace("x: ", "")
window_data = window_data.replace("y: ", "")
window_data = window_data.replace("width: ", "")
window_data = window_data.replace("height: ", "")
coordinates = window_data.split("\n")
if len(coordinates) >= 4:
x = int(coordinates[0])
y = int(coordinates[1])
width = int(coordinates[2])
height = int(coordinates[3])
window_manager.window_counter += 1
new_window = tk.Toplevel()
new_window.title(f"Fenster {window_manager.window_counter}")
new_window.geometry(f"{width}x{height}+{x}+{y}")
window_manager.windows.append(new_window)
window_manager.update_window_count_label()
def create_main_window():
global root
root = tk.Tk()
root.title("Hauptfenster")
window_width = 400
window_height = 300
center_window(root, window_width, window_height)
window_manager = WindowManager()
count_label = tk.Label(root, text="Anzahl der offenen Fenster: 0", font=("Arial", 12))
count_label.pack()
window_manager.set_count_label(count_label)
open_window_button = tk.Button(root, text="Neues Fenster öffnen", command=window_manager.open_new_window)
open_window_button.pack()
print_coordinates_button = tk.Button(root, text="Koordinaten anzeigen", command=window_manager.update_window_coordinates)
print_coordinates_button.pack()
copy_to_clipboard_button = tk.Button(root, text="Kopiere Konsole", command=lambda: copy_to_clipboard(window_manager))
copy_to_clipboard_button.pack()
coordinates_frame = tk.Frame(root)
coordinates_frame.pack()
coordinates_label = tk.Label(coordinates_frame, text="Fenster Koordinaten:")
coordinates_label.pack()
coordinates_text = tk.Text(coordinates_frame, height=5, width=50)
coordinates_text.pack()
load_coordinates_button = tk.Button(coordinates_frame, text="Laden", command=lambda: load_coordinates(window_manager, coordinates_text))
load_coordinates_button.pack()
root.mainloop()
if __name__ == "__main__":
create_main_window()