Hey, ich möchte feld1 bis feld7 so zusammenfassen, sodass ich das nicht alles einzeln eingeben muss. Dazu finde ich nur leider nichts im Internet.
import tkinter as tk from tkinter import *
modul = tk.Tk()
modul.geometry("900x900")
modul.title("Bunt3x3")
modul.resizable(width=False, height=False)
r = "red"
y = "yellow"
g = "green"
b = "blue"
def change():
if feld1["bg"] == r:
feld1.config(bg=g)
feld3.config(bg=g)
feld5.config(bg=g)
feld7.config(bg=g)
feld2.config(bg=b)
feld4.config(bg=b)
feld6.config(bg=b)
feld8.config(bg=b)
feldlabel.config(text="Rot & gelb")
elif feld1["bg"] == g:
feld1.config(bg=r)
feld3.config(bg=r)
feld5.config(bg=r)
feld7.config(bg=r)
feld2.config(bg=y)
feld4.config(bg=y)
feld6.config(bg=y)
feld8.config(bg=y)
feldlabel.config(text="Gruen & blau")
feld1 = tk.Frame(bg=r, width=300, height=300)
feld1.grid(column=1, row=1)
feld3 = tk.Frame(bg=r, width=300, height=300)
feld3.grid(column=3, row=1)
feld5 = tk.Frame(bg=r, width=300, height=300)
feld5.grid(column=1, row=3)
feld7 = tk.Frame(bg=r, width=300, height=300)
feld7.grid(column=3, row=3)
feld2 = tk.Frame(bg=y, width=300, height=300)
feld2.grid(column=2, row=1)
feld4 = tk.Frame(bg=y, width=300, height=300)
feld4.grid(column=1, row=2)
feld6 = tk.Frame(bg=y, width=300, height=300)
feld6.grid(column=3, row=2)
feld8 = tk.Frame(bg=y, width=300, height=300)
feld8.grid(column=2, row=3)
feld = tk.Frame(bg="white", width=300, height=300).grid(column=2, row=2)
feldlabel = tk.Label(text="Gruen & blau", bg="white", fg="black", font=("Arial", 15, "bold"))
feldlabel.grid(column=2, row=2)
feldbutton = tk.Button(text="Farbe aendern!", font=("Arial", 15, "bold"), bg="white", fg="black", command=change).place(x=370, y=470)
modul.mainloop()