首页 > 解决方案 > 如何在我的脚本中使用 random.choice 以便在每次按下按钮后得到不同的结果而不关闭 Tkinter 窗口?

问题描述

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

在我的变量中,我有几个 random.choice 方法包含在同义词列表中,以改变响应中的单词选择每次(或大部分时间)都不同。现在,当我运行我的脚本时,会出现窗口并输入响应,尽管 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 在运行脚本后选择“Hey”、“concept”和“unique”。然后每次按下按钮直到关闭 Tkinter 窗口并再次重新运行脚本后的输出将是:

嘿,谢谢你伸出援手!这个概念是独一无二的,尽管它现在并不完全符合我们的愿景。我们感谢您的提交,并期待将来收到您的更多信息。

标签: pythontkinterpynput

解决方案


作为修复缩进之前的解决方案,请尝试rej在函数内部移动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)

虽然这给了我一个 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,使其随机化。


推荐阅读