3

我有一个非常简单的问题,关于在用 python 3 编写的脚本中杀死子子进程。

在哪里,

如果我有,

my_process = None

def open_storage():
    my_process = subprocess.Popen("waffles.exe")

def kill_children():
    my_process.kill()

打电话后open_storage(),如果我打电话kill_children(),我会得到

AttributeError: 'NoneType' object has no attribute 'kill'

但如果我有,

my_process = 无

my_process = subprocess.Popen("waffles.exe")

def kill_children():
    my_process.kill()

一切正常。

谁能解释这种奇怪的行为?我需要open_storage()作为一个函数,因为它被设计为由 tkinter 按钮触发。

谢谢。

4

1 回答 1

2

您需要使用全局变量,否则它将使用局部变量。

def open_storage():
    global my_process
    my_process = subprocess.Popen("waffles.exe")
于 2013-07-19T23:28:28.473 回答