如何在脚本中使用random.choice,以便在不关闭Tkinter窗口的情况下,每次按下按钮后都会得到不同的结果



我正在做一个项目,该项目将允许我模拟打字,以便我可以自动回复电子邮件。为此,我使用Tkinter创建了一个弹出窗口,它总是在所有其他窗口的顶部(总是在前台(,并带有一个按钮。按下按钮后,我有两秒钟的暂停时间,可以点击不同的窗口,然后执行一个引用字符串变量(电子邮件中的文本响应(的Pynput函数。

在我的变量中,我有几个随机选择方法,其中包括一个同义词列表,以改变响应中的单词选择,每次(或大部分时间(都会有所不同。现在,当我运行脚本时,会出现窗口并键入响应,尽管random.choice不是在每次单击按钮时执行,而是在每次关闭Tkinter窗口并再次运行脚本时执行。我希望能够保持Tkinter窗口打开,而不必每次都重新运行脚本才能使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选择";嘿"quot;概念";以及";独特的";运行脚本后。然后,每次按下按钮直到关闭Tkinter窗口并重新运行脚本后的输出将是:

嘿,谢谢你的联系!这个概念是独一无二的,尽管不太符合我们现在的愿景。我们感谢您的提交并期待着在未来收到更多您的来信。

作为修复缩进之前的解决方案,请尝试在函数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,使其成为随机的。

相关内容

最新更新