为什么我不能在Python中通过PS gui运行shell命令?
我正试图通过python的gui(PySimple)在终端中运行avrdude命令。。。它不会返回任何错误,但是,它也不工作。以下是问题:为什么我不能在Python中通过PS gui运行shell命令?,python,shell,pysimplegui,avrdude,Python,Shell,Pysimplegui,Avrdude,我正试图通过python的gui(PySimple)在终端中运行avrdude命令。。。它不会返回任何错误,但是,它也不工作。以下是问题: 为什么它不起作用?或者,如果我想运行该脚本,可以使用txt文件或简单的shell命令,如“ls”,请查看代码 如何在python中看到shell终端?这将帮助我了解发生了什么 原来的想法 import PySimpleGUI as sg import subprocess layout = [ [sg.Text('Filename')],
import PySimpleGUI as sg
import subprocess
layout = [
[sg.Text('Filename')],
[sg.Input('', key='filename'), sg.FileBrowse()],
[sg.Button('Ok'), sg.Button('Cancel')],
]
window = sg.Window('Test', layout)
while True:
event, values = window.read()
if event in ('Exit', None): break
#print(event, values)
if event == 'Ok':
print(event, values)
#run the terimanl code???
#subprocess.run('/Users/mu234/Desktop/myScript_sm-ir.txt')
subprocess.run('ls')
window.close()
经过几天你的评论(如MikeyB等)和我的几次讨论,谷歌,我已经移动了一点,并将在这里尝试如何处理这个问题,然而,当文件被处理时,不知何故整个程序无法工作
import subprocess
import sys
import PySimpleGUI as sg
sg.theme('Dark Blue 4')
def main():
layout = [
[sg.Output(size=(110,40), background_color='black', text_color='white')],
##[sg.T('Prompt> '), sg.Input(key='-IN-', do_not_clear=False)], #type in prompt
[sg.Text('Choose the script you want to process')],
[sg.Input('', key='filename'), sg.FileBrowse()], #choose a file you want to process with the script
#[sg.Button('Process'), sg.Button('Cancel'), sg.Button('Exit')]], #process the chosen file, else exit
[sg.Button('Process', bind_return_key=True), sg.Button('Exit')] ]
window = sg.Window('Realtime Shell Command Output', layout)
while True: # Event Loop
event, values = window.read()
# print(event, values)
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == 'Process':
#print(event, values)
runCommand(cmd=values['filename'], window=window)
window.close()
def runCommand(cmd, timeout=None, window=None):
nop = None
""" run shell command
@param cmd: command to execute
@param timeout: timeout for command execution
@param window: the PySimpleGUI window that the output is going to (needed to do refresh on)
@return: (return code from command, command output)
"""
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = ''
for line in p.stdout:
line = line.decode(errors='replace' if (sys.version_info) < (3, 5) else 'backslashreplace').rstrip()
output += line
print(line)
window.refresh() if window else nop # yes, a 1-line if, so shoot me
retval = p.wait(timeout)
return (retval, output)
main()
导入子流程
导入系统
将PySimpleGUI导入为sg
sg.主题(“深蓝色4”)
def main():
布局=[
[sg.输出(大小=(110,40),背景颜色=黑色,文本颜色=白色],
##[sg.T('Prompt>')、sg.Input(key='-IN-',do_not_clear=False)]、键入提示
[sg.Text('选择要处理的脚本'),
[sg.Input(“”,key='filename'),sg.FileBrowse()],选择要使用脚本处理的文件
#[sg.Button('Process')、sg.Button('Cancel')、sg.Button('Exit')]],#处理所选文件,否则退出
[sg.Button('Process',bind_return_key=True),sg.Button('Exit')]]
window=sg.window('实时Shell命令输出',布局)
为True时:#事件循环
事件,值=window.read()
#打印(事件、值)
如果事件发生(sg.WIN_关闭,“退出”):
打破
elif事件==“进程”:
#打印(事件、值)
runCommand(cmd=values['filename'],window=window)
window.close()
def RUNCOMAND(命令,超时=无,窗口=无):
nop=无
“”“运行shell命令
@param cmd:要执行的命令
@参数超时:命令执行超时
@param window:输出将要进入的PySimpleGUI窗口(需要刷新)
@return:(命令返回代码,命令输出)
"""
p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.stdout)
输出=“”
对于p.stdout中的行:
line=line.decode(如果(系统版本信息)<(3,5)否则为“反斜杠替换”),则错误为“替换”。rstrip()
输出+=行
打印(行)
window.refresh()如果window-else不,是的,一行如果,那么就开枪吧
retval=p.wait(超时)
返回(返回、输出)
main()
GUI在那里,用户可以轻松地浏览和选择文件(脚本)并对其进行处理,但是,不知何故,脚本被卡住了。例如,这是我在GUI中获得的输出:
/Users/mu234/Desktop/myScript_am-ir.txt:第3行:-:未找到命令
/Users/mu234/Desktop/myScript_am-ir.txt:第5行:avrdude:command not
发现
/Users/mu234/Desktop/myScript_am-ir.txt:第9行:-:未找到命令
/Users/mu234/Desktop/myScript_am-ir.txt:line 11:avrdude:command not
发现
/Users/mu234/Desktop/myScript_am-ir.txt:第14行:-:命令不可用
发现
/Users/mu234/Desktop/myScript_am-ir.txt:line 16:avrdude:command not
发现
有效吗
简言之,它不起作用。我在想权限有问题吗?我在OSX上,我想问题在于访问该文件的权限。。。脚本并在scrip中执行命令?通常,该脚本只有几行代码(请查看此处可能的命令:)
例如,如果只使用了“echo…”,它工作正常,正如您在上面的打印中所看到的。。。无论如何,请看一下代码,并指出可能的错误。当您使用
subprocess.run(command)
时,它会将输出写入sys.stdout(/dev/stdout on linux),这基本上是终端/控制台的输出
你可能想做更多类似的事情
from subprocess import Popen, PIPE
proc = Popen(command.split(' '), stdin=PIPE, stdout=PIPE, stderr=PIPE) # spawns the process, but redirects stdin, stdout, and stderr from the terminal/console
proc_output = proc.communicate()[0].decode('utf-8') # reads stdout output and stores string into a variable
# Do something with proc_output in your GUI
我还看到在你的注释行中,你试图用一个.txt文件生成一个进程?如果这是一个脚本,那么您需要更改文件扩展名(windows上需要,Shebang对于linux就足够了)
另外,您不必使用子流程模块来运行脚本,只需从脚本中导入函数并从其他脚本/文件调用这些函数即可
谷歌和文档是您的朋友当您使用
子流程时。运行(命令)
它会将输出写入sys.stdout(/dev/stdout,linux上),这基本上是终端/控制台的输出
你可能想做更多类似的事情
from subprocess import Popen, PIPE
proc = Popen(command.split(' '), stdin=PIPE, stdout=PIPE, stderr=PIPE) # spawns the process, but redirects stdin, stdout, and stderr from the terminal/console
proc_output = proc.communicate()[0].decode('utf-8') # reads stdout output and stores string into a variable
# Do something with proc_output in your GUI
我还看到在你的注释行中,你试图用一个.txt文件生成一个进程?如果这是一个脚本,那么您需要更改文件扩展名(windows上需要,Shebang对于linux就足够了)
另外,您不必使用子流程模块来运行脚本,只需从脚本中导入函数并从其他脚本/文件调用这些函数即可
谷歌和文档是你的朋友为快速回复和信息干杯。不太清楚你说的“代替使用…”是什么意思?脚本背后的想法是将所有功能作为一个文件,可以快速执行,特别是使用GUI,只需用鼠标按一下……如果您的程序只是一个简单的文件启动器,则无需注意这一点,当我再仔细看一看时,似乎确实如此。既然我的答案回答了你的问题,也许你可以把它标记为正确答案?(你以后可以随时更改)谢谢,不知怎么的,它不起作用了,我得到的错误,可能是我的错,我怎么放代码,很抱歉,这是新的…:Ok{'filename':'/Users/mu234/Desktop/myScript_sm-ir_co