为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

料青山看我应如是 2023-10-06 11:01:12
我正在开发一个项目,该项目允许我模拟打字,以便我可以自动回复电子邮件。为此,我使用 Tkinter 创建一个弹出窗口,该窗口始终位于所有其他窗口的顶部(始终位于前台),并带有一个按钮。按下按钮后,我会停顿两秒,以便单击进入不同的窗口,然后执行引用字符串变量(电子邮件中的文本响应)的 Pynput 函数。在我的变量中,我有几个 random.choice 方法,其中包含同义词列表,用于改变响应中的单词选择每次(或大多数时间)都会变化。现在,当我运行脚本时,会出现窗口并键入响应,尽管 random.choice 不会在每次单击按钮时执行,而是在每次关闭 Tkinter 窗口并再次运行脚本时执行。我希望能够保持 Tkinter 窗口打开,而不必每次都重新运行脚本以使 random.choice 正常工作。如何修改我的脚本,以便 random.choice 在每次单击按钮时都能起作用?这是代码:from tkinter import * from pynput.keyboard import Key, Controller keyboard = Controller()import timeimport randomdef 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 窗口并再次重新运行脚本后的输出将是:嘿,感谢您伸出援手!这个概念很独特,尽管它不太符合我们现在的愿景。我们感谢您的提交,并期待将来收到您的更多信息。
查看完整描述

1 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

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


查看完整回答
反对 回复 2023-10-06
  • 1 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信