![](/img/trans.png)
[英]Struggling to make a program that has variables that change when a button is pressed on Python
[英]How automatically change to different program when a button is pressed in Python
我希望我的第一個程序在按下按鈕時自動切換到第二個程序。
我嘗試了break和sys.exit(),但是那讓我回到了命令行,我不想這么做,我想消除使用鼠標和鍵盤來更改為其他程序的麻煩。
這些程序用於一台微型計算機,該計算機每天將執行多次相同的任務; 安裝后,我不想連接鍵盤或鼠標。
您可以為此使用subprocess
模塊。 為要觸發的腳本創建一個Popen
對象,然后退出父進程,而無需等待子進程完成:
import subprocess
import sys
def on_button_pressed():
subprocess.Popen(["path/to/script", "arg1", "arg2"], shell=False)
sys.exit()
您可以將第二個程序封裝在一個函數中,然后從第一個程序中調用該函數。 或者,如果第二個程序位於其自己的模塊中,則可以導入該模塊。 導入模塊將在其中執行代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.