Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_4x4_ButtonLayout_demo.py
- import tkinter as tk
- def on_enter(event):
- event.widget.config(bg="red")
- event.widget.config(fg="white")
- def on_leave(event):
- widget = event.widget
- if button_colors[widget] == 'green':
- widget.config(bg="green")
- widget.config(fg="white")
- else:
- widget.config(bg="yellow")
- widget.config(fg="gray")
- def on_click(event):
- widget = event.widget
- if button_colors[widget] == 'green':
- widget.config(bg="yellow")
- widget.config(fg="gray")
- button_colors[widget] = 'yellow'
- else:
- widget.config(bg="green")
- widget.config(fg="white")
- button_colors[widget] = 'green'
- root = tk.Tk()
- root.geometry("400x400+10+10")
- root.title("# tk_4x4_ButtonLayout_demo.py")
- button_colors = {} # Dictionary to store button colors
- for i in range(4):
- root.grid_rowconfigure(i, weight=1)
- root.grid_columnconfigure(i, weight=1)
- for j in range(4):
- button = tk.Button(root, text=f"{i*4+j+1}", font=("Arial", 50), fg="gray", bg="yellow")
- button.grid(row=i, column=j, sticky="nsew")
- button.bind("<Enter>", on_enter)
- button.bind("<Leave>", on_leave)
- button.bind("<Button-1>", on_click)
- button_colors[button] = 'yellow' # Initial color
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement