0

I'm trying to modify a global variable with my function, called "submit." The function is supposed to change the variables "start_stop_key" and "delay." I'm using "tkinter" as "tk" and the pynput library. My code is below:

# four variables are created to 
# control the auto-clicker
# delay (in seconds)
delay = 0.001
# Button.right, Button.left, Button.middle for their respective mouse buttons
button = Button.right
start_stop_key = KeyCode(char=']')
stop_key = KeyCode(char='[')

def submit():
    delay = delay_entry.get()
    start_stop_key = start_stop_key_entry.get()

root = tk.Tk()
root.title("Buggie Autoclicker")

canvas = tk.Canvas(root, height=600, width=470)
canvas.pack()

delay_label = tk.Label(root, text="Time between clicks(in seconds):", font=("Arial", 14))
delay_label.pack(pady=20)
delay_entry = tk.Entry(root)
delay_entry.pack(pady=10)

start_stop_key_label = tk.Label(root, text="Key to toggle the autoclicker on and off:", font=("Arial", 14))
start_stop_key_label.pack(pady=20)
start_stop_key_entry = tk.Entry(root)
start_stop_key_entry.pack(pady=10)

submitbutton = tk.Button(root, text="Submit Values", command=submit())
submitbutton.pack(pady=20)

root.mainloop()
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • You need `global delay` and `global start_stop_key` in the function. – Barmar Jul 02 '21 at 23:56
  • A note for future use -- the "snippet" button is only for HTML/JavaScript/CSS -- it provides a way to run such samples directly in the browser, but that doesn't work for Python; just use the `{}` button to format code samples in non-HTML languages. – Charles Duffy Jul 03 '21 at 00:00

1 Answers1

0

Use the global keyword to modify global variables.

In your case:

def submit():
    global delay
    global start_stop_key
    delay = delay_entry.get()
    start_stop_key = start_stop_key_entry.get()
Kingsley Zhong
  • 358
  • 2
  • 9