python - 如何在我的脚本中使用 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 窗口并再次重新运行脚本后的输出将是:
嘿,谢谢你伸出援手!这个概念是独一无二的,尽管它现在并不完全符合我们的愿景。我们感谢您的提交,并期待将来收到您的更多信息。
解决方案
作为修复缩进之前的解决方案,请尝试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
,使其随机化。
推荐阅读
- rust - 使用线程和异步/等待时如何解决“无法返回引用本地数据的值”?
- android - 无法捕获 Exec 类型的 Gradle 任务的异常
- angular - 给出错误结果的时刻日期格式
- python - Python:打印列差异
- javascript - Angular / Typescript使用方法中的参数值作为js对象值
- r - 整个矩阵行只取 3 个数值的最后一个值
- java - 具有不支持线程的函数的 Java ExecutorService 线程
- c# - 在 C# 中,`new List 的结果容量是什么
() {3,5};`? - javascript - Vuetify - 如何使用 setTimeout 函数更改 v-img 源
- javascript - 使用 npx create-react-app newapp 命令,但在一段时间后它显示 57 个包正在寻找资金