- modify the following code so there is a " " between each 4 checkboxes : import time…
- User
- modify the following code so there is a "|" between each 4 checkboxes : import time import threading import tkinter as tk
state = False
def AND_Gate(a,b): return a and b def OR_Gate(a,b): return a or b def NOT_Gate(a): return not a def NAND_Gate(a,b): return not(a and b) def NOR_Gate(a,b): return not(a or b) def XOR_Gate(a,b): return (a or b) and not(a and b) def XNOR_Gate(a,b): return not((a or b) and not(a and b))
def clock(update_clock,speed = 1): cycle = 0 global state while True: state = False time.sleep(1 / speed / 2) update_clock(f"State : False , Cycle {cycle}") state = True time.sleep(1 / speed / 2) update_clock(f"State : True , Cycle {cycle}") cycle += 1
main_window = tk.Tk() main_window.title("Intel 4004") clock_label = tk.Label(main_window, text="") clock_label.pack(side="bottom") def update_clock(text): clock_label.config(text=text) checkbox_window_1 = tk.Frame(main_window) checkbox_window_1.pack(side="top") pins = [] for i in range(16): var = tk.BooleanVar(name=f"Pin {i}") checkbox = tk.Checkbutton(checkbox_window_1, variable=var) checkbox.pack(side="left") pins.append(var) clock_thread = threading.Thread(target=clock,args=(update_clock,),daemon=True) clock_thread.start() main_window.mainloop()
- Assistant
To introduce a vertical bar (
|) between every four checkboxes, you can add a label widget with the character between the groups as you iterate through creating the checkboxes. Here’s how you can modify your code:import time import threading import tkinter as tk state = False def AND_Gate(a, b): return a and b def OR_Gate(a, b): return a or b def NOT_Gate(a): return not a def NAND_Gate(a, b): return not (a and b) def NOR_Gate(a, b): return not (a or b) def XOR_Gate(a, b): return (a or b) and not (a and b) def XNOR_Gate(a, b): return not ((a or b) and not (a and b)) def clock(update_clock, speed=1): cycle = 0 global state while True: state = False time.sleep(1 / speed / 2) update_clock(f"State : False , Cycle {cycle}") state = True time.sleep(1 / speed / 2) update_clock(f"State : True , Cycle {cycle}") cycle += 1 main_window = tk.Tk() main_window.title("Intel 4004") clock_label = tk.Label(main_window, text="") clock_label.pack(side="bottom") def update_clock(text): clock_label.config(text=text) checkbox_window_1 = tk.Frame(main_window) checkbox_window_1.pack(side="top") pins = [] # Iterate over a range of 16 for i in range(16): var = tk.BooleanVar(name=f"Pin {i}") checkbox = tk.Checkbutton(checkbox_window_1, variable=var) checkbox.pack(side="left") pins.append(var) # After every 4 checkboxes, add a vertical bar if (i + 1) % 4 == 0 and i < 15: # Avoid adding a bar after the last group separator = tk.Label(checkbox_window_1, text="|") separator.pack(side="left") clock_thread = threading.Thread(target=clock, args=(update_clock,), daemon=True) clock_thread.start() main_window.mainloop()