python shell运行快捷键是什么,Python脚本无法在键盘快捷键上运行

博主遇到了一个问题,即一个Python脚本只能从终端运行,无法通过键盘快捷方式执行。问题在于键盘快捷方式配置。解决方案是创建一个简单的Shell脚本,该脚本调用Python脚本,并将这个Shell脚本设置为键盘快捷方式的命令。这样,通过启动终端并运行Shell脚本,可以成功执行Python脚本,同时实现通过键盘快捷方式打开HTML文档和上传图片到Imgur。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

So i have plenty of scripts which i run from keyboard shortcuts, things like uploading screenshots to imgur and putting links in the clipboard, stuff for digitising plots, etc.

I have this current script, which only runs from the terminal, and not when i try to run it as a keyboard shortcut.

I'm trying to run it via the System > Preferences > Keyboard Shortcuts on Scientific linux 6.4.

I've included the script below, in case there's anything specific about it which would stop it from working.

#!/usr/bin/python

import fileinput, os

import subprocess

from pygments import highlight

from pygments.lexers import get_lexer_by_name, guess_lexer

import pygments.formatters as formatters

#stdin = "\n".join([line for line in fileinput.input()])

p = subprocess.Popen(["xclip", "-selection", "primary", "-o"], stdout=subprocess.PIPE)

code, err = p.communicate()

if not err:

lexer = guess_lexer(code)

print lexer.name

imageformatter = formatters.ImageFormatter(linenos=True, cssclass="source", font_name="Liberation Mono")

formatter = formatters.HtmlFormatter(linenos=True, cssclass="source")

HTMLresult = highlight(code, lexer, formatter)

Jpgresult = highlight(code, lexer, imageformatter, outfile=open("syntax.png", "w"))

with open("syntax.html", "w") as f:

f.write("

")

f.write(HTMLresult)

f.write("")

# os.system("pdflatex syntax.tex")

os.system("firefox syntax.html")

os.system("uploadImage.sh syntax.png")

else:

print err

The way it works, is by extracting the clipboard selection using xclip, using pygments on the text, and then both creating an html document and opening it in firefox, and uploading an image to imgur (using another script i have, which i know 100% works), and then putting that image url back into the clipboard.

The bin folder it resides in is in my path.

I've tried all of:

script

script.sh (where this file is just a shell script which calls the python script)

/home/will/bin/script

/home/will/bin/script.sh

as the command in the keyboard preferences.

If i change the contents of these scripts to just something like notify-send "hello", and that then produces the notification message, so i'm fairlyconfident it's a probelm with the script, and not the keyboard shortcuts menu.

解决方案

I ran into exactly the same issue. Here's how I fixed it:

First of all, I wrote a one-liner shell script that looked something like python /home/user/Scripts/script.py where "/home/user/Scripts/script.py" was the location of my Python. I placed this shell script in the executable path.

Next, when I went to make my shortcut, I didn't tell the computer to run the file. I told the computer to start a terminal and gave the shell script as an argument to that terminal. In my case, it looked something like:xfce4-terminal -x ralia.sh.

This works for me.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值