简体   繁体   English

在Python中按下按钮时如何自动更改为其他程序

[英]How automatically change to different program when a button is pressed in Python

I'd like for my first program to automatically switch over to a second program when a button is pressed. 我希望我的第一个程序在按下按钮时自动切换到第二个程序。

I tried break and sys.exit() but that put me back at the command line, which I don't want to I'm trying to eliminate having to use the mouse and keyboard to change to a different program. 我尝试了break和sys.exit(),但是那让我回到了命令行,我不想这么做,我想消除使用鼠标和键盘来更改为其他程序的麻烦。

The programs are for a microcomputer that will be doing the same tasks many times a day; 这些程序用于一台微型计算机,该计算机每天将执行多次相同的任务; I don't want to have a keyboard or mouse connected after the installation. 安装后,我不想连接键盘或鼠标。

You can use the subprocess module for this. 您可以为此使用subprocess模块。 Create a Popen object for the script you want triggered, and then exit the parent process without waiting for the child to complete: 为要触发的脚本创建一个Popen对象,然后退出父进程,而无需等待子进程完成:

import subprocess
import sys

def on_button_pressed():
    subprocess.Popen(["path/to/script", "arg1", "arg2"], shell=False)
    sys.exit()

You may encapsulate the second program in a function, and call the function from your first program. 您可以将第二个程序封装在一个函数中,然后从第一个程序中调用该函数。 Alternatively, if the second program resides in its own module, you may import that module. 或者,如果第二个程序位于其自己的模块中,则可以导入该模块。 Importing the module will execute the code inside of it. 导入模块将在其中执行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 努力制作一个程序,当在 Python 上按下按钮时,变量会发生变化 - Struggling to make a program that has variables that change when a button is pressed on Python 如何在 Python、Kivy 中按下按钮时更改按钮的背景颜色 - How to change the background colour of a button when it is pressed in Python, Kivy 在python中按下键时如何停止程序? - How to stop a program when a key is pressed in python? 如何在按下按钮时更改按钮颜色并在按下其他按钮时更改为原始颜色。 按钮是使用 python 中的 class 创建的 - How do i change a button color when it is pressed and to original when other button pressed. Buttons are created using class in python 使用 PySimpleGUI 按下按钮时如何更改文本 - How to change text when a button is pressed with PySimpleGUI 如何在Bokeh中按下按钮时更改数据帧? - How to change dataframe when button is pressed in Bokeh? 按下按钮时如何更改空间? - How to change a space when a button is pressed with kivy? 在Flash程序中按下按钮时可以执行Python命令吗? - Can a Python Command be executed when a button is pressed in a Flash Program 在python中运行程序时如何检查按钮是否被按下 - How to check if a button is pressed while running a program in python Python xbox 控制器输入。 如何更改按下按钮时发送的消息 - Python xbox controller inputs. How to change the message send when a button is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM