我如何在我的脚本中使用 random.choice 以便在每次按下按钮后得到不同的结果而不关闭 Tkinter window?

How can I use random.choice in my script so I get different a different result after each button press without closing the Tkinter window?

我正在从事一个项目,该项目将允许我模拟打字,以便自动回复电子邮件。为此,我使用 Tkinter 创建一个弹出窗口 window,它始终位于所有其他 windows 之上(始终在前台),带有一个按钮。按下按钮后,我有两秒钟的暂停时间,让我可以单击不同的 window,然后执行引用字符串变量(电子邮件中的文本响应)的 Pynput 函数。

在我的变量中,我有几个 random.choice 方法包含在同义词列表中,以改变每次(或大部分时间)响应中的单词选择。现在,当我 运行 我的脚本时, window 出现并输入响应,尽管 random.choice 不会在每次单击按钮时执行,而是每次我关闭 Tkinter window 和 运行 脚本。我希望能够保持 Tkinter window 打开,而不必每次都重新 运行 脚本以使 random.choice 正常工作。我如何修改我的脚本,以便 random.choice 在每次单击按钮时都能正常工作?

代码如下:

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()

假设 random.choice 在 运行 脚本之后选择了“Hey”、“concept”和“unique”。然后每次按下按钮后的输出直到关闭 Tkinter window 并再次重新 运行 脚本将是:

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.

作为修复缩进之前的解决方案,请尝试在函数 Reject():

中移动 rej
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)

虽然这给了我一个 EOL 错误,但这与您发布的内容相同,或者您也可以这样说:

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."

有什么区别,它全部在一行中,而您在多行中使用,为此我建议使用三引号和 f 字符串来动态插入变量,例如:

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)

从代码开始到 正常 代码块结束的所有内容都只会 运行 一次,而您的 rej 介于两者之间这一点,并且将只执行一次,因此这解释了为什么它在整个 mainloop() 中保持不变,因此要使其在每次单击按钮时正确随机化,您必须将其移动到函数内部。因此,每次调用该函数都会 运行 每次 rej,使其随机化。