I have coded my GUI and main program seperately. It is a scheduled clicker that clicks a list of coordinates when a specific time is reached. Can I and should I code them in the same file? If not, then how can I connect my main program to the GUI and get access to the values in the GUI from my main program? GUI code:
from tkinter import *
from db import Database
db = Database("coordinates.db")
def start():
print("Start")
def populate_list():
print("Populate")
app = Tk()
#Time to run
time_text = StringVar()
time_label = Label(app, text="Time", font=("bold", 14), pady=20, )
time_label.grid(row=0, column=0, sticky=W)
time_entry = Entry(app, textvariable=time_text)
time_entry.grid(row=0, column=1, padx=20)
#Interval
interval_text = StringVar()
interval_label = Label(app, text="Interval", font=("bold", 14))
interval_label.grid(row=0, column=3, sticky=W)
interval_entry = Entry(app, textvariable=interval_text)
interval_entry.grid(row=0, column=4)
#Coordinates
coordinates_list = Listbox(app, height=8, width=50)
coordinates_list.grid(row=3, column=0, columnspan=3, rowspan=6, pady=20, padx=20)
#Create Scrollbar
scrollbar = Scrollbar(app)
scrollbar.grid(row=3, column=3)
coordinates_list.configure(yscrollcommand=scrollbar.set)
scrollbar.configure(command=coordinates_list.yview)
#Buttons
add_btn = Button(app, text="Start", width=12, command=start)
add_btn.grid(row=2, column=0, pady=20)
app.title("Autoclicker")
app.geometry("700x350")
#Populate data
populate_list()
app.mainloop()
Main Program/Backend
import time as time_module
import threading
import pyautogui
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode
import datetime
import sched
delay = 1
button = Button.left
start_stop_key = KeyCode(char='s')
exit_key = KeyCode(char='e')
get_position_key = KeyCode(char="g")
reset_coordinates_key = KeyCode(char="r")
coordinates = []
scheduler = sched.scheduler(time_module.time, time_module.sleep)
t = time_module.strptime('2022-04-27 23:35:00', '%Y-%m-%d %H:%M:%S')
t = time_module.mktime(t)
class ClickMouse(threading.Thread):
def __init__(self, delay, button):
super(ClickMouse, self).__init__()
self.delay = delay
self.button = button
self.running = False
self.program_running = True
def start_clicking(self):
self.running = True
def stop_clicking(self):
self.running = False
def exit(self):
self.stop_clicking()
self.program_running = False
def run(self):
while self.program_running:
if self.running:
for coordinate in coordinates:
pyautogui.moveTo(coordinate["x"], coordinate["y"])
mouse.click(self.button)
time_module.sleep(self.delay)
self.running = False
mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()
def on_press(key):
if key == start_stop_key:
scheduler_e = scheduler.enterabs(t, 1, click_thread.start_clicking, ())
scheduler.run()
elif key == exit_key:
click_thread.exit()
listener.stop()
elif key == get_position_key:
x, y = pyautogui.position()
coordinates.append({"x" : x, "y" : y})
print(coordinates)
elif key == reset_coordinates_key:
coordinates.clear()
with Listener(on_press=on_press) as listener:
listener.join()