1 | initial version |
Here are the steps to generate a grid by utilizing input functions within tkinter:
import tkinter as tk
root = tk.Tk()
grid_size = 20
row_colors = ["white", "lightgray"]
column_colors = ["white", "lightgray"]
cell_width = 25
cell_height = 25
def create_cell(frame, row, col):
color = row_colors[row % 2] if col % 2 == 0 else column_colors[col % 2]
cell = tk.Frame(frame, width=cell_width, height=cell_height, bg=color)
cell.grid(row=row, column=col)
def create_grid(frame):
for row in range(grid_size):
for col in range(grid_size):
create_cell(frame, row, col)
# Example input functions
def set_grid_size(size):
global grid_size
grid_size = size
main_frame.destroy()
main()
def set_colors(row_colors, column_colors):
global row_colors, column_colors
row_colors = row_colors
column_colors = column_colors
main_frame.destroy()
main()
def main():
global main_frame
main_frame = tk.Frame(root)
main_frame.pack()
create_grid(main_frame)
# Example button to change grid size
grid_size_btn = tk.Button(root, text="Change Grid Size", command=lambda: set_grid_size(25))
grid_size_btn.pack()
# Example button to change cell colors
colors_btn = tk.Button(root, text="Change Colors", command=lambda: set_colors(["white", "lightgray"], ["lightgray", "white"]))
colors_btn.pack()
root.mainloop()
if __name__ == "__main__":
main()
This will create a grid within a tkinter window that can be customized by the user through input functions.