Tkinter starting window before the rest of the programming. (Python 3.9)

Asked

Viewed 22 times

-1

Hello, I come to ask for the help of someone more experienced in this matter than myself. I’m trying to use Tkinter and Pyautogui to automate a dull task of my everyday life. My goal is to create a looping, it needs to run without the Tkinter window in the first loop, from the second on, open the window, because the button I put has to "de-pause" the looping and continue repeating until I stop the programming. I’ve tried using for in range, but to no avail..

from tkinter import *
import pyautogui
import time
from time import sleep

def sd():
    time.sleep(3)
    pyautogui.click(69, 90, duration= 1)
    time.sleep(4.5)
    pyautogui.click(116, 86, duration= 1)
    time.sleep(12)
    pyautogui.click(394, 88, duration= 1)
    pyautogui.click(101, 575, duration= 1)
    pyautogui.click(153, 419, duration= 1)
    pyautogui.click(213, 578, duration= 1)
    pyautogui.click(714, 571, duration= 1)

janela = Tk()
bt = Button(janela, width=20, text="Próximo CTE", command=sd)
bt.place(x=50, y=50)
janela.geometry("250x100+200+200")
janela.mainloop()

1 answer

0


To run the looping before opening the tkinter, just call the function sd() before line 18 > janela = Tk():

sd()  # Chama a função sd
janela = Tk()
bt = Button(janela, width=20, text="Próximo CTE", command=sd)

To make the function looping until some command stops it, import the library Keyboard with pip install keyboard, then create the function loop() that will perform the function sd() until you hold the key ESC pressed:

def loop():
    while True:
        sd()
        if keyboard.is_pressed('ESC'):
            break

Change the method command of the button of sd for loop:

bt = Button(janela, width=20, text="Próximo CTE", command=**loop**)

Click on the button on the tkinter to start the looping again.


Upshot:

from tkinter import *
import pyautogui
import time
from time import sleep
import keyboard


def sd():
    time.sleep(3)
    pyautogui.click(69, 90, duration= 1)
    time.sleep(4.5)
    pyautogui.click(116, 86, duration= 1)
    time.sleep(12)
    pyautogui.click(394, 88, duration= 1)
    pyautogui.click(101, 575, duration= 1)
    pyautogui.click(153, 419, duration= 1)
    pyautogui.click(213, 578, duration= 1)
    pyautogui.click(714, 571, duration= 1)

def loop():
   """Chama a função sd() em looping ate que a tecla 'ESC' se mantenha pressionada"""
    while True:
        sd()
        if keyboard.is_pressed('ESC'):
            break
    

sd()  # Chama a função sd
janela = Tk()
bt = Button(janela, width=20, text="Próximo CTE", command=loop)
bt.place(x=50, y=50)
janela.geometry("250x100+200+200")
janela.mainloop()
  • Thanks buddy. it worked perfectly !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.