I'm working on a project which will allow me to simulate typing so I can automate email responses. For this I'm using Tkinter to create a pop-up window that is always on top of all other windows (always in foreground) with a button. After pressing the button, I have a two second pause to allow me to click into the different window and then a Pynput function referencing the string variable (the text response in the email) is executed.

In my variable I have a couple random.choice methods included with a list of synonyms to vary the word choice in the response varies each time (or most of the time). Right now when I run my script, the window appears and a response is typed although the random.choice does not execute upon each button click but rather each time I close the Tkinter window and run the script again. I would like to be able to keep the Tkinter window open and not have to re-run the script each time in order for the random.choice to work properly. How can I modify my script so that random.choice works each button click?

Here is the code:

from tkinter import * 
from pynput.keyboard import Key, Controller 
keyboard = Controller()
import time
import random

def center_window(w=300, h=200):
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))

rej = random.choice(["Hey", "Hello", "What's up"]) +", thanks for reaching out! 
This " + random.choice(["idea", "concept", "project"]) + " was " \ 
+ random.choice(["unique,", "interesting,", "creative,", "solid,", "clever,"]) + 
" although it doesn't quite fit our vision right now. " \
"We appreciate the submission and look forward to receiving more from you in the future."

root = Tk()
root.title('Automate')

def Reject():
    time.sleep(2)
    keyboard.type(rej)

RejectButton = Button(root, text="Reject", padx=50, pady=10, command=Reject, fg="#ffffff",
bg="#000000")
RejectButton.pack()

center_window(300, 250)
root.lift()
root.wm_attributes("-topmost", 1)

root.mainloop()

Let's say that the random.choice selects "Hey", "concept" and "unique" after running the script. Then the output after each button press until closing the Tkinter window and re-running the script again will be:

Hey, thanks for reaching out! This concept was unique, although it doesn't quite fit our vision right now. We appreciate the submission and look forward to receiving more from you in the future.

1

There are 1 best solutions below

3
On BEST ANSWER

As a solution before fixing your indentation, try moving rej inside the function Reject():

def Reject():
    rej = random.choice(["Hey", "Hello", "What's up"]) +", thanks for reaching out! 
    This " + random.choice(["idea", "concept", "project"]) + " was " \ 
    + random.choice(["unique,", "interesting,", "creative,", "solid,", "clever,"]) + 
    " although it doesn't quite fit our vision right now. " \
    "We appreciate the submission and look forward to receiving more from you in the 
    future."

    time.sleep(2)
    keyboard.type(rej)
    print(rej)

Though this gave me a EOL error, this is same as what you posted, or you could also say something like:

rej = random.choice(["Hey", "Hello", "What's up"]) +", thanks for reaching out! This " + random.choice(["idea", "concept", "project"]) + " was "  + random.choice(["unique,", "interesting,", "creative,", "solid,", "clever,"]) + " although it doesn't quite fit our vision right now. We appreciate the submission and look forward to receiving more from you in the future."

Whats the difference, its all in one line, while there you used in multiple lines, for which I recommend using triple quotes and f strings for dynamic insertion of variables, like:

def Reject():
    rej = f'''{random.choice(["Hey", "Hello", "What's up"])}, thanks for reaching out! 
This {random.choice(["idea", "concept", "project"])} was {random.choice(["unique,", "interesting,", "creative,", "solid,", "clever,"])} although it doesn't quite fit our vision right now.
We appreciate the submission and look forward to receiving more from you in the future.'''
    time.sleep(2)
    keyboard.type(rej)
    print(rej)

Everything from the start of the code till the end of the normal code block will just be run once, and your rej is in between this point, and will get executed just once, and hence that explains why it stays same through out the whole mainloop(), so to make it properly randomized on each button click, you have to move it to inside the function. So calling that function each time will run the rej each time, making it random.