简体   繁体   English

双击通过 Shell 脚本运行 Python 脚本

[英]Run Python Script through Shell Script by Double Click

I have a runGUI.sh script and I want to run a Python script through it.我有一个runGUI.sh脚本,我想通过它运行一个 Python 脚本。 The Python Script is a GUI.py that I want to make it run as an App, by either double clicking the Shell script or the Python Script itself (ideally I would like to have both ways). Python 脚本是一个GUI.py ,我想让它作为应用程序运行,方法是双击 Shell 脚本或 Python 脚本本身(理想情况下我想要两种方式)。

I tried everything that others recommend:我尝试了其他人推荐的所有内容:

  • I changed permissions to both scripts and also made them executable.我更改了两个脚本的权限并使它们可执行。
  • I added the appropriate #!/bin/sh or #!/bin/bash in the Shell Script and #!/usr/bin/env python at the Python Script.我在 Shell 脚本中添加了适当的#!/bin/sh#!/bin/bash ,在 Python 脚本中添加了#!/usr/bin/env python
  • I tried all combinations in the Shell script for the Python executable command, like python GUI.py or ./GUI.py , and even with the full path of the Python Script.我在 Shell 脚本中尝试了 Python 可执行命令的所有组合,例如python GUI.py./GUI.py ,甚至使用 Python 脚本的完整路径。

The result is the same.结果是一样的。 If I double click the GUI.py or the runGUI.sh, and because they are executable, I get the options:如果我双击 GUI.py 或 runGUI.sh,并且因为它们是可执行的,我会得到以下选项:

Options when double clicking双击时的选项

Regardless of "Run in Terminal" or "Run", nothing happens.无论“在终端中运行”还是“运行”,都没有任何反应。

If I run either scripts through terminal, like ./GUI.py or runGUI.sh , the Python file works fine and the GUI is opening in both cases.如果我通过终端运行任一脚本,如./GUI.pyrunGUI.sh ,Python 文件工作正常,GUI 在这两种情况下都打开。

Is there a way to open this GUI by double clicking the Shell or Python scripts?有没有办法通过双击 Shell 或 Python 脚本来打开这个 GUI?

I am using Ubuntu 16.04, standard Gnome, have installed Zshell if this helps and I built the Python GUI with the Tkinter module.我正在使用 Ubuntu 16.04,标准 Gnome,如果有帮助,我已经安装了 Zshell,并且我使用 Tkinter 模块构建了 Python GUI。

Diego is mostly correct, but they left out one important detail that will cause it to fail.迭戈大体上是正确的,但他们遗漏了一个会导致它失败的重要细节。 I am assuming you are using a python terminal application.我假设您使用的是 python 终端应用程序。 It should look more like this:它应该看起来更像这样:

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=My GUI
Exec=python3 /path/to/GUI.py
Terminal=true

The Terminal=true makes it so it actually runs when you click on it. Terminal=true使它在您单击它时实际运行。 Also, put your python alias (python3,py,python,etc.) before it.另外,将您的 python 别名(python3、py、python 等)放在它前面。 Lastly, if you don't want an icon, just omit it rather than putting Icon= .最后,如果您不想要图标,只需省略它而不是放置Icon=

I know this is an old question, but I would have forgotten it too if I ran into this issue any less recently.我知道这是一个老问题,但如果我最近遇到这个问题,我也会忘记它。

Create a file name mygui.desktop in ~/.local/share/applications ~/.local/share/applications创建文件名mygui.desktop

[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=My GUI
Icon=
Exec=/path/to/GUI.py

and then mygui will be appearing as an application that you can launch clicking it. 然后mygui将显示为可以单击它的应用程序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM