Advertisement
ArafatGS

Rows & Columns generator

Jun 3rd, 2023 (edited)
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3. def generate_pattern(rows, cols):
  4.     pattern = ""
  5.     for i in range(rows):
  6.         for j in range(cols):
  7.             if (i + j) % 2 == 0:
  8.                 pattern += "*"
  9.             else:
  10.                 pattern += "-"
  11.         pattern += "\n"
  12.     return pattern
  13.  
  14. def show_pattern():
  15.     num_rows = int(rows_entry.get())
  16.     num_cols = int(cols_entry.get())
  17.  
  18.     pattern = generate_pattern(num_rows, num_cols)
  19.     pattern_text.configure(state='normal')
  20.     pattern_text.delete("1.0", tk.END)
  21.     pattern_text.insert(tk.END, pattern)
  22.     pattern_text.configure(state='disabled')
  23.  
  24. def toggle_dark_mode():
  25.     dark_mode.set(not dark_mode.get())
  26.     if dark_mode.get():
  27.         pattern_frame.config(bg="black")
  28.         pattern_text.config(bg="black", fg="white", insertbackground="white")
  29.         dark_mode_button.config(text="\u263E")  # Moon symbol
  30.     else:
  31.         pattern_frame.config(bg="white")
  32.         pattern_text.config(bg="white", fg="black", insertbackground="black")
  33.         dark_mode_button.config(text="\u2600")  # Sun symbol
  34.  
  35. window = tk.Tk()
  36. window.title("Pattern Generator")
  37.  
  38. dark_mode = tk.BooleanVar()
  39. dark_mode_button = tk.Button(window, text="\u2600", font=("Arial", 12), command=toggle_dark_mode)
  40. dark_mode_button.pack()
  41.  
  42. rows_label = tk.Label(window, text="Number of Rows:")
  43. rows_label.pack()
  44.  
  45. rows_entry = tk.Entry(window)
  46. rows_entry.pack()
  47.  
  48. cols_label = tk.Label(window, text="Number of Columns:")
  49. cols_label.pack()
  50.  
  51. cols_entry = tk.Entry(window)
  52. cols_entry.pack()
  53.  
  54. generate_button = tk.Button(window, text="Generate Pattern", command=show_pattern)
  55. generate_button.pack()
  56.  
  57. pattern_frame = tk.Frame(window, bg="white")
  58. pattern_frame.pack()
  59.  
  60. pattern_text = tk.Text(pattern_frame, state='disabled')
  61. pattern_text.pack()
  62.  
  63. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement