亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在我的腳本中使用 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 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號