Ich verwendet Python 3.8 (Windows 10).
Zwei Fragen:
- Wie bekomme ich die initialen x, y, width, height Daten?
- Warum werden beim Resize x und y auf 0 gesetzt? Wenn ich horizontal resize wird y auf 0 gesetzt; wenn ich vertikal resize wird x und y auf 0 gesetzt.
Code: Alles auswählen
import tkinter as tk
variables = {}
def create_content(root):
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
main_window = tk.Frame(root)
main_window["background"] = "green"
main_window.grid_rowconfigure(0, weight=1)
main_window.grid_columnconfigure(0, minsize=100)
main_window.grid_columnconfigure(1, weight=1)
main_window.grid_columnconfigure(2, minsize=100)
sidebar_left = tk.Frame(main_window)
sidebar_left["background"] = "yellow"
sidebar_left.grid(row=0, column=0, sticky=tk.N+tk.S+tk.W+tk.E)
sidebar_left.grid_columnconfigure(1, weight=1)
sidebar_right = tk.Frame(main_window)
sidebar_right["background"] = "blue"
sidebar_right.grid(row=0, column=2, sticky=tk.N+tk.S+tk.W+tk.E)
variables = {}
for row, (text, variable) in enumerate([
("X", tk.IntVar()), ("Y", tk.IntVar()), ("Width", tk.IntVar()), ("Height", tk.IntVar())]):
label = tk.Label(sidebar_left, text=text, background="orange", relief=tk.RAISED, anchor=tk.W)
label.grid(row=row, column=0, sticky=tk.W+tk.E, ipadx=5)
value = tk.Label(sidebar_left, background="cyan")
value["textvariable"] = variable
value.grid(row=row, column=1, sticky=tk.W+tk.E)
variables[text.lower()] = variable
main_window.grid(sticky=tk.N+tk.S+tk.W+tk.E)
return variables
def on_resize(event):
variables["x"].set(event.x)
variables["y"].set(event.y)
variables["width"].set(event.width)
variables["height"].set(event.height)
root = tk.Tk()
root.title("Demo Tkinter Grid Application")
root.geometry("640x480+50+50")
root.attributes("-alpha", 0.8)
variables.update(create_content(root))
root.bind("<Configure>", on_resize)
root.mainloop()