can you say why my code doesnt work ? import time import threading import tkinter as tk…
- User
can you say why my code doesnt work ? 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
def main(): window = tk.Tk() window.title("Intel 4004") window.geometry("400x300") clock_label = tk.Label(window, text="", font=("Arial", 14)) clock_label.pack() def update_clock(msg): clock_label.config(text=msg) clock_thread = threading.Thread(target=clock(update_clock),daemon=True) clock_thread.run() window.mainloop()
if name == "main": main()
- Assistant
To address the issues you are experiencing in your code, let's go through the issues and how to resolve them:
-
Thread Initialization:
-
In the line
clock_thread = threading.Thread(target=clock(update_clock),daemon=True), you're immediately calling theclockfunction instead of passing it as a target to the thread. This will cause theclockfunction to execute in the main thread before a new thread is created, which is not the intended behavior. -
You should pass the function object (without calling it) to the
targetparameter. Additionally, if you want to pass arguments to the target function, use theargskeyword like this:python
-
-